Skip to content

Commit aad1a12

Browse files
feat: heading padding options allow arrays, opt in always virtual border
## Details Request 1: #182 This change adds support for 2 features. The first is around the padding options for headings. For each of: - `left_margin` - `left_pad` - `right_pad` - `min_width` We now accept an array of numbers in addition to a single number. When the value is an array we index it using the level and we clamp the last value. This is the same behavior we support for `width`. Request 2: #183 This request is simply to allow using virtual lines for heading borders all the time rather than trying to overlay borders on empty lines. This can now be opted in to by setting `border_virtual` to `true`.
1 parent 3adb9d5 commit aad1a12

File tree

9 files changed

+96
-36
lines changed

9 files changed

+96
-36
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
[0986638](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/0986638b381a4b01eb108bb946f3a67a9eb3d0ec)
1414
[67288fe](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/67288febca78b7aac8fae9543ef8980237e27d2a)
1515
- integrate with lazy.nvim filetypes [cb9a5e2](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/cb9a5e2412d21c7a89627e0d6da5459acbc0eb9c)
16+
- bullet left & right padding on all lines of items [#181](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/181)
17+
[3adb9d5](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/3adb9d539a016bc63fee83aa740e38fa4eeab094)
18+
1619

1720
### Bug Fixes
1821

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,23 @@ require('render-markdown').setup({
260260
-- Amount of margin to add to the left of headings
261261
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
262262
-- Margin available space is computed after accounting for padding
263+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
263264
left_margin = 0,
264265
-- Amount of padding to add to the left of headings
265266
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
267+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
266268
left_pad = 0,
267269
-- Amount of padding to add to the right of headings when width is 'block'
268270
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
271+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
269272
right_pad = 0,
270273
-- Minimum width to use for headings when width is 'block'
274+
-- Can also be an array of integers in which case the 'level' is used to index into the array using a clamp
271275
min_width = 0,
272276
-- Determins if a border is added above and below headings
273277
border = false,
278+
-- Alway use virtual lines for heading borders instead of attempting to use empty lines
279+
border_virtual = false,
274280
-- Highlight the start of the border using the foreground highlight
275281
border_prefix = false,
276282
-- Used above heading for border
@@ -622,17 +628,23 @@ require('render-markdown').setup({
622628
-- Amount of margin to add to the left of headings
623629
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
624630
-- Margin available space is computed after accounting for padding
631+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
625632
left_margin = 0,
626633
-- Amount of padding to add to the left of headings
627634
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
635+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
628636
left_pad = 0,
629637
-- Amount of padding to add to the right of headings when width is 'block'
630638
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
639+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
631640
right_pad = 0,
632641
-- Minimum width to use for headings when width is 'block'
642+
-- Can also be an array of integers in which case the 'level' is used to index into the array using a clamp
633643
min_width = 0,
634644
-- Determins if a border is added above and below headings
635645
border = false,
646+
-- Alway use virtual lines for heading borders instead of attempting to use empty lines
647+
border_virtual = false,
636648
-- Highlight the start of the border using the foreground highlight
637649
border_prefix = false,
638650
-- Used above heading for border

Diff for: doc/render-markdown.txt

+12
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,23 @@ Default Configuration ~
306306
-- Amount of margin to add to the left of headings
307307
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
308308
-- Margin available space is computed after accounting for padding
309+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
309310
left_margin = 0,
310311
-- Amount of padding to add to the left of headings
311312
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
313+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
312314
left_pad = 0,
313315
-- Amount of padding to add to the right of headings when width is 'block'
314316
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
317+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
315318
right_pad = 0,
316319
-- Minimum width to use for headings when width is 'block'
320+
-- Can also be an array of integers in which case the 'level' is used to index into the array using a clamp
317321
min_width = 0,
318322
-- Determins if a border is added above and below headings
319323
border = false,
324+
-- Alway use virtual lines for heading borders instead of attempting to use empty lines
325+
border_virtual = false,
320326
-- Highlight the start of the border using the foreground highlight
321327
border_prefix = false,
322328
-- Used above heading for border
@@ -666,17 +672,23 @@ Heading Configuration ~
666672
-- Amount of margin to add to the left of headings
667673
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
668674
-- Margin available space is computed after accounting for padding
675+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
669676
left_margin = 0,
670677
-- Amount of padding to add to the left of headings
671678
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
679+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
672680
left_pad = 0,
673681
-- Amount of padding to add to the right of headings when width is 'block'
674682
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
683+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
675684
right_pad = 0,
676685
-- Minimum width to use for headings when width is 'block'
686+
-- Can also be an array of integers in which case the 'level' is used to index into the array using a clamp
677687
min_width = 0,
678688
-- Determins if a border is added above and below headings
679689
border = false,
690+
-- Alway use virtual lines for heading borders instead of attempting to use empty lines
691+
border_virtual = false,
680692
-- Highlight the start of the border using the foreground highlight
681693
border_prefix = false,
682694
-- Used above heading for border

Diff for: lua/render-markdown/debug/validator.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,25 @@ function Spec:one_of(keys, values, input_types)
6666
end
6767

6868
---@param keys string|string[]
69+
---@param array_type type
6970
---@param input_types? type|type[]
7071
---@return render.md.debug.ValidatorSpec
71-
function Spec:string_array(keys, input_types)
72+
function Spec:array(keys, array_type, input_types)
7273
local types, suffix = self:handle_types(input_types)
7374
return self:add(keys, function(v)
7475
if vim.tbl_contains(types, type(v)) then
7576
return true
7677
elseif type(v) == 'table' then
7778
for i, item in ipairs(v) do
78-
if type(item) ~= 'string' then
79+
if type(item) ~= array_type then
7980
return false, string.format('Index %d is %s', i, type(item))
8081
end
8182
end
8283
return true
8384
else
8485
return false
8586
end
86-
end, 'string array' .. suffix)
87+
end, array_type .. ' array' .. suffix)
8788
end
8889

8990
---@param keys string|string[]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.1.10'
7+
M.version = '7.1.11'
88

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

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,12 @@ local M = {}
135135
---@field public icons? string[]
136136
---@field public signs? string[]
137137
---@field public width? render.md.heading.Width|(render.md.heading.Width)[]
138-
---@field public left_margin? number
139-
---@field public left_pad? number
140-
---@field public right_pad? number
141-
---@field public min_width? integer
138+
---@field public left_margin? number|number[]
139+
---@field public left_pad? number|number[]
140+
---@field public right_pad? number|number[]
141+
---@field public min_width? integer|integer[]
142142
---@field public border? boolean
143+
---@field public border_virtual? boolean
143144
---@field public border_prefix? boolean
144145
---@field public above? string
145146
---@field public below? string
@@ -332,17 +333,23 @@ M.default_config = {
332333
-- Amount of margin to add to the left of headings
333334
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
334335
-- Margin available space is computed after accounting for padding
336+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
335337
left_margin = 0,
336338
-- Amount of padding to add to the left of headings
337339
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
340+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
338341
left_pad = 0,
339342
-- Amount of padding to add to the right of headings when width is 'block'
340343
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
344+
-- Can also be an array of numbers in which case the 'level' is used to index into the array using a clamp
341345
right_pad = 0,
342346
-- Minimum width to use for headings when width is 'block'
347+
-- Can also be an array of integers in which case the 'level' is used to index into the array using a clamp
343348
min_width = 0,
344349
-- Determins if a border is added above and below headings
345350
border = false,
351+
-- Alway use virtual lines for heading borders instead of attempting to use empty lines
352+
border_virtual = false,
346353
-- Highlight the start of the border using the foreground highlight
347354
border_prefix = false,
348355
-- Used above heading for border

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

+39-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ local str = require('render-markdown.core.str')
1010
---@field sign? string
1111
---@field foreground string
1212
---@field background string
13-
---@field heading_width render.md.heading.Width
13+
---@field width render.md.heading.Width
14+
---@field left_margin number
15+
---@field left_pad number
16+
---@field right_pad number
17+
---@field min_width integer
1418
---@field end_row integer
1519

1620
---@class render.md.width.Heading
@@ -47,25 +51,37 @@ function Render:setup()
4751
atx, level = true, str.width(self.info.text)
4852
end
4953

50-
local heading_width = self.heading.width
51-
if type(heading_width) == 'table' then
52-
heading_width = list.clamp(heading_width, level)
53-
end
54-
5554
self.data = {
5655
atx = atx,
5756
level = level,
5857
icon = list.cycle(self.heading.icons, level),
5958
sign = list.cycle(self.heading.signs, level),
6059
foreground = list.clamp(self.heading.foregrounds, level),
6160
background = list.clamp(self.heading.backgrounds, level),
62-
heading_width = heading_width,
61+
width = Render.resolve(self.heading.width, level),
62+
left_margin = Render.resolve(self.heading.left_margin, level),
63+
left_pad = Render.resolve(self.heading.left_pad, level),
64+
right_pad = Render.resolve(self.heading.right_pad, level),
65+
min_width = Render.resolve(self.heading.min_width, level),
6366
end_row = self.info.end_row + (atx and 1 or 0),
6467
}
6568

6669
return true
6770
end
6871

72+
---@private
73+
---@generic T
74+
---@param values `T`|T[]
75+
---@param level integer
76+
---@return T
77+
function Render.resolve(values, level)
78+
if type(values) == 'table' then
79+
return list.clamp(values, level)
80+
else
81+
return values
82+
end
83+
end
84+
6985
function Render:render()
7086
local width = self:width(self:icon())
7187
if self.heading.sign then
@@ -139,22 +155,22 @@ function Render:width(icon_width)
139155
text_width = vim.fn.max(vim.tbl_map(str.width, self.info:lines()))
140156
end
141157
local width = icon_width + text_width
142-
local left_padding = self.context:resolve_offset(self.heading.left_pad, width)
143-
local right_padding = self.context:resolve_offset(self.heading.right_pad, width)
144-
width = math.max(left_padding + width + right_padding, self.heading.min_width)
158+
local left_padding = self.context:resolve_offset(self.data.left_pad, width)
159+
local right_padding = self.context:resolve_offset(self.data.right_pad, width)
160+
width = math.max(left_padding + width + right_padding, self.data.min_width)
145161
---@type render.md.width.Heading
146162
return {
147-
margin = self.context:resolve_offset(self.heading.left_margin, width),
163+
margin = self.context:resolve_offset(self.data.left_margin, width),
148164
padding = left_padding,
149-
content = self.data.heading_width == 'block' and width or self.context:get_width(),
165+
content = self.data.width == 'block' and width or self.context:get_width(),
150166
}
151167
end
152168

153169
---@private
154170
---@param width render.md.width.Heading
155171
function Render:background(width)
156172
local win_col, padding = 0, {}
157-
if self.data.heading_width == 'block' then
173+
if self.data.width == 'block' then
158174
win_col = width.margin + width.content + self:indent(self.data.level)
159175
table.insert(padding, { str.pad(vim.o.columns * 2), self.config.padding.highlight })
160176
end
@@ -185,6 +201,7 @@ function Render:border(width)
185201

186202
local foreground, background = self.data.foreground, colors.inverse_bg(self.data.background)
187203
local prefix = self.heading.border_prefix and self.data.level or 0
204+
local virtual = self.heading.border_virtual
188205

189206
---@param icon string
190207
---@return { [1]: string, [2]: string }[]
@@ -198,7 +215,7 @@ function Render:border(width)
198215
end
199216

200217
local line_above = line(self.heading.above)
201-
if str.width(self.info:line('above', 1)) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
218+
if not virtual and self:empty_line('above') and self.info.start_row - 1 ~= self.context.last_heading then
202219
self.marks:add(true, self.info.start_row - 1, 0, {
203220
virt_text = line_above,
204221
virt_text_pos = 'overlay',
@@ -211,7 +228,7 @@ function Render:border(width)
211228
end
212229

213230
local line_below = line(self.heading.below)
214-
if str.width(self.info:line('below', 1)) == 0 then
231+
if not virtual and self:empty_line('below') then
215232
self.marks:add(true, self.info.end_row + 1, 0, {
216233
virt_text = line_below,
217234
virt_text_pos = 'overlay',
@@ -224,6 +241,13 @@ function Render:border(width)
224241
end
225242
end
226243

244+
---@private
245+
---@param position 'above'|'below'
246+
---@return boolean
247+
function Render:empty_line(position)
248+
return str.width(self.info:line(position, 1)) == 0
249+
end
250+
227251
---@private
228252
---@param width render.md.width.Heading
229253
function Render:left_pad(width)

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ function M.validate()
137137

138138
validator
139139
:spec(path, config, 'heading', nilable)
140-
:type({ 'enabled', 'sign', 'border', 'border_prefix' }, 'boolean')
141-
:type({ 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number')
140+
:type({ 'enabled', 'sign', 'border', 'border_virtual', 'border_prefix' }, 'boolean')
142141
:type({ 'above', 'below' }, 'string')
143-
:string_array({ 'icons', 'signs', 'backgrounds', 'foregrounds' })
142+
:array({ 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number', 'number')
143+
:array({ 'icons', 'signs', 'backgrounds', 'foregrounds' }, 'string')
144144
:one_of('position', { 'overlay', 'inline' })
145145
:one_or_array_of('width', { 'full', 'block' })
146146
:check()
@@ -150,7 +150,7 @@ function M.validate()
150150
:type({ 'enabled', 'sign' }, 'boolean')
151151
:type({ 'language_pad', 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number')
152152
:type({ 'above', 'below', 'highlight', 'highlight_inline' }, 'string')
153-
:string_array('disable_background')
153+
:array('disable_background', 'string')
154154
:one_of('style', { 'full', 'normal', 'language', 'none' })
155155
:one_of('position', { 'left', 'right' })
156156
:one_of('width', { 'full', 'block' })
@@ -169,7 +169,7 @@ function M.validate()
169169
:type('enabled', 'boolean')
170170
:type({ 'left_pad', 'right_pad' }, 'number')
171171
:type('highlight', 'string')
172-
:string_array('icons')
172+
:array('icons', 'string')
173173
:check()
174174

175175
validator
@@ -198,7 +198,7 @@ function M.validate()
198198
:type('enabled', 'boolean')
199199
:type('min_width', 'number')
200200
:type({ 'alignment_indicator', 'head', 'row', 'filler' }, 'string')
201-
:string_array('border')
201+
:array('border', 'string')
202202
:one_of('preset', { 'none', 'round', 'double', 'heavy' })
203203
:one_of('style', { 'full', 'normal', 'none' })
204204
:one_of('cell', { 'trimmed', 'padded', 'raw', 'overlay' })
@@ -238,10 +238,10 @@ function M.validate()
238238
:type({ 'max_file_size', 'debounce' }, 'number')
239239
:type({ 'anti_conceal', 'padding', 'heading', 'code', 'dash', 'bullet', 'checkbox' }, 'table')
240240
:type({ 'quote', 'pipe_table', 'callout', 'link', 'sign', 'indent', 'win_options' }, 'table')
241-
:string_array('render_modes', 'boolean')
241+
:array('render_modes', 'string', 'boolean')
242242
:type({ 'markdown_query', 'markdown_quote_query', 'inline_query' }, 'string')
243243
:type({ 'injections', 'latex', 'overrides', 'custom_handlers' }, 'table')
244-
:string_array('file_types')
244+
:array('file_types', 'string')
245245
:one_of('preset', { 'none', 'lazy', 'obsidian' })
246246
:one_of('log_level', { 'debug', 'info', 'error' })
247247
:check()
@@ -267,7 +267,7 @@ function M.validate()
267267
:type({ 'max_file_size', 'debounce' }, 'number')
268268
:type({ 'anti_conceal', 'padding', 'heading', 'code', 'dash', 'bullet', 'checkbox' }, 'table')
269269
:type({ 'quote', 'pipe_table', 'callout', 'link', 'sign', 'indent', 'win_options' }, 'table')
270-
:string_array('render_modes', 'boolean')
270+
:array('render_modes', 'string', 'boolean')
271271
:check()
272272
validate_buffer_config(spec:get_suffix(), spec:get_input(), true)
273273
end, true)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@
110110
---@field public icons string[]
111111
---@field public signs string[]
112112
---@field public width render.md.heading.Width|(render.md.heading.Width)[]
113-
---@field public left_margin number
114-
---@field public left_pad number
115-
---@field public right_pad number
116-
---@field public min_width integer
113+
---@field public left_margin number|number[]
114+
---@field public left_pad number|number[]
115+
---@field public right_pad number|number[]
116+
---@field public min_width integer|integer[]
117117
---@field public border boolean
118+
---@field public border_virtual boolean
118119
---@field public border_prefix boolean
119120
---@field public above string
120121
---@field public below string

0 commit comments

Comments
 (0)