Skip to content

Commit acddb80

Browse files
feat: allow html.comment.text to be a function
## Details Request: #574 The `html.comment.text` field can now be a function which takes as input a context table that includes the text of the comment. Users can use this to transform the raw input text into a more useful representation of the information.
1 parent 07d088b commit acddb80

6 files changed

Lines changed: 56 additions & 10 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,16 @@ require('render-markdown').setup({
887887
-- Additional modes to render HTML.
888888
render_modes = false,
889889
comment = {
890+
-- Useful context to have when evaluating values.
891+
-- | text | text value of the comment node |
892+
890893
-- Turn on / off HTML comment concealing.
891894
conceal = true,
892-
-- Optional text to inline before the concealed comment.
895+
-- Text to inline before the concealed comment.
896+
-- Output is evaluated depending on the type.
897+
-- | function | `value(context)` |
898+
-- | string | `value` |
899+
-- | nil | nothing |
893900
text = nil,
894901
-- Highlight for the inlined text.
895902
highlight = 'RenderMarkdownHtmlComment',

doc/render-markdown.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.5 Last change: 2025 December 16
1+
*render-markdown.txt* For NVIM v0.11.5 Last change: 2026 January 02
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -953,9 +953,16 @@ Default Configuration ~
953953
-- Additional modes to render HTML.
954954
render_modes = false,
955955
comment = {
956+
-- Useful context to have when evaluating values.
957+
-- | text | text value of the comment node |
958+
956959
-- Turn on / off HTML comment concealing.
957960
conceal = true,
958-
-- Optional text to inline before the concealed comment.
961+
-- Text to inline before the concealed comment.
962+
-- Output is evaluated depending on the type.
963+
-- | function | `value(context)` |
964+
-- | string | `value` |
965+
-- | nil | nothing |
959966
text = nil,
960967
-- Highlight for the inlined text.
961968
highlight = 'RenderMarkdownHtmlComment',

lua/render-markdown/health.lua

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

88
---@private
9-
M.version = '8.10.9'
9+
M.version = '8.10.10'
1010

1111
function M.check()
1212
M.start('versions')

lua/render-markdown/render/html/comment.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,27 @@ function Render:run()
1919
return
2020
end
2121
self.marks:over(self.config, true, self.node, { conceal = '' })
22-
if config.text then
22+
local text = Render.get_string(config.text, { text = self.node.text })
23+
if text then
2324
self.marks:start(self.config, true, self.node, {
24-
virt_text = { { config.text, config.highlight } },
25+
virt_text = { { text, config.highlight } },
2526
virt_text_pos = 'inline',
2627
})
2728
end
2829
end
2930

31+
---@private
32+
---@param value? render.md.html.comment.String
33+
---@param ctx render.md.html.comment.Context
34+
---@return string?
35+
function Render.get_string(value, ctx)
36+
if not value then
37+
return nil
38+
elseif type(value) == 'function' then
39+
return value(ctx)
40+
else
41+
return value
42+
end
43+
end
44+
3045
return Render

lua/render-markdown/settings.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,9 +958,16 @@ M.html = {}
958958

959959
---@class (exact) render.md.html.comment.Config
960960
---@field conceal boolean
961-
---@field text? string
961+
---@field text? render.md.html.comment.String
962962
---@field highlight string
963963

964+
---@class (exact) render.md.html.comment.Context
965+
---@field text string
966+
967+
---@alias render.md.html.comment.String
968+
---| string
969+
---| fun(ctx: render.md.html.comment.Context): string?
970+
964971
---@class (exact) render.md.html.Tag
965972
---@field icon? string
966973
---@field highlight? string
@@ -973,9 +980,16 @@ M.html.default = {
973980
-- Additional modes to render HTML.
974981
render_modes = false,
975982
comment = {
983+
-- Useful context to have when evaluating values.
984+
-- | text | text value of the comment node |
985+
976986
-- Turn on / off HTML comment concealing.
977987
conceal = true,
978-
-- Optional text to inline before the concealed comment.
988+
-- Text to inline before the concealed comment.
989+
-- Output is evaluated depending on the type.
990+
-- | function | `value(context)` |
991+
-- | string | `value` |
992+
-- | nil | nothing |
979993
text = nil,
980994
-- Highlight for the inlined text.
981995
highlight = 'RenderMarkdownHtmlComment',
@@ -1002,7 +1016,10 @@ function M.html.schema()
10021016
comment = {
10031017
record = {
10041018
conceal = { type = 'boolean' },
1005-
text = { optional = true, type = 'string' },
1019+
text = {
1020+
optional = true,
1021+
union = { { type = 'string' }, { type = 'function' } },
1022+
},
10061023
highlight = { type = 'string' },
10071024
},
10081025
},

lua/render-markdown/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177

178178
---@class (exact) render.md.html.comment.UserConfig
179179
---@field conceal? boolean
180-
---@field text? string
180+
---@field text? render.md.html.comment.String
181181
---@field highlight? string
182182

183183
---@class (exact) render.md.indent.UserConfig: render.md.base.UserConfig

0 commit comments

Comments
 (0)