Skip to content

Commit a38dd1f

Browse files
committed
fix(parser): Added logic for supporting markers inside code blocks
Improved logic for finding lines within a list item. Fixes a bug preventing lost item markers from existing within lines of code blocks. SUPPORT FOR TABLES WILL NOT BE ADDED AS THERE IS NO NEED TO NEST TABLES WITHIN LIST ITEMS. BLOCK QUOTES WORK FINE SO LOGIC FOR THEM WILL NOT BE ADDED. See: #69
1 parent 6cbbd9b commit a38dd1f

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

lua/markview/parser.lua

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
11
local parser = {};
22
-- local renderer = require("markview/renderer");
33

4-
parser.fiter_lines = function (buffer, from, to, marker)
4+
parser.fiter_lines = function (buffer, from, to)
55
local captured_lines = vim.api.nvim_buf_get_lines(buffer, from, to, false);
66
local filtered_lines = {};
77
local indexes = {};
88
local spaces = {};
99

10+
local withinCodeBlock;
11+
local parent_marker;
12+
1013
local tolarence = 3;
1114
local found = 0;
1215

1316
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
1623
end
1724

1825
if found >= tolarence then
1926
break;
2027
end
2128

22-
local spaces_before = vim.fn.strchars(line:match("(%s*)"));
29+
local spaces_before = vim.fn.strchars(line:match("^(%s*)"));
2330

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;
2639
end
2740

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+
2853
table.insert(filtered_lines, line);
2954
table.insert(indexes, l);
3055
table.insert(spaces, spaces_before)
@@ -317,7 +342,7 @@ parser.md = function (buffer, TStree, from, to)
317342
local marker_text = vim.treesitter.get_node_text(marker, buffer);
318343
local symbol = marker_text:gsub("%s", "");
319344

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);
321346
local spaces_before_marker = list_lines[1]:match("^(%s*)" .. symbol .. "%s*");
322347

323348
local c_end, _ = parser.get_list_end_range(buffer, row_start, row_end, symbol)

0 commit comments

Comments
 (0)