Skip to content

Commit 2ddb145

Browse files
fix: indenting heading borders with single empty line between
## Details When looking at headings separated by a single blank line and no other content the alignement would of heading borders was not calculated properly with indent and heading borders enabled. For example with the following text: ```text # Heading 1 ## Heading 2 # Heading 1 ``` We would end up overindent the bottom border of the first heading and under indenting the bottom row of the seconding heading. Both of these were caused by how section ownership was changed in a recent change. In terms ownership the second heading owns the line below the first heading and the third heading owns the line below the second heading. However when actually adding a border we assume top down ownership. By adding a line owned by a different section we end up with the misalignment. To fix this only extend ownership of a section upwards if there is more than a single blank line. If it is just the single line than do not extend the section. Very similar logic applies for contracting upward. Only do this if the section does not rely on that blank line to exist.
1 parent cdb58fc commit 2ddb145

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

lua/render-markdown/core/node_info.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,19 @@ function NodeInfo:for_each_child(callback)
127127
end
128128
end
129129

130-
---@param position 'above'|'below'|'first'|'last'
130+
---@param position 'above'|'first'|'below'|'last'
131+
---@param by integer
131132
---@return string?
132-
function NodeInfo:line(position)
133-
local row = nil
133+
function NodeInfo:line(position, by)
134+
local one_line, row = self.start_row == self.end_row, nil
134135
if position == 'above' then
135-
row = self.start_row - 1
136-
elseif position == 'below' then
137-
row = self.end_row + 1
136+
row = self.start_row - by
138137
elseif position == 'first' then
139-
row = self.start_row
138+
row = self.start_row + by
139+
elseif position == 'below' then
140+
row = self.end_row - (one_line and 0 or 1) + by
140141
elseif position == 'last' then
141-
row = self.end_row - 1
142+
row = self.end_row - (one_line and 0 or 1) - by
142143
end
143144
return row ~= nil and vim.api.nvim_buf_get_lines(self.buf, row, row + 1, false)[1] or nil
144145
end

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function Handler:shortcut(info)
6767
return
6868
end
6969

70-
local line = info:line('first')
70+
local line = info:line('first', 0)
7171
if line ~= nil and line:find('[' .. info.text .. ']', 1, true) ~= nil then
7272
self:wiki_link(info)
7373
return

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.3.9'
8+
M.version = '6.3.10'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

lua/render-markdown/render/heading.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function Render:border(width)
174174
{ self.heading.above:rep(prefix), self.data.foreground },
175175
{ self.heading.above:rep(width - self.heading.left_pad - prefix), background },
176176
}
177-
if str.width(self.info:line('above')) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
177+
if str.width(self.info:line('above', 1)) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
178178
self.marks:add(true, self.info.start_row - 1, 0, {
179179
virt_text = line_above,
180180
virt_text_pos = 'overlay',
@@ -191,7 +191,7 @@ function Render:border(width)
191191
{ self.heading.below:rep(prefix), self.data.foreground },
192192
{ self.heading.below:rep(width - self.heading.left_pad - prefix), background },
193193
}
194-
if str.width(self.info:line('below')) == 0 then
194+
if str.width(self.info:line('below', 1)) == 0 then
195195
self.marks:add(true, self.info.end_row + 1, 0, {
196196
virt_text = line_below,
197197
virt_text_pos = 'overlay',

lua/render-markdown/render/section.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ function Render:setup()
3636
end
3737

3838
function Render:render()
39-
-- Do include empty line in previous section
40-
local start_offset = str.width(self.info:line('above')) == 0 and 1 or 0
39+
-- Include last empty line in previous section
40+
-- Exclude if it is the only empty line in that section
41+
local above, two_above = self.info:line('above', 1), self.info:line('above', 2)
42+
local above_is_empty = str.width(above) == 0
43+
local two_above_is_section = two_above ~= nil and vim.startswith(two_above, '#')
44+
local start_offset = (above_is_empty and not two_above_is_section) and 1 or 0
4145
local start_row = math.max(self.info.start_row - start_offset, 0)
4246

43-
-- Do not include empty line at the end of current section
44-
local end_offset = str.width(self.info:line('last')) == 0 and 1 or 0
47+
-- Exclude last empty line in current section
48+
-- Include if it is the only empty line of the last subsection
49+
local last, second_last = self.info:line('last', 0), self.info:line('last', 1)
50+
local last_is_empty = str.width(last) == 0
51+
local second_last_is_section = second_last ~= nil and vim.startswith(second_last, '#')
52+
local end_offset = (last_is_empty and not second_last_is_section) and 1 or 0
4553
local end_row = self.info.end_row - 1 - end_offset
4654

4755
-- Each level stacks inline marks so we only need to multiply based on any skipped levels

0 commit comments

Comments
 (0)