Skip to content

Commit 0f32655

Browse files
feature: Set custom highlight groups with default links to better support colorschemes
## Details Request: #70 The current approach of using standard highlight groups works well, however if color scheme maintainers wish to support this plugin specifically it will impact everywhere else the highlight groups are used. To improve this add a level of indirection. Create custom highlights that link to all the same defaults as currently used. That way changing the underlying links still has the same impact as well as being backwards compatible. Users setting custom values also remain unaffected. However color scheme maintainers can override the highlights used by this plugin giving them the ability to apply their theme better to this plugin specifically. I do not think this will be a very common trend, but I am more than happy to support it. Add a table to the README with all the highlghts and default links.
1 parent 3c36a25 commit 0f32655

13 files changed

+398
-226
lines changed

Diff for: README.md

+101-67
Large diffs are not rendered by default.

Diff for: demo/box_dash_quote.gif

-4.1 KB
Loading

Diff for: demo/callout.gif

-3.57 KB
Loading

Diff for: demo/heading_code.gif

-7.48 KB
Loading

Diff for: demo/latex.gif

-9.57 KB
Loading

Diff for: demo/list_table.gif

-5.86 KB
Loading

Diff for: doc/render-markdown.txt

+141-69
Large diffs are not rendered by default.

Diff for: lua/render-markdown/colors.lua

+50-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,59 @@ local cache = {
99
---@class render.md.Colors
1010
local M = {}
1111

12+
---@private
13+
---@type string
14+
M.prefix = 'RenderMarkdown'
15+
16+
-- stylua: ignore
17+
---@private
18+
---@type table<string, string>
19+
M.colors = {
20+
-- Headings
21+
H1 = '@markup.heading.1.markdown',
22+
H2 = '@markup.heading.2.markdown',
23+
H3 = '@markup.heading.3.markdown',
24+
H4 = '@markup.heading.4.markdown',
25+
H5 = '@markup.heading.5.markdown',
26+
H6 = '@markup.heading.6.markdown',
27+
H1Bg = 'DiffAdd',
28+
H2Bg = 'DiffChange',
29+
H3Bg = 'DiffDelete',
30+
-- General
31+
Code = 'ColorColumn',
32+
Bullet = 'Normal',
33+
Quote = '@markup.quote',
34+
Dash = 'LineNr',
35+
Link = '@markup.link.label.markdown_inline',
36+
Sign = 'SignColumn',
37+
Math = '@markup.math',
38+
-- Checkboxes
39+
Unchecked = '@markup.list.unchecked',
40+
Checked = '@markup.list.checked',
41+
Todo = '@markup.raw',
42+
-- Pipe tables
43+
TableHead = '@markup.heading',
44+
TableRow = 'Normal',
45+
TableFill = 'Conceal',
46+
-- Callouts
47+
Success = 'DiagnosticOk',
48+
Info = 'DiagnosticInfo',
49+
Hint = 'DiagnosticHint',
50+
Warn = 'DiagnosticWarn',
51+
Error = 'DiagnosticError',
52+
}
53+
54+
function M.setup()
55+
for name, link in pairs(M.colors) do
56+
vim.api.nvim_set_hl(0, M.prefix .. name, { link = link, default = true })
57+
end
58+
end
59+
1260
---@param foreground string
1361
---@param background string
1462
---@return string
1563
M.combine = function(foreground, background)
16-
local name = string.format('RenderMd_%s_%s', foreground, background)
64+
local name = string.format('%s_%s_%s', M.prefix, foreground, background)
1765
if not vim.tbl_contains(cache.highlights, name) then
1866
local fg = M.get_hl(foreground).fg
1967
local bg = M.get_hl(background).bg
@@ -26,7 +74,7 @@ end
2674
---@param highlight string
2775
---@return string
2876
M.inverse = function(highlight)
29-
local name = string.format('RenderMd_Inverse_%s', highlight)
77+
local name = string.format('%s_Inverse_%s', M.prefix, highlight)
3078
if not vim.tbl_contains(cache.highlights, name) then
3179
local hl = M.get_hl(highlight)
3280
vim.api.nvim_set_hl(0, name, { fg = hl.bg, bg = hl.fg })

Diff for: lua/render-markdown/init.lua

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local colors = require('render-markdown.colors')
12
local manager = require('render-markdown.manager')
23
local state = require('render-markdown.state')
34

@@ -193,7 +194,7 @@ M.default_config = {
193194
-- Executable used to convert latex formula to rendered unicode
194195
converter = 'latex2text',
195196
-- Highlight for LaTeX blocks
196-
highlight = '@markup.math',
197+
highlight = 'RenderMarkdownMath',
197198
},
198199
heading = {
199200
-- Turn on / off heading icon & background rendering
@@ -210,16 +211,16 @@ M.default_config = {
210211
signs = { '󰫎 ' },
211212
-- The 'level' is used to index into the array using a clamp
212213
-- Highlight for the heading icon and extends through the entire line
213-
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
214+
backgrounds = { 'RenderMarkdownH1Bg', 'RenderMarkdownH2Bg', 'RenderMarkdownH3Bg' },
214215
-- The 'level' is used to index into the array using a clamp
215216
-- Highlight for the heading and sign icons
216217
foregrounds = {
217-
'@markup.heading.1.markdown',
218-
'@markup.heading.2.markdown',
219-
'@markup.heading.3.markdown',
220-
'@markup.heading.4.markdown',
221-
'@markup.heading.5.markdown',
222-
'@markup.heading.6.markdown',
218+
'RenderMarkdownH1',
219+
'RenderMarkdownH2',
220+
'RenderMarkdownH3',
221+
'RenderMarkdownH4',
222+
'RenderMarkdownH5',
223+
'RenderMarkdownH6',
223224
},
224225
},
225226
code = {
@@ -244,7 +245,7 @@ M.default_config = {
244245
-- Used below code blocks for thin border
245246
below = '',
246247
-- Highlight for code blocks & inline code
247-
highlight = 'ColorColumn',
248+
highlight = 'RenderMarkdownCode',
248249
},
249250
dash = {
250251
-- Turn on / off thematic break rendering
@@ -253,7 +254,7 @@ M.default_config = {
253254
-- The icon gets repeated across the window's width
254255
icon = '',
255256
-- Highlight for the whole line generated from the icon
256-
highlight = 'LineNr',
257+
highlight = 'RenderMarkdownDash',
257258
},
258259
bullet = {
259260
-- Turn on / off list bullet rendering
@@ -264,7 +265,7 @@ M.default_config = {
264265
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
265266
icons = { '', '', '', '' },
266267
-- Highlight for the bullet icon
267-
highlight = 'Normal',
268+
highlight = 'RenderMarkdownBullet',
268269
},
269270
-- Checkboxes are a special instance of a 'list_item' that start with a 'shortcut_link'
270271
-- There are two special states for unchecked & checked defined in the markdown grammar
@@ -275,13 +276,13 @@ M.default_config = {
275276
-- Replaces '[ ]' of 'task_list_marker_unchecked'
276277
icon = '󰄱 ',
277278
-- Highlight for the unchecked icon
278-
highlight = '@markup.list.unchecked',
279+
highlight = 'RenderMarkdownUnchecked',
279280
},
280281
checked = {
281282
-- Replaces '[x]' of 'task_list_marker_checked'
282283
icon = '󰱒 ',
283284
-- Highligh for the checked icon
284-
highlight = '@markup.heading',
285+
highlight = 'RenderMarkdownChecked',
285286
},
286287
-- Define custom checkbox states, more involved as they are not part of the markdown grammar
287288
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
@@ -291,7 +292,7 @@ M.default_config = {
291292
-- 'rendered': Replaces the 'raw' value when rendering
292293
-- 'highlight': Highlight for the 'rendered' icon
293294
custom = {
294-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = '@markup.raw' },
295+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
295296
},
296297
},
297298
quote = {
@@ -300,7 +301,7 @@ M.default_config = {
300301
-- Replaces '>' of 'block_quote'
301302
icon = '',
302303
-- Highlight for the quote icon
303-
highlight = '@markup.quote',
304+
highlight = 'RenderMarkdownQuote',
304305
},
305306
pipe_table = {
306307
-- Turn on / off pipe table rendering
@@ -325,11 +326,11 @@ M.default_config = {
325326
'', '',
326327
},
327328
-- Highlight for table heading, delimiter, and the line above
328-
head = '@markup.heading',
329+
head = 'RenderMarkdownTableHead',
329330
-- Highlight for everything else, main table rows and the line below
330-
row = 'Normal',
331+
row = 'RenderMarkdownTableRow',
331332
-- Highlight for inline padding used to add back concealed space
332-
filler = 'Conceal',
333+
filler = 'RenderMarkdownTableFill',
333334
},
334335
-- Callouts are a special instance of a 'block_quote' that start with a 'shortcut_link'
335336
-- Can specify as many additional values as you like following the pattern from any below, such as 'note'
@@ -338,21 +339,21 @@ M.default_config = {
338339
-- 'rendered': Replaces the 'raw' value when rendering
339340
-- 'highlight': Highlight for the 'rendered' text and quote markers
340341
callout = {
341-
note = { raw = '[!NOTE]', rendered = '󰋽 Note', highlight = 'DiagnosticInfo' },
342-
tip = { raw = '[!TIP]', rendered = '󰌶 Tip', highlight = 'DiagnosticOk' },
343-
important = { raw = '[!IMPORTANT]', rendered = '󰅾 Important', highlight = 'DiagnosticHint' },
344-
warning = { raw = '[!WARNING]', rendered = '󰀪 Warning', highlight = 'DiagnosticWarn' },
345-
caution = { raw = '[!CAUTION]', rendered = '󰳦 Caution', highlight = 'DiagnosticError' },
342+
note = { raw = '[!NOTE]', rendered = '󰋽 Note', highlight = 'RenderMarkdownInfo' },
343+
tip = { raw = '[!TIP]', rendered = '󰌶 Tip', highlight = 'RenderMarkdownSuccess' },
344+
important = { raw = '[!IMPORTANT]', rendered = '󰅾 Important', highlight = 'RenderMarkdownHint' },
345+
warning = { raw = '[!WARNING]', rendered = '󰀪 Warning', highlight = 'RenderMarkdownWarn' },
346+
caution = { raw = '[!CAUTION]', rendered = '󰳦 Caution', highlight = 'RenderMarkdownError' },
346347
-- Obsidian: https://help.a.md/Editing+and+formatting/Callouts
347-
abstract = { raw = '[!ABSTRACT]', rendered = '󰨸 Abstract', highlight = 'DiagnosticInfo' },
348-
todo = { raw = '[!TODO]', rendered = '󰗡 Todo', highlight = 'DiagnosticInfo' },
349-
success = { raw = '[!SUCCESS]', rendered = '󰄬 Success', highlight = 'DiagnosticOk' },
350-
question = { raw = '[!QUESTION]', rendered = '󰘥 Question', highlight = 'DiagnosticWarn' },
351-
failure = { raw = '[!FAILURE]', rendered = '󰅖 Failure', highlight = 'DiagnosticError' },
352-
danger = { raw = '[!DANGER]', rendered = '󱐌 Danger', highlight = 'DiagnosticError' },
353-
bug = { raw = '[!BUG]', rendered = '󰨰 Bug', highlight = 'DiagnosticError' },
354-
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'DiagnosticHint' },
355-
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
348+
abstract = { raw = '[!ABSTRACT]', rendered = '󰨸 Abstract', highlight = 'RenderMarkdownInfo' },
349+
todo = { raw = '[!TODO]', rendered = '󰗡 Todo', highlight = 'RenderMarkdownInfo' },
350+
success = { raw = '[!SUCCESS]', rendered = '󰄬 Success', highlight = 'RenderMarkdownSuccess' },
351+
question = { raw = '[!QUESTION]', rendered = '󰘥 Question', highlight = 'RenderMarkdownWarn' },
352+
failure = { raw = '[!FAILURE]', rendered = '󰅖 Failure', highlight = 'RenderMarkdownError' },
353+
danger = { raw = '[!DANGER]', rendered = '󱐌 Danger', highlight = 'RenderMarkdownError' },
354+
bug = { raw = '[!BUG]', rendered = '󰨰 Bug', highlight = 'RenderMarkdownError' },
355+
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'RenderMarkdownHint' },
356+
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = 'RenderMarkdownQuote' },
356357
},
357358
link = {
358359
-- Turn on / off inline link icon rendering
@@ -362,7 +363,7 @@ M.default_config = {
362363
-- Inlined with 'inline_link' elements
363364
hyperlink = '󰌹 ',
364365
-- Applies to the inlined icon
365-
highlight = '@markup.link.label.markdown_inline',
366+
highlight = 'RenderMarkdownLink',
366367
},
367368
sign = {
368369
-- Turn on / off sign rendering
@@ -372,7 +373,7 @@ M.default_config = {
372373
buftypes = { 'nofile' },
373374
},
374375
-- Applies to background of sign text
375-
highlight = 'SignColumn',
376+
highlight = 'RenderMarkdownSign',
376377
},
377378
-- Window options to use that change between rendered and raw view
378379
win_options = {
@@ -407,6 +408,7 @@ function M.setup(opts)
407408
state.inline_link_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_link_query)
408409
end)
409410

411+
colors.setup()
410412
manager.setup()
411413

412414
vim.api.nvim_create_user_command('RenderMarkdown', M.command, {

Diff for: tests/box_dash_quote_spec.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ async_tests.describe('box_dash_quote.md', function()
1111
vim.list_extend(expected, util.heading(0, 1))
1212

1313
-- Checkboxes
14-
vim.list_extend(expected, util.checkbox(2, ' 󰄱 ', '@markup.list.unchecked', false))
15-
vim.list_extend(expected, util.checkbox(3, ' 󰱒 ', '@markup.heading', false))
16-
vim.list_extend(expected, util.checkbox(4, ' 󰥔 ', '@markup.raw', true))
14+
vim.list_extend(expected, util.checkbox(2, ' 󰄱 ', 'RenderMarkdownUnchecked', false))
15+
vim.list_extend(expected, util.checkbox(3, ' 󰱒 ', 'RenderMarkdownChecked', false))
16+
vim.list_extend(expected, util.checkbox(4, ' 󰥔 ', 'RenderMarkdownTodo', true))
1717

1818
-- Line break
1919
vim.list_extend(expected, {
2020
{
2121
row = { 6 },
2222
col = { 0 },
23-
virt_text = { { string.rep('', vim.opt.columns:get()), 'LineNr' } },
23+
virt_text = { { string.rep('', vim.opt.columns:get()), 'RenderMarkdownDash' } },
2424
virt_text_pos = 'overlay',
2525
},
2626
})
2727

2828
-- Quote lines
2929
vim.list_extend(expected, {
30-
util.quote(8, ' %s ', '@markup.quote'),
31-
util.quote(9, ' %s ', '@markup.quote'),
30+
util.quote(8, ' %s ', 'Quote'),
31+
util.quote(9, ' %s ', 'Quote'),
3232
})
3333

3434
local actual = util.get_actual_marks()

Diff for: tests/callout_spec.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local function callout(row, start_col, end_col, text, highlight)
1111
return {
1212
row = { row, row },
1313
col = { start_col, end_col },
14-
virt_text = { { text, highlight } },
14+
virt_text = { { text, 'RenderMarkdown' .. highlight } },
1515
virt_text_pos = 'overlay',
1616
}
1717
end
@@ -20,11 +20,11 @@ async_tests.describe('callout.md', function()
2020
async_tests.it('default', function()
2121
util.setup('demo/callout.md')
2222

23-
local info = 'DiagnosticInfo'
24-
local ok = 'DiagnosticOk'
25-
local hint = 'DiagnosticHint'
26-
local warn = 'DiagnosticWarn'
27-
local error = 'DiagnosticError'
23+
local info = 'Info'
24+
local ok = 'Success'
25+
local hint = 'Hint'
26+
local warn = 'Warn'
27+
local error = 'Error'
2828

2929
local expected = {}
3030

Diff for: tests/latex_spec.lua

+20-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ local util = require('tests.util')
44

55
local eq = assert.are.same
66

7+
---@param start_row integer
8+
---@param end_row integer
9+
---@param start_col integer
10+
---@param end_col integer
11+
---@param lines string[]
12+
---@return render.md.MarkInfo
13+
local function latex(start_row, end_row, start_col, end_col, lines)
14+
local virt_lines = vim.tbl_map(function(line)
15+
return { { line, 'RenderMarkdownMath' } }
16+
end, lines)
17+
return {
18+
row = { start_row, end_row },
19+
col = { start_col, end_col },
20+
virt_lines = virt_lines,
21+
virt_lines_above = true,
22+
}
23+
end
24+
725
async_tests.describe('latex.md', function()
826
async_tests.it('default', function()
927
stub.new(vim.fn, 'executable', function(expr)
@@ -23,27 +41,12 @@ async_tests.describe('latex.md', function()
2341

2442
local expected = {}
2543

26-
-- Heading
2744
vim.list_extend(expected, util.heading(0, 1))
28-
2945
vim.list_extend(expected, {
3046
-- Inline
31-
{
32-
row = { 2, 2 },
33-
col = { 0, 21 },
34-
virt_lines = { { { '√(3x-1)+(1+x)^2', '@markup.math' } } },
35-
virt_lines_above = true,
36-
},
47+
latex(2, 2, 0, 21, { '√(3x-1)+(1+x)^2' }),
3748
-- Block
38-
{
39-
row = { 4, 7 },
40-
col = { 0, 2 },
41-
virt_lines = {
42-
{ { 'f(x,y) = x + √(y)', '@markup.math' } },
43-
{ { 'f(x,y) = √(y) + x^2/4y', '@markup.math' } },
44-
},
45-
virt_lines_above = true,
46-
},
49+
latex(4, 7, 0, 2, { 'f(x,y) = x + √(y)', 'f(x,y) = √(y) + x^2/4y' }),
4750
})
4851

4952
local actual = util.get_actual_marks()

0 commit comments

Comments
 (0)