Skip to content

Commit

Permalink
Allow for different width headings
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
MeanderingProgrammer committed Apr 3, 2024
1 parent 2b98d16 commit a0da7cf
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ M.render = function(namespace, root)

if capture == 'heading' then
local level = #value

local heading = list.cycle(state.config.headings, level)
-- Available width is level + 1, where level = number of `#` characters and one is added
-- to account for the space after the last `#` but before the heading title
local padding = level + 1 - vim.fn.strdisplaywidth(heading)

local background = list.clamp_last(highlights.heading.backgrounds, level)
local foreground = list.clamp_last(highlights.heading.foregrounds, level)

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

0 comments on commit a0da7cf

Please sign in to comment.