Skip to content

Commit 10126ef

Browse files
feat: custom inline highlight based on prefix
## Details Request: #544 Allows setting custom inline highlight groups to use for the associated text body based on matching a configured prefix. When a matching prefix is found it is also hidden since it adds only metadata. For example the following will apply a red background to inline highlight text that starts with an exclamation mark: ```lua vim.api.nvim_set_hl(0, 'Red', { bg = '#ff0000' }) require('render-markdown').setup({ inline_highlight = { custom = { important = { prefix = '!', highlight = 'Red' }, }, }, }) ``` No defaults are setup for this out of the box for the time being, it simply allows users to set their own values, this may change.
1 parent 93dbd55 commit 10126ef

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,11 @@ require('render-markdown').setup({
842842
render_modes = false,
843843
-- Applies to background of surrounded text.
844844
highlight = 'RenderMarkdownInlineHighlight',
845+
-- Define custom highlights based on text prefix.
846+
-- The key is for healthcheck and to allow users to change its values, value type below.
847+
-- | prefix | matched against text body, @see :h vim.startswith() |
848+
-- | highlight | highlight for text body |
849+
custom = {},
845850
},
846851
indent = {
847852
-- Mimic org-indent-mode behavior by indenting everything under a heading based on the

doc/render-markdown.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ Default Configuration ~
908908
render_modes = false,
909909
-- Applies to background of surrounded text.
910910
highlight = 'RenderMarkdownInlineHighlight',
911+
-- Define custom highlights based on text prefix.
912+
-- The key is for healthcheck and to allow users to change its values, value type below.
913+
-- | prefix | matched against text body, @see :h vim.startswith() |
914+
-- | highlight | highlight for text body |
915+
custom = {},
911916
},
912917
indent = {
913918
-- Mimic org-indent-mode behavior by indenting everything under a heading based on the

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.9.11'
9+
M.version = '8.9.12'
1010

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

lua/render-markdown/render/inline/highlight.lua

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ function Render:run()
2020
for _, range in ipairs(self.node:find('==[^=]+==')) do
2121
-- hide first 2 equal signs
2222
self:hide(range[1], range[2], range[2] + 2)
23+
-- hide last 2 equal signs
24+
self:hide(range[3], range[4] - 2, range[4])
25+
26+
local custom = self:custom(range, { 0, 2, 0, -2 })
27+
local highlight = self.config.highlight
28+
if custom then
29+
-- hide custom prefix
30+
self:hide(range[1], range[2] + 2, range[2] + 2 + #custom.prefix)
31+
highlight = custom.highlight
32+
end
33+
2334
-- highlight contents
2435
self.marks:add(self.config, false, range[1], range[2], {
2536
end_row = range[3],
2637
end_col = range[4],
27-
hl_group = self.config.highlight,
38+
hl_group = highlight,
2839
})
29-
-- hide last 2 equal signs
30-
self:hide(range[3], range[4] - 2, range[4])
3140
end
3241
end
3342

@@ -42,4 +51,26 @@ function Render:hide(row, start_col, end_col)
4251
})
4352
end
4453

54+
---@private
55+
---@param range Range4
56+
---@param offset Range4
57+
---@return render.md.inline.highlight.custom.Config?
58+
function Render:custom(range, offset)
59+
local lines = vim.api.nvim_buf_get_text(
60+
self.context.buf,
61+
range[1] + offset[1],
62+
range[2] + offset[2],
63+
range[3] + offset[3],
64+
range[4] + offset[4],
65+
{}
66+
)
67+
local text = table.concat(lines, '\n')
68+
for _, custom in pairs(self.config.custom) do
69+
if vim.startswith(text, custom.prefix) then
70+
return custom
71+
end
72+
end
73+
return nil
74+
end
75+
4576
return Render

lua/render-markdown/settings.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,11 @@ M.inline_highlight = {}
10941094

10951095
---@class (exact) render.md.inline.highlight.Config: render.md.base.Config
10961096
---@field highlight string
1097+
---@field custom table<string, render.md.inline.highlight.custom.Config>
1098+
1099+
---@class (exact) render.md.inline.highlight.custom.Config
1100+
---@field prefix string
1101+
---@field highlight string
10971102

10981103
---@type render.md.inline.highlight.Config
10991104
M.inline_highlight.default = {
@@ -1106,12 +1111,25 @@ M.inline_highlight.default = {
11061111
render_modes = false,
11071112
-- Applies to background of surrounded text.
11081113
highlight = 'RenderMarkdownInlineHighlight',
1114+
-- Define custom highlights based on text prefix.
1115+
-- The key is for healthcheck and to allow users to change its values, value type below.
1116+
-- | prefix | matched against text body, @see :h vim.startswith() |
1117+
-- | highlight | highlight for text body |
1118+
custom = {},
11091119
}
11101120

11111121
---@return render.md.Schema
11121122
function M.inline_highlight.schema()
1123+
---@type render.md.Schema
1124+
local custom = {
1125+
record = {
1126+
prefix = { type = 'string' },
1127+
highlight = { type = 'string' },
1128+
},
1129+
}
11131130
return M.base.schema({
11141131
highlight = { type = 'string' },
1132+
custom = { map = { { type = 'string' }, custom } },
11151133
})
11161134
end
11171135

lua/render-markdown/types.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@
194194

195195
---@class (exact) render.md.inline.highlight.UserConfig: render.md.base.UserConfig
196196
---@field highlight? string
197+
---@field custom? table<string, render.md.inline.highlight.custom.UserConfig>
198+
199+
---@class (exact) render.md.inline.highlight.custom.UserConfig
200+
---@field prefix? string
201+
---@field highlight? string
197202

198203
---@class (exact) render.md.latex.UserConfig: render.md.base.UserConfig
199204
---@field converter? string|string[]

0 commit comments

Comments
 (0)