Skip to content

Commit fcf0b32

Browse files
committed
fix: heading border with indentation
When heading borders are rendered on empty lines above the heading, those lines also need to be indented to the next level.
1 parent 13b6b94 commit fcf0b32

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lua/render-markdown/handler/markdown.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,21 @@ function Handler:section(info)
6969
return
7070
end
7171

72+
local start_row = info.start_row
73+
local heading_renderer = self.renderers.heading --[[@as render.md.render.Heading?]]
74+
-- If heading borders are on, and we reuse an empty line in the buffer for the top border, that line needs to be
75+
-- indented as well
76+
if
77+
self.config.heading.border
78+
and heading_renderer
79+
and heading_renderer:has_space_for_border('above', info, self.context.last_heading)
80+
then
81+
start_row = start_row - 1
82+
end
83+
7284
-- Each level stacks inline marks so we do not need to multiply spaces
7385
-- However skipping a level, i.e. 2 -> 5, will only add one level of spaces
74-
for row = info.start_row, info.end_row - 1 do
86+
for row = start_row, info.end_row - 1 do
7587
self.marks:add(false, row, 0, {
7688
priority = 0,
7789
virt_text = { { str.spaces(indent.per_level), 'Normal' } },

lua/render-markdown/render/heading.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ function Render:render()
7272
self:conceal_underline()
7373
end
7474

75+
---@param position 'above'|'below'
76+
---@param info render.md.NodeInfo
77+
---@param last_heading integer
78+
---@return boolean
79+
function Render:has_space_for_border(position, info, last_heading)
80+
if position == 'above' then
81+
return str.width(info:line('above')) == 0 and info.start_row - 1 ~= last_heading
82+
else
83+
return str.width(self.info:line('below')) == 0
84+
end
85+
end
86+
7587
---@private
7688
---@return integer
7789
function Render:icon()
@@ -174,7 +186,7 @@ function Render:border(width)
174186
{ self.heading.above:rep(prefix), self.data.foreground },
175187
{ self.heading.above:rep(width - self.heading.left_pad - prefix), background },
176188
}
177-
if str.width(self.info:line('above')) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
189+
if self:has_space_for_border('above', self.info, self.context.last_heading) then
178190
self.marks:add(true, self.info.start_row - 1, 0, {
179191
virt_text = line_above,
180192
virt_text_pos = 'overlay',
@@ -191,7 +203,7 @@ function Render:border(width)
191203
{ self.heading.below:rep(prefix), self.data.foreground },
192204
{ self.heading.below:rep(width - self.heading.left_pad - prefix), background },
193205
}
194-
if str.width(self.info:line('below')) == 0 then
206+
if self:has_space_for_border('below', self.info, self.context.last_heading) then
195207
self.marks:add(true, self.info.end_row + 1, 0, {
196208
virt_text = line_below,
197209
virt_text_pos = 'overlay',

0 commit comments

Comments
 (0)