Skip to content

Commit 558310a

Browse files
feat: conceal html comments
## Details Request: #244 Use `html` parser to find and conceal comment nodes.
1 parent b6b903c commit 558310a

File tree

11 files changed

+98
-5
lines changed

11 files changed

+98
-5
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Plugin to improve viewing Markdown files in Neovim
5353
Used to parse `markdown` files
5454
- [latex](https://github.com/latex-lsp/tree-sitter-latex) (Optional):
5555
Used to get `LaTeX` blocks from `markdown` files
56+
- [html](https://github.com/tree-sitter/tree-sitter-html) (Optional):
57+
Used to conceal `HTML` comments
5658
- Icon provider plugin (Optional): Used for icon above code blocks
5759
- [mini.icons](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-icons.md)
5860
- [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons)
@@ -202,6 +204,12 @@ require('render-markdown').setup({
202204
-- Amount of empty lines below LaTeX blocks
203205
bottom_pad = 0,
204206
},
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+
},
205213
on = {
206214
-- Called when plugin initially attaches to a buffer
207215
attach = function() end,

benches/readme_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local util = require('benches.util')
44

55
describe('README.md', function()
66
it('default', function()
7-
local base_marks = 114
7+
local base_marks = 115
88
util.less_than(util.setup('README.md'), 60)
99
util.num_marks(base_marks)
1010

doc/render-markdown.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 December 03
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 December 04
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -75,6 +75,8 @@ Plugin to improve viewing Markdown files in Neovim
7575
Used to parse `markdown` files
7676
- latex <https://github.com/latex-lsp/tree-sitter-latex> (Optional):
7777
Used to get `LaTeX` blocks from `markdown` files
78+
- html <https://github.com/tree-sitter/tree-sitter-html> (Optional):
79+
Used to conceal `HTML` comments
7880
- Icon provider plugin (Optional): Used for icon above code blocks
7981
- mini.icons <https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-icons.md>
8082
- nvim-web-devicons <https://github.com/nvim-tree/nvim-web-devicons>
@@ -249,6 +251,12 @@ Default Configuration ~
249251
-- Amount of empty lines below LaTeX blocks
250252
bottom_pad = 0,
251253
},
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+
},
252260
on = {
253261
-- Called when plugin initially attaches to a buffer
254262
attach = function() end,

lua/render-markdown/core/ui.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ local util = require('render-markdown.core.util')
88

99
---@type table<string, render.md.Handler>
1010
local builtin_handlers = {
11+
html = require('render-markdown.handler.html'),
12+
latex = require('render-markdown.handler.latex'),
1113
markdown = require('render-markdown.handler.markdown'),
1214
markdown_inline = require('render-markdown.handler.markdown_inline'),
13-
latex = require('render-markdown.handler.latex'),
1415
}
1516

1617
---@class render.md.cache.Ui
@@ -69,6 +70,7 @@ function M.clear(buf, buffer_state)
6970
buffer_state:set_marks(nil)
7071
end
7172

73+
---Used directly by fzf-lua: https://github.com/ibhagwan/fzf-lua/blob/main/lua/fzf-lua/previewer/builtin.lua
7274
---@param buf integer
7375
---@param win integer
7476
---@param event string

lua/render-markdown/handler/html.lua

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local Context = require('render-markdown.core.context')
2+
local List = require('render-markdown.lib.list')
3+
local state = require('render-markdown.state')
4+
local treesitter = require('render-markdown.core.treesitter')
5+
6+
---@class render.md.handler.buf.Html
7+
---@field private config render.md.Html
8+
---@field private context render.md.Context
9+
---@field private marks render.md.Marks
10+
---@field private query vim.treesitter.Query
11+
local Handler = {}
12+
Handler.__index = Handler
13+
14+
---@param buf integer
15+
---@return render.md.handler.buf.Html
16+
function Handler.new(buf)
17+
local self = setmetatable({}, Handler)
18+
self.config = state.html
19+
self.context = Context.get(buf)
20+
self.marks = List.new_marks(buf, true)
21+
self.query = treesitter.parse('html', '(comment) @comment')
22+
return self
23+
end
24+
25+
---@param root TSNode
26+
---@return render.md.Mark[]
27+
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)
35+
end
36+
return self.marks:get()
37+
end
38+
39+
---@class render.md.handler.Html: render.md.Handler
40+
local M = {}
41+
42+
---@param root TSNode
43+
---@param buf integer
44+
---@return render.md.Mark[]
45+
function M.parse(root, buf)
46+
return Handler.new(buf):parse(root)
47+
end
48+
49+
return M

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.8'
7+
M.version = '7.6.9'
88

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

lua/render-markdown/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ 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+
1721
---@class (exact) render.md.UserLatex
1822
---@field public enabled? boolean
1923
---@field public converter? string
@@ -248,6 +252,7 @@ local M = {}
248252
---@field public file_types? string[]
249253
---@field public injections? table<string, render.md.UserInjection>
250254
---@field public latex? render.md.UserLatex
255+
---@field public html? render.md.UserHtml
251256
---@field public on? render.md.UserCallback
252257
---@field public overrides? render.md.UserConfigOverrides
253258
---@field public custom_handlers? table<string, render.md.Handler>
@@ -326,6 +331,12 @@ M.default_config = {
326331
-- Amount of empty lines below LaTeX blocks
327332
bottom_pad = 0,
328333
},
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+
},
329340
on = {
330341
-- Called when plugin initially attaches to a buffer
331342
attach = function() end,

lua/render-markdown/state.lua

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local configs = {}
1313
---@field log_runtime boolean
1414
---@field file_types string[]
1515
---@field latex render.md.Latex
16+
---@field html render.md.Html
1617
---@field on render.md.Callback
1718
---@field custom_handlers table<string, render.md.Handler>
1819
local M = {}
@@ -45,6 +46,7 @@ function M.setup(default_config, user_config)
4546
M.log_runtime = config.log_runtime
4647
M.file_types = config.file_types
4748
M.latex = config.latex
49+
M.html = config.html
4850
M.on = config.on
4951
M.custom_handlers = config.custom_handlers
5052
log.setup(config.log_level)
@@ -294,6 +296,9 @@ function M.validate()
294296
:type({ 'converter', 'highlight' }, 'string')
295297
:check()
296298
end)
299+
:nested('html', function(html)
300+
html:type({ 'enabled', 'conceal_comments' }, 'boolean'):check()
301+
end)
297302
:nested('on', function(on)
298303
on:type('attach', 'function'):check()
299304
end)

lua/render-markdown/types.lua

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
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+
610
---@class (exact) render.md.Latex
711
---@field public enabled boolean
812
---@field public converter string
@@ -199,6 +203,7 @@
199203
---@field public file_types string[]
200204
---@field public injections table<string, render.md.Injection>
201205
---@field public latex render.md.Latex
206+
---@field public html render.md.Html
202207
---@field public on render.md.Callback
203208
---@field public overrides render.md.ConfigOverrides
204209
---@field public custom_handlers table<string, render.md.Handler>

tests/ad_hoc_spec.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('ad_hoc.md', function()
101101
vim.list_extend(expected, {
102102
util.bullet(row:increment(), 0, 1),
103103
link(row:get(), 16, 25, '¹ ᴵⁿᶠᵒ', 'Link', ''),
104+
util.conceal(row:increment(2), 0, 16),
104105
link(row:increment(2), 0, 9, '¹ ᴵⁿᶠᵒ', 'Link', ''),
105106
})
106107

@@ -119,7 +120,9 @@ describe('ad_hoc.md', function()
119120
' 12 ● 󰗃 Youtube Link',
120121
' 13 ● Footnote Link ¹ ᴵⁿᶠᵒ',
121122
' 14',
122-
' 15 ¹ ᴵⁿᶠᵒ: Some Info',
123+
' 15',
124+
' 16',
125+
' 17 ¹ ᴵⁿᶠᵒ: Some Info',
123126
})
124127
end)
125128
end)

tests/data/ad_hoc.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ Heading 2 Line 2
1212
- [Youtube Link](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
1313
- Footnote Link [^1 Info]
1414

15+
<!-- comment -->
16+
1517
[^1 Info]: Some Info

0 commit comments

Comments
 (0)