|
1 | 1 | local parser = {};
|
2 | 2 | -- local renderer = require("markview/renderer");
|
3 | 3 |
|
4 |
| -parser.fiter_lines = function (buffer, from, to, marker) |
| 4 | +parser.fiter_lines = function (buffer, from, to) |
5 | 5 | local captured_lines = vim.api.nvim_buf_get_lines(buffer, from, to, false);
|
6 | 6 | local filtered_lines = {};
|
7 | 7 | local indexes = {};
|
8 | 8 | local spaces = {};
|
9 | 9 |
|
| 10 | + local withinCodeBlock; |
| 11 | + local parent_marker; |
| 12 | + |
10 | 13 | local tolarence = 3;
|
11 | 14 | local found = 0;
|
12 | 15 |
|
13 | 16 | for l, line in ipairs(captured_lines) do
|
14 |
| - if l ~= 1 and line:match(marker) then |
15 |
| - break; |
| 17 | + if l ~= 1 then |
| 18 | + if withinCodeBlock ~= true and line:match("^%s*([+%-*])") then |
| 19 | + break; |
| 20 | + elseif withinCodeBlock ~= true and line:match("^%s*(%d+%.)") then |
| 21 | + break; |
| 22 | + end |
16 | 23 | end
|
17 | 24 |
|
18 | 25 | if found >= tolarence then
|
19 | 26 | break;
|
20 | 27 | end
|
21 | 28 |
|
22 |
| - local spaces_before = vim.fn.strchars(line:match("(%s*)")); |
| 29 | + local spaces_before = vim.fn.strchars(line:match("^(%s*)")); |
23 | 30 |
|
24 |
| - if not line:match(marker) then |
25 |
| - spaces_before = math.max(0, spaces_before - vim.fn.strchars(marker .. " ")); |
| 31 | + if line:match("(```)") and withinCodeBlock ~= true then |
| 32 | + withinCodeBlock = true; |
| 33 | + goto withinElement; |
| 34 | + elseif line:match("(```)") and withinCodeBlock == true then |
| 35 | + withinCodeBlock = false; |
| 36 | + goto withinElement; |
| 37 | + elseif withinCodeBlock == true then |
| 38 | + goto withinElement; |
26 | 39 | end
|
27 | 40 |
|
| 41 | + if line:match("^%s*([+%-*])") then |
| 42 | + parent_marker = line:match("^%s*([+%-*])"); |
| 43 | + elseif line:match("^%s*(%d+%.)") then |
| 44 | + parent_marker = line:match("^%s*(%d+%.)"); |
| 45 | + end |
| 46 | + |
| 47 | + if not line:match("^%s*([+%-*])") and not line:match("^%s*(%d+%.)") and parent_marker then |
| 48 | + spaces_before = math.max(0, spaces_before - vim.fn.strchars((parent_marker or "") .. " ")); |
| 49 | + end |
| 50 | + |
| 51 | + ::withinElement:: |
| 52 | + |
28 | 53 | table.insert(filtered_lines, line);
|
29 | 54 | table.insert(indexes, l);
|
30 | 55 | table.insert(spaces, spaces_before)
|
@@ -317,7 +342,7 @@ parser.md = function (buffer, TStree, from, to)
|
317 | 342 | local marker_text = vim.treesitter.get_node_text(marker, buffer);
|
318 | 343 | local symbol = marker_text:gsub("%s", "");
|
319 | 344 |
|
320 |
| - local list_lines, lines, spaces = parser.fiter_lines(buffer, row_start, row_end, symbol); |
| 345 | + local list_lines, lines, spaces = parser.fiter_lines(buffer, row_start, row_end); |
321 | 346 | local spaces_before_marker = list_lines[1]:match("^(%s*)" .. symbol .. "%s*");
|
322 | 347 |
|
323 | 348 | local c_end, _ = parser.get_list_end_range(buffer, row_start, row_end, symbol)
|
|
0 commit comments