Skip to content

Commit df98da8

Browse files
Handle leading spaces for list_items
## Details As reported in the following issues: * #2 * #5 The list_items we capture from tree-sitter will sometimes have leading spaces leading to us rendering a bullet point incorrectly. Add some logic on our side to handle these cases.
1 parent d7d793b commit df98da8

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

demo/sample.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ if __name__ == "__main__":
2222

2323
* List Item 1
2424
* List Item 2
25-
* Nested List Item 1
26-
* Nested List Item 2
25+
* Nested List Item 1
26+
* Nested List Item 2
2727
* List Item 3
2828

2929
| Heading 1 | Heading 2 |

lua/render-markdown/init.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ end
9595

9696
M.namespace = vim.api.nvim_create_namespace('render-markdown.nvim')
9797

98+
M.clear = function()
99+
-- Remove existing highlights / virtual text
100+
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
101+
end
102+
98103
M.refresh = function()
99104
if not vim.tbl_contains(state.config.file_types, vim.bo.filetype) then
100105
return
101106
end
102-
103-
-- Remove existing highlights / virtual text
104-
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
105-
107+
-- Needs to happen after file_type check and before mode check
108+
M.clear()
106109
if not vim.tbl_contains(state.config.render_modes, vim.fn.mode()) then
107110
return
108111
end
@@ -139,7 +142,11 @@ M.refresh = function()
139142
hl_eol = true,
140143
})
141144
elseif capture == 'item' then
142-
local virt_text = { state.config.bullet, highlights.bullet }
145+
-- List items from tree-sitter should have leading spaces removed, however there are known
146+
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
147+
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
148+
local _, leading_spaces = value:find('^%s*')
149+
local virt_text = { string.rep(' ', leading_spaces or 0) .. state.config.bullet, highlights.bullet }
143150
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
144151
end_row = end_row,
145152
end_col = end_col,

0 commit comments

Comments
 (0)