Skip to content

Commit cfe5746

Browse files
feat: fill missing levels in heading sections calculation with 0
## Details Previously when calculating sections for the following input: ``` ## Notice # Section A ### Note # Section B ``` The results would be: ``` ## Notice -> { 1 } # Section A -> { 2 } ### Note -> { 2, 1 } # Section B -> { 3 } ``` This change updates the calculation to always produce a number of sections equal to the current heading level and to compute the nesting level based off of sibling sections at the same level only. Any levels that are skipped are filled in with 0s. So now the results would be: ``` ## Notice -> { 0, 1 } # Section A -> { 1 } ### Note -> { 1, 0, 1 } # Section B -> { 2 } ```
1 parent 873bdee commit cfe5746

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '7.8.4'
8+
M.version = '7.8.5'
99

1010
function M.check()
1111
M.start('version')

lua/render-markdown/lib/node.lua

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ function Node.new(buf, node)
2727
return self
2828
end
2929

30+
---@private
31+
---@param node TSNode
32+
---@return render.md.Node
33+
function Node:create(node)
34+
return Node.new(self.buf, node)
35+
end
36+
3037
---@param a render.md.Node
3138
---@param b render.md.Node
3239
---@return boolean
@@ -50,18 +57,23 @@ end
5057

5158
---@return integer[]
5259
function Node:sections()
53-
local result, section = {}, self:parent('section')
60+
local result, levels, section = {}, 0, self:parent('section')
5461
while section ~= nil do
55-
local count = section:sibling_count('section')
56-
table.insert(result, 1, count)
62+
local level = section:level(false)
63+
result[level] = section:sibling_count('section', level)
64+
levels = math.max(levels, level)
5765
section = section:parent('section')
5866
end
67+
-- Fill in any heading level gaps with 0
68+
for i = 1, levels do
69+
result[i] = result[i] or 0
70+
end
5971
return result
6072
end
6173

6274
---@param parent boolean
6375
---@return integer
64-
function Node:heading_level(parent)
76+
function Node:level(parent)
6577
local section = not parent and self or self:parent('section')
6678
if section == nil then
6779
return 0
@@ -84,7 +96,7 @@ function Node:level_in_section(target)
8496
end
8597
parent = parent:parent()
8698
end
87-
return level, root ~= nil and Node.new(self.buf, root) or nil
99+
return level, root ~= nil and self:create(root) or nil
88100
end
89101

90102
---@param target string
@@ -93,7 +105,7 @@ function Node:parent(target)
93105
local parent = self.node:parent()
94106
while parent ~= nil do
95107
if parent:type() == target then
96-
return Node.new(self.buf, parent)
108+
return self:create(parent)
97109
end
98110
parent = parent:parent()
99111
end
@@ -106,18 +118,23 @@ function Node:sibling(target)
106118
local sibling = self.node:next_sibling()
107119
while sibling ~= nil do
108120
if sibling:type() == target then
109-
return Node.new(self.buf, sibling)
121+
return self:create(sibling)
110122
end
111123
sibling = sibling:next_sibling()
112124
end
113125
return nil
114126
end
115127

116128
---@param target string
129+
---@param level? integer
117130
---@return integer
118-
function Node:sibling_count(target)
131+
function Node:sibling_count(target, level)
119132
local count, sibling = 1, self.node:prev_sibling()
120-
while sibling ~= nil and sibling:type() == target do
133+
while
134+
sibling ~= nil
135+
and sibling:type() == target
136+
and (level == nil or self:create(sibling):level(false) == level)
137+
do
121138
count = count + 1
122139
sibling = sibling:prev_sibling()
123140
end
@@ -128,7 +145,7 @@ end
128145
---@return render.md.Node?
129146
function Node:child_at(index)
130147
local node = self.node:named_child(index)
131-
return node ~= nil and Node.new(self.buf, node) or nil
148+
return node ~= nil and self:create(node) or nil
132149
end
133150

134151
---@param target_type string
@@ -138,7 +155,7 @@ function Node:child(target_type, target_row)
138155
for child in self.node:iter_children() do
139156
if child:type() == target_type then
140157
if target_row == nil or child:range() == target_row then
141-
return Node.new(self.buf, child)
158+
return self:create(child)
142159
end
143160
end
144161
end
@@ -148,7 +165,7 @@ end
148165
---@param callback fun(node: render.md.Node)
149166
function Node:for_each_child(callback)
150167
for child in self.node:iter_children() do
151-
callback(Node.new(self.buf, child))
168+
callback(self:create(child))
152169
end
153170
end
154171

lua/render-markdown/render/base.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ function Base:indent(level)
9797
return 0
9898
end
9999
if level == nil then
100-
level = self.node:heading_level(true)
100+
level = self.node:level(true)
101101
elseif indent.skip_heading then
102102
local parent = self.node:parent('section')
103-
level = parent ~= nil and parent:heading_level(true) or 0
103+
level = parent ~= nil and parent:level(true) or 0
104104
end
105105
level = level - indent.skip_level
106106
if level <= 0 then

lua/render-markdown/render/section.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function Render:setup()
1414
return false
1515
end
1616

17-
local current_level = self.node:heading_level(false)
18-
local parent_level = math.max(self.node:heading_level(true), self.indent.skip_level)
17+
local current_level = self.node:level(false)
18+
local parent_level = math.max(self.node:level(true), self.indent.skip_level)
1919
self.level_change = current_level - parent_level
2020

2121
-- Nothing to do if there is not a change in level

0 commit comments

Comments
 (0)