Skip to content

Commit a0da7cf

Browse files
Allow for different width headings
# Details As stated in this issue: MeanderingProgrammer#12, we currently only handle headings with a string width of 1 or 2, longer headings will overlay the titles. To fix this calculate the width of the heading to determine the amount of paddings needed. This will still fail if the heading provided is longer than the level of the heading, since we have fewer characters to work with.
1 parent 2b98d16 commit a0da7cf

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lua/render-markdown/handler/markdown.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ M.render = function(namespace, root)
1515

1616
if capture == 'heading' then
1717
local level = #value
18+
1819
local heading = list.cycle(state.config.headings, level)
20+
-- Available width is level + 1, where level = number of `#` characters and one is added
21+
-- to account for the space after the last `#` but before the heading title
22+
local padding = level + 1 - vim.fn.strdisplaywidth(heading)
23+
1924
local background = list.clamp_last(highlights.heading.backgrounds, level)
2025
local foreground = list.clamp_last(highlights.heading.foregrounds, level)
2126

22-
local virt_text = { string.rep(' ', level - 1) .. heading, { foreground, background } }
27+
local virt_text = { string.rep(' ', padding) .. heading, { foreground, background } }
2328
vim.api.nvim_buf_set_extmark(0, namespace, start_row, 0, {
2429
end_row = end_row + 1,
2530
end_col = 0,

0 commit comments

Comments
 (0)