Skip to content

Commit cb935af

Browse files
fix: heading borders with indentation (#164)
* fix: heading borders when there is no first heading Don't assume that there is always a first level heading when counting the indentation level. * 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. * fixup! fix: heading border with indentation
1 parent c37f698 commit cb935af

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

lua/render-markdown/core/node_info.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ function NodeInfo:level()
5858
local parent = self.node:parent()
5959
while parent ~= nil and parent:type() ~= 'document' do
6060
if parent:type() == 'section' then
61-
level = level + 1
61+
local child = parent:child(0)
62+
child = child and child:child(0)
63+
local is_first_heading = child and child:type() == 'atx_h1_marker'
64+
if not is_first_heading then
65+
level = level + 1
66+
end
6267
end
6368
parent = parent:parent()
6469
end

lua/render-markdown/render/base.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ end
4141

4242
---@protected
4343
---@param line { [1]: string, [2]: string }[]
44+
---@param level integer?
4445
---@return { [1]: string, [2]: string }[]
45-
function Base:indent_virt_line(line)
46+
function Base:indent_virt_line(line, level)
4647
local indent = self.config.indent
4748
if not indent.enabled then
4849
return line
4950
end
50-
local level = self.info:level() - 1
51+
level = level or self.info:level()
5152
if level <= 0 then
5253
return line
5354
end

lua/render-markdown/render/heading.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ function Render:border(width)
175175
{ self.heading.above:rep(width - self.heading.left_pad - prefix), background },
176176
}
177177
if str.width(self.info:line('above')) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
178+
if self.data.level > 1 then
179+
line_above = self:indent_virt_line(line_above, 1)
180+
end
178181
self.marks:add(true, self.info.start_row - 1, 0, {
179182
virt_text = line_above,
180183
virt_text_pos = 'overlay',

0 commit comments

Comments
 (0)