Skip to content

Commit 7674543

Browse files
feat: add text option to html comment concealing
## Details Request: #244 Previously support was added for html comment concealing gated by the `html.conceal_comments` option. A request to add some kind of icon next to the now concealed text was made so I'm breaking this configuration, should be very minimal impact and having an older version won't fail. Now we have an `html.comment` configuration which stores a `conceal` boolean and functions like the previous option. Additionally an optional `text` field is added along with a `highlight` to use for this text. By default the `text` field is made nil, so we don't do anything more by default than before. If a user specifies some string we inline this at the start of the now concealed comment with the `highlight` specified or the default which naturally links to `@comment`. Since this is inline text and not a conceal character users are free to make it arbitrarily long or use a single icon, all should work without a problem. Refactored HTML handler to match markdown & markdown_inline approach.
1 parent 558310a commit 7674543

File tree

10 files changed

+123
-46
lines changed

10 files changed

+123
-46
lines changed

README.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,6 @@ require('render-markdown').setup({
204204
-- Amount of empty lines below LaTeX blocks
205205
bottom_pad = 0,
206206
},
207-
html = {
208-
-- Turn on / off all HTML rendering
209-
enabled = true,
210-
-- Whether HTML comments should be concealed or not
211-
conceal_comments = true,
212-
},
213207
on = {
214208
-- Called when plugin initially attaches to a buffer
215209
attach = function() end,
@@ -580,6 +574,18 @@ require('render-markdown').setup({
580574
-- Do not indent heading titles, only the body
581575
skip_heading = false,
582576
},
577+
html = {
578+
-- Turn on / off all HTML rendering
579+
enabled = true,
580+
comment = {
581+
-- Turn on / off HTML comment concealing
582+
conceal = true,
583+
-- Optional text to inline before the concealed comment
584+
text = nil,
585+
-- Highlight for the inlined text
586+
highlight = 'RenderMarkdownHtmlComment',
587+
},
588+
},
583589
-- Window options to use that change between rendered and raw view
584590
win_options = {
585591
-- See :h 'conceallevel'
@@ -602,7 +608,7 @@ require('render-markdown').setup({
602608
-- if no override is provided. Supports the following fields:
603609
-- enabled, max_file_size, debounce, render_modes, anti_conceal, padding,
604610
-- heading, paragraph, code, dash, bullet, checkbox, quote, pipe_table,
605-
-- callout, link, sign, indent, win_options
611+
-- callout, link, sign, indent, html, win_options
606612
overrides = {
607613
-- Overrides for different buftypes, see :h 'buftype'
608614
buftype = {
@@ -1193,6 +1199,7 @@ The table below shows all the highlight groups with their default link
11931199
| RenderMarkdownDash | LineNr | Thematic break line |
11941200
| RenderMarkdownSign | SignColumn | Sign column background |
11951201
| RenderMarkdownMath | @markup.math | LaTeX lines |
1202+
| RenderMarkdownHtmlComment | @comment | HTML comment inline text |
11961203
| RenderMarkdownLink | @markup.link.label.markdown_inline | Image & hyperlink icons |
11971204
| RenderMarkdownWikiLink | RenderMarkdownLink | WikiLink icon & text |
11981205
| RenderMarkdownUnchecked | @markup.list.unchecked | Unchecked checkbox |

doc/render-markdown.txt

+16-7
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,6 @@ Default Configuration ~
251251
-- Amount of empty lines below LaTeX blocks
252252
bottom_pad = 0,
253253
},
254-
html = {
255-
-- Turn on / off all HTML rendering
256-
enabled = true,
257-
-- Whether HTML comments should be concealed or not
258-
conceal_comments = true,
259-
},
260254
on = {
261255
-- Called when plugin initially attaches to a buffer
262256
attach = function() end,
@@ -627,6 +621,18 @@ Default Configuration ~
627621
-- Do not indent heading titles, only the body
628622
skip_heading = false,
629623
},
624+
html = {
625+
-- Turn on / off all HTML rendering
626+
enabled = true,
627+
comment = {
628+
-- Turn on / off HTML comment concealing
629+
conceal = true,
630+
-- Optional text to inline before the concealed comment
631+
text = nil,
632+
-- Highlight for the inlined text
633+
highlight = 'RenderMarkdownHtmlComment',
634+
},
635+
},
630636
-- Window options to use that change between rendered and raw view
631637
win_options = {
632638
-- See :h 'conceallevel'
@@ -649,7 +655,7 @@ Default Configuration ~
649655
-- if no override is provided. Supports the following fields:
650656
-- enabled, max_file_size, debounce, render_modes, anti_conceal, padding,
651657
-- heading, paragraph, code, dash, bullet, checkbox, quote, pipe_table,
652-
-- callout, link, sign, indent, win_options
658+
-- callout, link, sign, indent, html, win_options
653659
overrides = {
654660
-- Overrides for different buftypes, see :h 'buftype'
655661
buftype = {
@@ -1242,6 +1248,9 @@ The table below shows all the highlight groups with their default link
12421248

12431249
RenderMarkdownMath @markup.math LaTeX lines
12441250

1251+
RenderMarkdownHtmlComment @comment HTML comment inline
1252+
text
1253+
12451254
RenderMarkdownLink @markup.link.label.markdown_inline Image & hyperlink
12461255
icons
12471256

lua/render-markdown/colors.lua

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ M.colors = {
3838
Dash = 'LineNr',
3939
Sign = 'SignColumn',
4040
Math = '@markup.math',
41+
HtmlComment = '@comment',
4142
-- Links
4243
Link = '@markup.link.label.markdown_inline',
4344
WikiLink = 'RenderMarkdownLink',

lua/render-markdown/handler/html.lua

+16-9
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,42 @@ local state = require('render-markdown.state')
44
local treesitter = require('render-markdown.core.treesitter')
55

66
---@class render.md.handler.buf.Html
7-
---@field private config render.md.Html
7+
---@field private config render.md.buffer.Config
88
---@field private context render.md.Context
99
---@field private marks render.md.Marks
1010
---@field private query vim.treesitter.Query
11+
---@field private renderers table<string, render.md.Renderer>
1112
local Handler = {}
1213
Handler.__index = Handler
1314

1415
---@param buf integer
1516
---@return render.md.handler.buf.Html
1617
function Handler.new(buf)
1718
local self = setmetatable({}, Handler)
18-
self.config = state.html
19+
self.config = state.get(buf)
1920
self.context = Context.get(buf)
2021
self.marks = List.new_marks(buf, true)
2122
self.query = treesitter.parse('html', '(comment) @comment')
23+
self.renderers = {
24+
comment = require('render-markdown.render.html_comment'),
25+
}
2226
return self
2327
end
2428

2529
---@param root TSNode
2630
---@return render.md.Mark[]
2731
function Handler:parse(root)
28-
if self.config.enabled and self.config.conceal_comments then
29-
self.context:query(root, self.query, function(capture, node)
30-
assert(capture == 'comment', 'Unhandled html capture: ' .. capture)
31-
self.marks:add_over(true, node, {
32-
conceal = '',
33-
})
34-
end)
32+
if not self.config.html.enabled then
33+
return {}
3534
end
35+
self.context:query(root, self.query, function(capture, node)
36+
local renderer = self.renderers[capture]
37+
assert(renderer ~= nil, 'Unhandled html capture: ' .. capture)
38+
local render = renderer:new(self.marks, self.config, self.context, node)
39+
if render:setup() then
40+
render:render()
41+
end
42+
end)
3643
return self.marks:get()
3744
end
3845

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.6.9'
7+
M.version = '7.6.10'
88

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

lua/render-markdown/init.lua

+23-12
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ local M = {}
1414
---@class (exact) render.md.UserCallback
1515
---@field public attach? fun(buf: integer)
1616

17-
---@class (exact) render.md.UserHtml
18-
---@field public enabled? boolean
19-
---@field public conceal_comments? boolean
20-
2117
---@class (exact) render.md.UserLatex
2218
---@field public enabled? boolean
2319
---@field public converter? string
@@ -35,6 +31,15 @@ local M = {}
3531
---@field public default? render.md.option.Value
3632
---@field public rendered? render.md.option.Value
3733

34+
---@class (exact) render.md.UserHtmlComment
35+
---@field public conceal? boolean
36+
---@field public text? string
37+
---@field public highlight? string
38+
39+
---@class (exact) render.md.UserHtml
40+
---@field public enabled? boolean
41+
---@field public comment? render.md.UserHtmlComment
42+
3843
---@class (exact) render.md.UserIndent
3944
---@field public enabled? boolean
4045
---@field public per_level? integer
@@ -240,6 +245,7 @@ local M = {}
240245
---@field public sign? render.md.UserSign
241246
---@field public inline_highlight? render.md.UserInlineHighlight
242247
---@field public indent? render.md.UserIndent
248+
---@field public html? render.md.UserHtml
243249
---@field public win_options? table<string, render.md.UserWindowOption>
244250

245251
---@alias render.md.config.Preset 'none'|'lazy'|'obsidian'
@@ -252,7 +258,6 @@ local M = {}
252258
---@field public file_types? string[]
253259
---@field public injections? table<string, render.md.UserInjection>
254260
---@field public latex? render.md.UserLatex
255-
---@field public html? render.md.UserHtml
256261
---@field public on? render.md.UserCallback
257262
---@field public overrides? render.md.UserConfigOverrides
258263
---@field public custom_handlers? table<string, render.md.Handler>
@@ -331,12 +336,6 @@ M.default_config = {
331336
-- Amount of empty lines below LaTeX blocks
332337
bottom_pad = 0,
333338
},
334-
html = {
335-
-- Turn on / off all HTML rendering
336-
enabled = true,
337-
-- Whether HTML comments should be concealed or not
338-
conceal_comments = true,
339-
},
340339
on = {
341340
-- Called when plugin initially attaches to a buffer
342341
attach = function() end,
@@ -707,6 +706,18 @@ M.default_config = {
707706
-- Do not indent heading titles, only the body
708707
skip_heading = false,
709708
},
709+
html = {
710+
-- Turn on / off all HTML rendering
711+
enabled = true,
712+
comment = {
713+
-- Turn on / off HTML comment concealing
714+
conceal = true,
715+
-- Optional text to inline before the concealed comment
716+
text = nil,
717+
-- Highlight for the inlined text
718+
highlight = 'RenderMarkdownHtmlComment',
719+
},
720+
},
710721
-- Window options to use that change between rendered and raw view
711722
win_options = {
712723
-- See :h 'conceallevel'
@@ -729,7 +740,7 @@ M.default_config = {
729740
-- if no override is provided. Supports the following fields:
730741
-- enabled, max_file_size, debounce, render_modes, anti_conceal, padding,
731742
-- heading, paragraph, code, dash, bullet, checkbox, quote, pipe_table,
732-
-- callout, link, sign, indent, win_options
743+
-- callout, link, sign, indent, html, win_options
733744
overrides = {
734745
-- Overrides for different buftypes, see :h 'buftype'
735746
buftype = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local Base = require('render-markdown.render.base')
2+
3+
---@class render.md.render.HtmlComment: render.md.Renderer
4+
---@field private comment render.md.HtmlComment
5+
local Render = setmetatable({}, Base)
6+
Render.__index = Render
7+
8+
---@return boolean
9+
function Render:setup()
10+
self.comment = self.config.html.comment
11+
if not self.comment.conceal then
12+
return false
13+
end
14+
return true
15+
end
16+
17+
function Render:render()
18+
self.marks:add_over(true, self.node, {
19+
conceal = '',
20+
})
21+
if self.comment.text ~= nil then
22+
self.marks:add_over(true, self.node, {
23+
virt_text = { { self.comment.text, self.comment.highlight } },
24+
virt_text_pos = 'inline',
25+
})
26+
end
27+
end
28+
29+
return Render

lua/render-markdown/state.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ local configs = {}
1313
---@field log_runtime boolean
1414
---@field file_types string[]
1515
---@field latex render.md.Latex
16-
---@field html render.md.Html
1716
---@field on render.md.Callback
1817
---@field custom_handlers table<string, render.md.Handler>
1918
local M = {}
@@ -46,7 +45,6 @@ function M.setup(default_config, user_config)
4645
M.log_runtime = config.log_runtime
4746
M.file_types = config.file_types
4847
M.latex = config.latex
49-
M.html = config.html
5048
M.on = config.on
5149
M.custom_handlers = config.custom_handlers
5250
log.setup(config.log_level)
@@ -121,6 +119,7 @@ function M.default_buffer_config()
121119
sign = config.sign,
122120
inline_highlight = config.inline_highlight,
123121
indent = config.indent,
122+
html = config.html,
124123
win_options = config.win_options,
125124
}
126125
return vim.deepcopy(buffer_config)
@@ -266,6 +265,17 @@ function M.validate()
266265
:type({ 'per_level', 'skip_level' }, 'number')
267266
:check()
268267
end)
268+
:nested('html', function(html)
269+
html:type('enabled', 'boolean')
270+
:nested('comment', function(comment)
271+
comment
272+
:type('conceal', 'boolean')
273+
:type('highlight', 'string')
274+
:type('text', { 'string', 'nil' })
275+
:check()
276+
end)
277+
:check()
278+
end)
269279
:nested('win_options', function(win_options)
270280
win_options
271281
:nested('ALL', function(win_option)
@@ -296,9 +306,6 @@ function M.validate()
296306
:type({ 'converter', 'highlight' }, 'string')
297307
:check()
298308
end)
299-
:nested('html', function(html)
300-
html:type({ 'enabled', 'conceal_comments' }, 'boolean'):check()
301-
end)
302309
:nested('on', function(on)
303310
on:type('attach', 'function'):check()
304311
end)

lua/render-markdown/types.lua

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
---@class (exact) render.md.Callback
44
---@field public attach fun(buf: integer)
55

6-
---@class (exact) render.md.Html
7-
---@field public enabled boolean
8-
---@field public conceal_comments boolean
9-
106
---@class (exact) render.md.Latex
117
---@field public enabled boolean
128
---@field public converter string
@@ -22,6 +18,15 @@
2218
---@field public default render.md.option.Value
2319
---@field public rendered render.md.option.Value
2420

21+
---@class (exact) render.md.HtmlComment
22+
---@field public conceal boolean
23+
---@field public text? string
24+
---@field public highlight string
25+
26+
---@class (exact) render.md.Html
27+
---@field public enabled boolean
28+
---@field public comment render.md.HtmlComment
29+
2530
---@class (exact) render.md.Indent
2631
---@field public enabled boolean
2732
---@field public per_level integer
@@ -194,6 +199,7 @@
194199
---@field public sign render.md.Sign
195200
---@field public inline_highlight render.md.InlineHighlight
196201
---@field public indent render.md.Indent
202+
---@field public html render.md.Html
197203
---@field public win_options table<string, render.md.WindowOption>
198204

199205
---@class (exact) render.md.Config: render.md.BufferConfig
@@ -203,7 +209,6 @@
203209
---@field public file_types string[]
204210
---@field public injections table<string, render.md.Injection>
205211
---@field public latex render.md.Latex
206-
---@field public html render.md.Html
207212
---@field public on render.md.Callback
208213
---@field public overrides render.md.ConfigOverrides
209214
---@field public custom_handlers table<string, render.md.Handler>

scripts/update.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def is_optional(self, field: str) -> bool:
3131
"UserCheckboxComponent": ["scope_highlight"],
3232
"UserCustomCallout": ["quote_icon"],
3333
"UserLinkComponent": ["highlight"],
34+
"UserHtmlComment": ["text"],
3435
}
3536

3637
# ---@field public extends? boolean -> extends

0 commit comments

Comments
 (0)