Skip to content

Commit cdb58fc

Browse files
feat: configurable starting indent level
## Details Another feature related to org-mode indents. Rather than hard coding that indents skip level 1 and increment from there allow users to set whatever value they like using indent -> skip. This tells us how many levels should be skipped before starting to indent, default value remains 1. Using a value of 0 will indent level 1 headings as well. Using a value greater than 1 will indent relative to that level instead. Only kinda tricky thing is to remember that the level change should be computed relative to the parent level or this skip value, whichever is greater. So setting a value of 2, then having an H1 & H3. The H3 should be indented only once, so the parent level is calculated as 2 instead of 1. Using negative values breaks things of course.
1 parent 015d345 commit cdb58fc

File tree

12 files changed

+62
-45
lines changed

12 files changed

+62
-45
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ require('render-markdown').setup({
512512
enabled = false,
513513
-- Amount of additional padding added for each heading level
514514
per_level = 2,
515+
-- Heading levels <= this value will not be indented
516+
-- Use 0 to begin indenting from the very first level
517+
skip = 1,
515518
},
516519
-- Window options to use that change between rendered and raw view
517520
win_options = {
@@ -936,6 +939,9 @@ require('render-markdown').setup({
936939
enabled = false,
937940
-- Amount of additional padding added for each heading level
938941
per_level = 2,
942+
-- Heading levels <= this value will not be indented
943+
-- Use 0 to begin indenting from the very first level
944+
skip = 1,
939945
},
940946
})
941947
```

Diff for: doc/render-markdown.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 12
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 13
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -545,6 +545,9 @@ Full Default Configuration ~
545545
enabled = false,
546546
-- Amount of additional padding added for each heading level
547547
per_level = 2,
548+
-- Heading levels <= this value will not be indented
549+
-- Use 0 to begin indenting from the very first level
550+
skip = 1,
548551
},
549552
-- Window options to use that change between rendered and raw view
550553
win_options = {
@@ -989,6 +992,9 @@ Wiki Page
989992
enabled = false,
990993
-- Amount of additional padding added for each heading level
991994
per_level = 2,
995+
-- Heading levels <= this value will not be indented
996+
-- Use 0 to begin indenting from the very first level
997+
skip = 1,
992998
},
993999
})
9941000
<

Diff for: lua/render-markdown/core/node_info.lua

+9-10
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,18 @@ function NodeInfo:has_error()
5252
return self.node:has_error()
5353
end
5454

55+
---@param parent boolean
5556
---@return integer
56-
function NodeInfo:parent_heading_level()
57-
local parent = self:parent('section')
58-
return parent ~= nil and parent:heading_level() or 1
59-
end
60-
61-
---@return integer
62-
function NodeInfo:heading_level()
63-
assert(self.type == 'section', 'Node must be a section')
64-
local heading = self:child('atx_heading')
57+
function NodeInfo:heading_level(parent)
58+
local info = not parent and self or self:parent('section')
59+
if info == nil then
60+
return 0
61+
end
62+
assert(info.type == 'section', 'Node must be a section')
63+
local heading = info:child('atx_heading')
6564
local node = heading ~= nil and heading.node:child(0) or nil
6665
-- Counts the number of hashtags in the heading marker
67-
return node ~= nil and #vim.treesitter.get_node_text(node, self.buf) or 1
66+
return node ~= nil and #vim.treesitter.get_node_text(node, self.buf) or 0
6867
end
6968

7069
---Walk through parent nodes, count the number of target nodes

Diff for: lua/render-markdown/health.lua

+1-1
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.8'
8+
M.version = '6.3.9'
99

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

Diff for: lua/render-markdown/init.lua

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ local M = {}
2929
---@class (exact) render.md.UserIndent
3030
---@field public enabled? boolean
3131
---@field public per_level? integer
32+
---@field public skip? integer
3233

3334
---@class (exact) render.md.UserSign
3435
---@field public enabled? boolean
@@ -576,6 +577,9 @@ M.default_config = {
576577
enabled = false,
577578
-- Amount of additional padding added for each heading level
578579
per_level = 2,
580+
-- Heading levels <= this value will not be indented
581+
-- Use 0 to begin indenting from the very first level
582+
skip = 1,
579583
},
580584
-- Window options to use that change between rendered and raw view
581585
win_options = {

Diff for: lua/render-markdown/render/base.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function Base:indent_virt_line(line)
4747
if not indent.enabled then
4848
return line
4949
end
50-
local level = self.info:parent_heading_level() - 1
50+
local level = self.info:heading_level(true) - indent.skip
5151
if level <= 0 then
5252
return line
5353
end

Diff for: lua/render-markdown/render/section.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function Render:setup()
2323
return false
2424
end
2525

26-
local current_level = self.info:heading_level()
27-
local parent_level = self.info:parent_heading_level()
26+
local current_level = self.info:heading_level(false)
27+
local parent_level = math.max(self.info:heading_level(true), self.indent.skip)
2828
self.level_change = current_level - parent_level
2929

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

Diff for: lua/render-markdown/state.lua

+1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ function M.validate()
386386
append_errors(path .. '.indent', indent, {
387387
enabled = { indent.enabled, 'boolean', nilable },
388388
per_level = { indent.per_level, 'number', nilable },
389+
skip = { indent.skip, 'number', nilable },
389390
})
390391
end
391392

Diff for: lua/render-markdown/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
---@class (exact) render.md.Indent
1919
---@field public enabled boolean
2020
---@field public per_level integer
21+
---@field public skip integer
2122

2223
---@class (exact) render.md.Sign
2324
---@field public enabled boolean

Diff for: tests/ad_hoc_spec.lua

+13-26
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,30 @@ local util = require('tests.util')
77
---@param level integer
88
---@return render.md.MarkInfo[]
99
local function setext_heading(start_row, end_row, level)
10-
local icon = level == 1 and '󰲡 ' or '󰲣 '
11-
local foreground = util.hl(string.format('H%d', level))
12-
local background = util.hl(string.format('H%dBg', level))
10+
local result = util.heading(start_row, level)
11+
local background_mark = table.remove(result, #result)
12+
local icon_mark = table.remove(result, #result)
1313

14-
---@type render.md.MarkInfo
15-
local sign_mark = {
16-
row = { start_row },
17-
col = { 0 },
18-
sign_text = '󰫎 ',
19-
sign_hl_group = util.hl('_' .. foreground .. '_' .. util.hl('Sign')),
20-
}
21-
local result = { sign_mark }
2214
for row = start_row, end_row do
23-
---@type render.md.MarkInfo
24-
local background_mark = {
25-
row = { row, row + 1 },
26-
col = { 0, 0 },
27-
hl_group = background,
28-
hl_eol = true,
29-
}
30-
table.insert(result, background_mark)
15+
local row_background_mark = vim.deepcopy(background_mark)
16+
row_background_mark.row = { row, row + 1 }
17+
table.insert(result, row_background_mark)
3118
end
32-
---@type render.md.MarkInfo
33-
local icon_mark = {
34-
row = { start_row, end_row + 1 },
35-
col = { 0, 0 },
36-
virt_text = { { icon, { foreground, background } } },
37-
virt_text_pos = 'inline',
38-
}
19+
20+
icon_mark.row = { start_row, end_row + 1 }
21+
icon_mark.col = { 0, 0 }
22+
icon_mark.virt_text[1][1] = vim.trim(icon_mark.virt_text[1][1]) .. ' '
23+
icon_mark.virt_text_pos = 'inline'
3924
table.insert(result, 3, icon_mark)
25+
4026
---@type render.md.MarkInfo
4127
local conceal_mark = {
4228
row = { end_row, end_row },
4329
col = { 0, 3 },
4430
conceal = '',
4531
}
4632
table.insert(result, #result, conceal_mark)
33+
4734
return result
4835
end
4936

Diff for: tests/indent_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121
---@return render.md.MarkInfo
2222
local function border(row, level, position)
2323
local foreground = util.hl(string.format('H%d', level))
24-
local background = util.hl('_Inverse_' .. util.hl(string.format('H%dBg', level)))
24+
local background = util.hl_inverse(string.format('H%dBg', level))
2525
local icon = position == 'above' and '' or ''
2626
local line = {
2727
{ '', background },

Diff for: tests/util.lua

+16-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function M.heading(row, level)
152152
row = { row },
153153
col = { 0 },
154154
sign_text = '󰫎 ',
155-
sign_hl_group = M.hl('_' .. foreground .. '_' .. M.hl('Sign')),
155+
sign_hl_group = M.hl_sign(foreground),
156156
}
157157
local result = { sign_mark }
158158
if row > 0 then
@@ -254,7 +254,7 @@ function M.code_language(row, col, name, win_col)
254254
row = { row },
255255
col = { col },
256256
sign_text = icon,
257-
sign_hl_group = M.hl('_' .. highlight .. '_' .. M.hl('Sign')),
257+
sign_hl_group = M.hl_sign(highlight),
258258
}
259259
---@type render.md.MarkInfo
260260
local language_mark = {
@@ -280,7 +280,7 @@ function M.code_below(row, col, width)
280280
return {
281281
row = { row },
282282
col = { col },
283-
virt_text = { { string.rep('', width), M.hl('_Inverse_' .. M.hl('Code')) } },
283+
virt_text = { { string.rep('', width), M.hl_inverse('Code') } },
284284
virt_text_pos = 'overlay',
285285
}
286286
end
@@ -399,6 +399,19 @@ function M.table_border(row, section, lengths, indent)
399399
end
400400
end
401401

402+
---@private
403+
---@param highlight string
404+
---@return string
405+
function M.hl_sign(highlight)
406+
return M.hl('_' .. highlight .. '_' .. M.hl('Sign'))
407+
end
408+
409+
---@param base string
410+
---@return string
411+
function M.hl_inverse(base)
412+
return M.hl('_Inverse_' .. M.hl(base))
413+
end
414+
402415
---@param suffix string
403416
---@return string
404417
function M.hl(suffix)

0 commit comments

Comments
 (0)