Skip to content

Commit 6d446de

Browse files
feat: allow html tags to be replaced with icons
## Details Request: #336 By default no tags are configured so nothing happens. If matching tags are found the start & end tags get concealed and the configured icon / highlight are inlined at the start. Example configuration: ```lua require('render-markdown').setup({ html = { tag = { file = { icon = '󰨸 ', highlight = 'Normal' }, }, }, }) ```
1 parent 4a28c13 commit 6d446de

File tree

8 files changed

+78
-3
lines changed

8 files changed

+78
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@ require('render-markdown').setup({
682682
-- Highlight for the inlined text
683683
highlight = 'RenderMarkdownHtmlComment',
684684
},
685+
-- HTML tags whose start and end will be hidden and icon shown
686+
-- The key is matched against the tag name
687+
-- 'icon': Gets inlined at the start
688+
-- 'highlight': Highlight for the icon
689+
tag = {},
685690
},
686691
-- Window options to use that change between rendered and raw view
687692
win_options = {

doc/render-markdown.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 February 17
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 February 19
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -743,6 +743,11 @@ Default Configuration ~
743743
-- Highlight for the inlined text
744744
highlight = 'RenderMarkdownHtmlComment',
745745
},
746+
-- HTML tags whose start and end will be hidden and icon shown
747+
-- The key is matched against the tag name
748+
-- 'icon': Gets inlined at the start
749+
-- 'highlight': Highlight for the icon
750+
tag = {},
746751
},
747752
-- Window options to use that change between rendered and raw view
748753
win_options = {

lua/render-markdown/handler/html.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ function Handler.new(buf)
1919
self.config = state.get(buf)
2020
self.context = Context.get(buf)
2121
self.marks = List.new_marks(buf, true)
22-
self.query = treesitter.parse('html', '(comment) @comment')
22+
self.query = treesitter.parse(
23+
'html',
24+
[[
25+
(comment) @comment
26+
(element) @tag
27+
]]
28+
)
2329
self.renderers = {
2430
comment = require('render-markdown.render.html_comment'),
31+
tag = require('render-markdown.render.html_tag'),
2532
}
2633
return self
2734
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.0.4'
8+
M.version = '8.0.5'
99

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

lua/render-markdown/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ local M = {}
4141
---@field public text? string
4242
---@field public highlight? string
4343

44+
---@class (exact) render.md.HtmlTag
45+
---@field public icon string
46+
---@field public highlight string
47+
4448
---@class (exact) render.md.UserHtml: render.md.UserBaseComponent
4549
---@field public comment? render.md.UserHtmlComment
50+
---@field public tag? table<string, render.md.HtmlTag>
4651

4752
---@class (exact) render.md.UserLatex: render.md.UserBaseComponent
4853
---@field public converter? string
@@ -799,6 +804,11 @@ M.default_config = {
799804
-- Highlight for the inlined text
800805
highlight = 'RenderMarkdownHtmlComment',
801806
},
807+
-- HTML tags whose start and end will be hidden and icon shown
808+
-- The key is matched against the tag name
809+
-- 'icon': Gets inlined at the start
810+
-- 'highlight': Highlight for the icon
811+
tag = {},
802812
},
803813
-- Window options to use that change between rendered and raw view
804814
win_options = {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local Base = require('render-markdown.render.base')
2+
3+
---@class render.md.render.HtmlTag: render.md.Renderer
4+
---@field private tag render.md.HtmlTag
5+
local Render = setmetatable({}, Base)
6+
Render.__index = Render
7+
8+
---@return boolean
9+
function Render:setup()
10+
local tag = self.node:child('start_tag')
11+
if tag == nil then
12+
return false
13+
end
14+
local name = tag:child('tag_name')
15+
if name == nil then
16+
return false
17+
end
18+
self.tag = self.config.html.tag[name.text]
19+
return self.tag ~= nil
20+
end
21+
22+
function Render:render()
23+
self:hide('start_tag')
24+
self:hide('end_tag')
25+
self.marks:add(false, self.node.start_row, self.node.start_col, {
26+
virt_text = { { self.tag.icon, self.tag.highlight } },
27+
virt_text_pos = 'inline',
28+
})
29+
end
30+
31+
---@private
32+
---@param child string
33+
function Render:hide(child)
34+
local node = self.node:child(child)
35+
if node ~= nil then
36+
self.marks:add_over(true, node, {
37+
conceal = '',
38+
})
39+
end
40+
end
41+
42+
return Render

lua/render-markdown/state.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ function M.validate()
300300
:type('text', { 'string', 'nil' })
301301
:check()
302302
end)
303+
:nested('tag', function(tags)
304+
tags:nested('ALL', function(tag)
305+
tag:type({ 'icon', 'highlight' }, 'string'):check()
306+
end, false):check()
307+
end)
303308
:check()
304309
end)
305310
:nested('win_options', function(win_options)

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
---@class (exact) render.md.Html: render.md.BaseComponent
2525
---@field public comment render.md.HtmlComment
26+
---@field public tag table<string, render.md.HtmlTag>
2627

2728
---@class (exact) render.md.Latex: render.md.BaseComponent
2829
---@field public converter string

0 commit comments

Comments
 (0)