Skip to content

Commit a028fbe

Browse files
feat!: allow skipping heading indents so body is offset
## Details Request: #161 There is a breaking change: - `indent.skip` -> `indent.skip_level` I added this a few hours ago so hopefully not used by many people but noting it down in either case. The feature request was to add an indent to section bodies offsetting them from the section titles. This is done, but in the other way. Instead indenting headings can be skipped using `indent.skip_heading`. This makes it easier to calculate reasonable offsets based on parent sections. It also avoids inconsistent definitions for level based indentation. For instance if you're working in an H2 then add an H4 but you still want the heading to line up with the content of the H2 it gets strange. You might be able to count nesting levels but this puts us back in a state we moved away from. By skipping headings instead all the sematics and definitions stay the same. And by simply not indenting the heading it will naturally line up with the content of the parent section.
1 parent 2ddb145 commit a028fbe

File tree

10 files changed

+51
-21
lines changed

10 files changed

+51
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- anyone using `acknowledge_conflicts` in their config should remove it
1111
- performance getting callouts and checkboxes [5513e28](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/5513e283973627385aec9758b00fc018e3a8303f)
1212
- indent based on heading level rather than nesting [27cc6ce](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/27cc6ce2605a2d42900b02648673a1de9b8cb933)
13+
- configurable starting indent level [cdb58fc](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/cdb58fc97c49a1ab75b35d99183c35b5863e845a)
1314

1415
### Bug Fixes
1516

@@ -18,6 +19,7 @@
1819
- leading spaces in checkbox bullet [#158](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/158)
1920
[06337f6](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/06337f64367ef1f1115f0a9ba41e49b84a04b1a4)
2021
- heading borders with indentation [#164](https://github.com/MeanderingProgrammer/render-markdown.nvim/pull/164)
22+
- indenting heading borders with single empty line between [2ddb145](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/2ddb145c9e60267a723083b5966189b13febc72b)
2123

2224
### Collaborator Shoutouts
2325

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ require('render-markdown').setup({
514514
per_level = 2,
515515
-- Heading levels <= this value will not be indented
516516
-- Use 0 to begin indenting from the very first level
517-
skip = 1,
517+
skip_level = 1,
518+
-- Do not indent heading titles, only the body
519+
skip_heading = false,
518520
},
519521
-- Window options to use that change between rendered and raw view
520522
win_options = {
@@ -941,7 +943,9 @@ require('render-markdown').setup({
941943
per_level = 2,
942944
-- Heading levels <= this value will not be indented
943945
-- Use 0 to begin indenting from the very first level
944-
skip = 1,
946+
skip_level = 1,
947+
-- Do not indent heading titles, only the body
948+
skip_heading = false,
945949
},
946950
})
947951
```

doc/render-markdown.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ Full Default Configuration ~
547547
per_level = 2,
548548
-- Heading levels <= this value will not be indented
549549
-- Use 0 to begin indenting from the very first level
550-
skip = 1,
550+
skip_level = 1,
551+
-- Do not indent heading titles, only the body
552+
skip_heading = false,
551553
},
552554
-- Window options to use that change between rendered and raw view
553555
win_options = {
@@ -994,7 +996,9 @@ Wiki Page
994996
per_level = 2,
995997
-- Heading levels <= this value will not be indented
996998
-- Use 0 to begin indenting from the very first level
997-
skip = 1,
999+
skip_level = 1,
1000+
-- Do not indent heading titles, only the body
1001+
skip_heading = false,
9981002
},
9991003
})
10001004
<

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

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

lua/render-markdown/init.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ local M = {}
2929
---@class (exact) render.md.UserIndent
3030
---@field public enabled? boolean
3131
---@field public per_level? integer
32-
---@field public skip? integer
32+
---@field public skip_level? integer
33+
---@field public skip_heading? boolean
3334

3435
---@class (exact) render.md.UserSign
3536
---@field public enabled? boolean
@@ -579,7 +580,9 @@ M.default_config = {
579580
per_level = 2,
580581
-- Heading levels <= this value will not be indented
581582
-- Use 0 to begin indenting from the very first level
582-
skip = 1,
583+
skip_level = 1,
584+
-- Do not indent heading titles, only the body
585+
skip_heading = false,
583586
},
584587
-- Window options to use that change between rendered and raw view
585588
win_options = {

lua/render-markdown/render/base.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ 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:heading_level(true) - indent.skip
51+
if level == nil then
52+
level = self.info:heading_level(true)
53+
elseif indent.skip_heading then
54+
local parent = self.info:parent('section')
55+
level = parent ~= nil and parent:heading_level(true) or 0
56+
end
57+
level = level - indent.skip_level
5158
if level <= 0 then
5259
return line
5360
end

lua/render-markdown/render/heading.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function Render:border(width)
181181
})
182182
else
183183
self.marks:add(false, self.info.start_row, 0, {
184-
virt_lines = { self:indent_virt_line(line_above) },
184+
virt_lines = { self:indent_virt_line(line_above, self.data.level) },
185185
virt_lines_above = true,
186186
})
187187
end
@@ -199,7 +199,7 @@ function Render:border(width)
199199
self.context.last_heading = self.info.end_row + 1
200200
else
201201
self.marks:add(false, self.info.end_row, 0, {
202-
virt_lines = { self:indent_virt_line(line_below) },
202+
virt_lines = { self:indent_virt_line(line_below, self.data.level) },
203203
})
204204
end
205205
end

lua/render-markdown/render/section.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Render:setup()
2424
end
2525

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

3030
-- Nothing to do if there is not a change in level
@@ -36,13 +36,21 @@ function Render:setup()
3636
end
3737

3838
function Render:render()
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
45-
local start_row = math.max(self.info.start_row - start_offset, 0)
39+
local start_row = nil
40+
if self.indent.skip_heading then
41+
-- Exclude any lines potentially used by section heading
42+
local second = self.info:line('first', 1)
43+
local start_offset = str.width(second) == 0 and 1 or 0
44+
start_row = self.info.start_row + 1 + start_offset
45+
else
46+
-- Include last empty line in previous section
47+
-- Exclude if it is the only empty line in that section
48+
local above, two_above = self.info:line('above', 1), self.info:line('above', 2)
49+
local above_is_empty = str.width(above) == 0
50+
local two_above_is_section = two_above ~= nil and vim.startswith(two_above, '#')
51+
local start_offset = (above_is_empty and not two_above_is_section) and 1 or 0
52+
start_row = math.max(self.info.start_row - start_offset, 0)
53+
end
4654

4755
-- Exclude last empty line in current section
4856
-- Include if it is the only empty line of the last subsection

lua/render-markdown/state.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ 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 },
389+
skip_level = { indent.skip_level, 'number', nilable },
390+
skip_heading = { indent.skip_heading, 'boolean', nilable },
390391
})
391392
end
392393

lua/render-markdown/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
---@class (exact) render.md.Indent
1919
---@field public enabled boolean
2020
---@field public per_level integer
21-
---@field public skip integer
21+
---@field public skip_level integer
22+
---@field public skip_heading boolean
2223

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

0 commit comments

Comments
 (0)