Skip to content

Commit dfc1299

Browse files
feat: support function for paragraph.left_margin
## Details Request: #401 Allows `paragraph.left_margin` option to be a function that returns a number based on the context in addition to a flat number. The context provided currently is just the text value of the node, but can be expanded in the future if needed. This can be used to set a different margin based on the length of the paragraph. For example use a margin of 4 for paragraphs longer than 80 characters, otherwise use no margin: ```lua require('render-markdown').setup({ paragraph = { left_margin = function(ctx) return #ctx.text > 80 and 4 or 0 end, }, }) ```
1 parent a2c2493 commit dfc1299

File tree

9 files changed

+69
-16
lines changed

9 files changed

+69
-16
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,18 @@ require('render-markdown').setup({
387387
-- | foreground | optional override for the foreground |
388388
custom = {},
389389
},
390+
-- Useful context to have when evaluating values.
391+
-- | text | text value of the node |
390392
paragraph = {
391393
-- Turn on / off paragraph rendering.
392394
enabled = true,
393395
-- Additional modes to render paragraphs.
394396
render_modes = false,
395397
-- Amount of margin to add to the left of paragraphs.
396398
-- If a float < 1 is provided it is treated as a percentage of available window space.
399+
-- Output is evaluated depending on the type.
400+
-- | function | `value(context)` |
401+
-- | number | `value` |
397402
left_margin = 0,
398403
-- Minimum width to use for paragraphs.
399404
min_width = 0,
@@ -949,13 +954,18 @@ require('render-markdown').setup({
949954

950955
```lua
951956
require('render-markdown').setup({
957+
-- Useful context to have when evaluating values.
958+
-- | text | text value of the node |
952959
paragraph = {
953960
-- Turn on / off paragraph rendering.
954961
enabled = true,
955962
-- Additional modes to render paragraphs.
956963
render_modes = false,
957964
-- Amount of margin to add to the left of paragraphs.
958965
-- If a float < 1 is provided it is treated as a percentage of available window space.
966+
-- Output is evaluated depending on the type.
967+
-- | function | `value(context)` |
968+
-- | number | `value` |
959969
left_margin = 0,
960970
-- Minimum width to use for paragraphs.
961971
min_width = 0,

doc/render-markdown.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.11.0 Last change: 2025 April 15
1+
*render-markdown.txt* For 0.11.0 Last change: 2025 April 17
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -452,13 +452,18 @@ Default Configuration ~
452452
-- | foreground | optional override for the foreground |
453453
custom = {},
454454
},
455+
-- Useful context to have when evaluating values.
456+
-- | text | text value of the node |
455457
paragraph = {
456458
-- Turn on / off paragraph rendering.
457459
enabled = true,
458460
-- Additional modes to render paragraphs.
459461
render_modes = false,
460462
-- Amount of margin to add to the left of paragraphs.
461463
-- If a float < 1 is provided it is treated as a percentage of available window space.
464+
-- Output is evaluated depending on the type.
465+
-- | function | `value(context)` |
466+
-- | number | `value` |
462467
left_margin = 0,
463468
-- Minimum width to use for paragraphs.
464469
min_width = 0,
@@ -1010,13 +1015,18 @@ Paragraph Configuration ~
10101015

10111016
>lua
10121017
require('render-markdown').setup({
1018+
-- Useful context to have when evaluating values.
1019+
-- | text | text value of the node |
10131020
paragraph = {
10141021
-- Turn on / off paragraph rendering.
10151022
enabled = true,
10161023
-- Additional modes to render paragraphs.
10171024
render_modes = false,
10181025
-- Amount of margin to add to the left of paragraphs.
10191026
-- If a float < 1 is provided it is treated as a percentage of available window space.
1027+
-- Output is evaluated depending on the type.
1028+
-- | function | `value(context)` |
1029+
-- | number | `value` |
10201030
left_margin = 0,
10211031
-- Minimum width to use for paragraphs.
10221032
min_width = 0,

lua/render-markdown/config/paragraph.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
---@class (exact) render.md.paragraph.Config: render.md.base.Config
2-
---@field left_margin number
2+
---@field left_margin render.md.paragraph.Margin
33
---@field min_width integer
44

5+
---@class (exact) render.md.paragraph.Context
6+
---@field text string
7+
8+
---@alias render.md.paragraph.Margin
9+
---| number
10+
---| fun(ctx: render.md.paragraph.Context): number
11+
512
local M = {}
613

714
---@param spec render.md.debug.ValidatorSpec
815
function M.validate(spec)
916
require('render-markdown.config.base').validate(spec)
10-
spec:type('left_margin', 'number')
17+
spec:type('left_margin', { 'number', 'function' })
1118
spec:type('min_width', 'number')
1219
spec:check()
1320
end

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

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

lua/render-markdown/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,18 @@ M.default = {
256256
-- | foreground | optional override for the foreground |
257257
custom = {},
258258
},
259+
-- Useful context to have when evaluating values.
260+
-- | text | text value of the node |
259261
paragraph = {
260262
-- Turn on / off paragraph rendering.
261263
enabled = true,
262264
-- Additional modes to render paragraphs.
263265
render_modes = false,
264266
-- Amount of margin to add to the left of paragraphs.
265267
-- If a float < 1 is provided it is treated as a percentage of available window space.
268+
-- Output is evaluated depending on the type.
269+
-- | function | `value(context)` |
270+
-- | number | `value` |
266271
left_margin = 0,
267272
-- Minimum width to use for paragraphs.
268273
min_width = 0,

lua/render-markdown/render/paragraph.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
local Base = require('render-markdown.render.base')
22

3+
---@class render.md.paragraph.Data
4+
---@field margin number
5+
36
---@class render.md.render.Paragraph: render.md.Render
47
---@field private info render.md.paragraph.Config
8+
---@field private data render.md.paragraph.Data
59
local Render = setmetatable({}, Base)
610
Render.__index = Render
711

@@ -11,16 +15,28 @@ function Render:setup()
1115
if self.context:skip(self.info) then
1216
return false
1317
end
14-
if self.info.left_margin <= 0 then
18+
19+
local margin, left_margin = nil, self.info.left_margin
20+
if type(left_margin) == 'function' then
21+
margin = left_margin({ text = self.node.text })
22+
else
23+
margin = left_margin
24+
end
25+
26+
if margin <= 0 then
1527
return false
1628
end
29+
self.data = {
30+
margin = margin,
31+
}
32+
1733
return true
1834
end
1935

2036
function Render:render()
2137
local width = vim.fn.max(self.node:widths())
2238
width = math.max(width, self.info.min_width)
23-
local margin = self.context:percent(self.info.left_margin, width)
39+
local margin = self.context:percent(self.data.margin, width)
2440
local line = self:append({}, margin)
2541
if #line == 0 then
2642
return

lua/render-markdown/render/quote.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Render.__index = Render
1414

1515
---@return boolean
1616
function Render:setup()
17-
local quote = self.config.quote
18-
if self.context:skip(quote) then
17+
local config = self.config.quote
18+
if self.context:skip(config) then
1919
return false
2020
end
2121

@@ -31,9 +31,9 @@ function Render:setup()
3131
] @quote_marker
3232
]]
3333
),
34-
icon = callout ~= nil and callout.quote_icon or quote.icon,
35-
highlight = callout ~= nil and callout.highlight or quote.highlight,
36-
repeat_linebreak = quote.repeat_linebreak or nil,
34+
icon = callout ~= nil and callout.quote_icon or config.icon,
35+
highlight = callout ~= nil and callout.highlight or config.highlight,
36+
repeat_linebreak = config.repeat_linebreak or nil,
3737
}
3838

3939
return true

lua/render-markdown/render/section.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
local Base = require('render-markdown.render.base')
22
local Str = require('render-markdown.lib.str')
33

4+
---@class render.md.indent.Data
5+
---@field level_change integer
6+
47
---@class render.md.render.Section: render.md.Render
58
---@field private info render.md.indent.Config
6-
---@field private level_change integer
9+
---@field private data render.md.indent.Data
710
local Render = setmetatable({}, Base)
811
Render.__index = Render
912

@@ -16,10 +19,12 @@ function Render:setup()
1619

1720
local current_level = self.node:level(false)
1821
local parent_level = math.max(self.node:level(true), self.info.skip_level)
19-
self.level_change = current_level - parent_level
22+
self.data = {
23+
level_change = current_level - parent_level,
24+
}
2025

2126
-- Nothing to do if there is not a change in level
22-
if self.level_change <= 0 then
27+
if self.data.level_change <= 0 then
2328
return false
2429
end
2530

@@ -30,7 +35,7 @@ function Render:render()
3035
local start_row = self:get_start_row()
3136
local end_row = self:get_end_row()
3237
-- Each level stacks inline marks so we only need to process change in level
33-
local virt_text = self:indent_line(false, self.level_change)
38+
local virt_text = self:indent_line(false, self.data.level_change)
3439
for row = start_row, end_row do
3540
self.marks:add(false, row, 0, {
3641
priority = 0,

lua/render-markdown/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
---@field highlight? string
222222

223223
---@class (exact) render.md.paragraph.UserConfig: render.md.base.UserConfig
224-
---@field left_margin? number
224+
---@field left_margin? render.md.paragraph.Margin
225225
---@field min_width? integer
226226

227227
---@class (exact) render.md.pattern.UserConfig

0 commit comments

Comments
 (0)