Skip to content

Commit 43bbefd

Browse files
Add support for callout syntax
# Details Resolves: #20 Replaces `shortcut_link` text that matches callout keywords with configured symbols / text and applies highlights. Unfortunately highlighting the quote bar itself is NOT supported as we need information from the inline parser to figure out what the callout is, while the quote / bar itself is handled by the standard block level markdown parser. Passing information between the parsers is not really possible, as far as I am aware. Any other solution would rely on a text search and would get tricky across multi line block quotes, which all callouts are. Will create a separate issue to make the highlighting consistent.
1 parent 49f4597 commit 43bbefd

File tree

11 files changed

+203
-31
lines changed

11 files changed

+203
-31
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Plugin to improve viewing Markdown files in Neovim
1919
- Updates table borders with better border characters, does NOT automatically align
2020
- Basic support for `LaTeX` if `pylatexenc` is installed on system
2121
- Disable rendering when file is larger than provided value
22+
- Support for [callouts](https://github.com/orgs/community/discussions/16925)
2223

2324
# Dependencies
2425

@@ -103,6 +104,8 @@ require('render-markdown').setup({
103104
-- Capture groups that get pulled from inline markdown
104105
inline_query = [[
105106
(code_span) @code
107+
108+
(shortcut_link) @callout
106109
]],
107110
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
108111
-- Only intended to be used for plugin development / debugging
@@ -126,6 +129,14 @@ require('render-markdown').setup({
126129
},
127130
-- Character that will replace the > at the start of block quotes
128131
quote = '',
132+
-- Symbol / text to use for different callouts
133+
callout = {
134+
note = ' Note',
135+
tip = ' Tip',
136+
important = '󰅾 Important',
137+
warning = ' Warning',
138+
caution = '󰳦 Caution',
139+
},
129140
-- See :h 'conceallevel' for more information about meaning of values
130141
conceal = {
131142
-- conceallevel used for buffer when not being rendered, get user setting
@@ -175,6 +186,14 @@ require('render-markdown').setup({
175186
latex = '@markup.math',
176187
-- Quote character in a block quote
177188
quote = '@markup.quote',
189+
-- Highlights to use for different callouts
190+
callout = {
191+
note = 'DiagnosticInfo',
192+
tip = 'DiagnosticOk',
193+
important = 'DiagnosticHint',
194+
warning = 'DiagnosticWarn',
195+
caution = 'DiagnosticError',
196+
},
178197
},
179198
})
180199
```

demo/demo.gif

57.8 KB
Loading

demo/record.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ def main(zoom: int, file: str, cast: str) -> None:
99
pyautogui.hotkey("`", "c")
1010
time.sleep(1.0)
1111

12-
# Zoom in
13-
for _ in range(zoom):
14-
pyautogui.hotkey("command", "=")
12+
# Zoom in / out
13+
zoom_key = "=" if zoom >= 0 else "-"
14+
for _ in range(abs(zoom)):
15+
pyautogui.hotkey("command", zoom_key)
1516

1617
# Start recording demo file
1718
# https://docs.asciinema.org/manual/cli/usage/

demo/sample.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ if __name__ == "__main__":
2020
main()
2121
```
2222

23+
> [!NOTE]
24+
> [!TIP]
25+
> [!IMPORTANT]
26+
> [!WARNING]
27+
> [!CAUTION]
28+
2329
- List Item 1: with [link](https://example.com)
2430
* List Item 2: with `inline` code
2531
* Nested List 1 Item 1

doc/render-markdown.txt

Lines changed: 20 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: 2024 May 21
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 May 22
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -40,6 +40,7 @@ Plugin to improve viewing Markdown files in Neovim
4040
- Updates table borders with better border characters, does NOT automatically align
4141
- Basic support for `LaTeX` if `pylatexenc` is installed on system
4242
- Disable rendering when file is larger than provided value
43+
- Support for callouts <https://github.com/orgs/community/discussions/16925>
4344

4445

4546
==============================================================================
@@ -132,6 +133,8 @@ modified by the user.
132133
-- Capture groups that get pulled from inline markdown
133134
inline_query = [[
134135
(code_span) @code
136+
137+
(shortcut_link) @callout
135138
]],
136139
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
137140
-- Only intended to be used for plugin development / debugging
@@ -155,6 +158,14 @@ modified by the user.
155158
},
156159
-- Character that will replace the > at the start of block quotes
157160
quote = '┃',
161+
-- Symbol / text to use for different callouts
162+
callout = {
163+
note = ' Note',
164+
tip = ' Tip',
165+
important = '󰅾 Important',
166+
warning = ' Warning',
167+
caution = '󰳦 Caution',
168+
},
158169
-- See :h 'conceallevel' for more information about meaning of values
159170
conceal = {
160171
-- conceallevel used for buffer when not being rendered, get user setting
@@ -204,6 +215,14 @@ modified by the user.
204215
latex = '@markup.math',
205216
-- Quote character in a block quote
206217
quote = '@markup.quote',
218+
-- Highlights to use for different callouts
219+
callout = {
220+
note = 'DiagnosticInfo',
221+
tip = 'DiagnosticOk',
222+
important = 'DiagnosticHint',
223+
warning = 'DiagnosticWarn',
224+
caution = 'DiagnosticError',
225+
},
207226
},
208227
})
209228
<

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
init := "tests/minimal.lua"
2-
default_zoom := "2"
2+
default_zoom := "-2"
33

44
test:
55
nvim --headless --noplugin -u {{init}} \

lua/render-markdown/handler/latex.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local logger = require('render-markdown.logger')
22
local state = require('render-markdown.state')
33

44
---@class render.md.Cache
5-
---@field expressions table<string,string[]>
5+
---@field expressions table<string, string[]>
66

77
---@type render.md.Cache
88
local cache = {

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ M.render = function(namespace, root, buf)
1010
local highlights = state.config.highlights
1111
for id, node in state.inline_query:iter_captures(root, buf) do
1212
local capture = state.inline_query.captures[id]
13+
local value = vim.treesitter.get_node_text(node, buf)
1314
local start_row, start_col, end_row, end_col = node:range()
1415
logger.debug_node(capture, node, buf)
1516

@@ -19,6 +20,24 @@ M.render = function(namespace, root, buf)
1920
end_col = end_col,
2021
hl_group = highlights.code,
2122
})
23+
elseif capture == 'callout' then
24+
local value_to_key = {
25+
['[!NOTE]'] = 'note',
26+
['[!TIP]'] = 'tip',
27+
['[!IMPORTANT]'] = 'important',
28+
['[!WARNING]'] = 'warning',
29+
['[!CAUTION]'] = 'caution',
30+
}
31+
local key = value_to_key[value]
32+
if key ~= nil then
33+
local virt_text = { state.config.callout[key], highlights.callout[key] }
34+
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
35+
end_row = end_row,
36+
end_col = end_col,
37+
virt_text = { virt_text },
38+
virt_text_pos = 'overlay',
39+
})
40+
end
2241
else
2342
-- Should only get here if user provides custom capture, currently unhandled
2443
logger.error('Unhandled inline capture: ' .. capture)

lua/render-markdown/init.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ local util = require('render-markdown.util')
44

55
local M = {}
66

7+
---@class render.md.UserCallout
8+
---@field public note? string
9+
---@field public tip? string
10+
---@field public important? string
11+
---@field public warning? string
12+
---@field public caution? string
13+
714
---@class render.md.UserTableHighlights
815
---@field public head? string
916
---@field public row? string
@@ -25,6 +32,7 @@ local M = {}
2532
---@field public table? render.md.UserTableHighlights
2633
---@field public latex? string
2734
---@field public quote? string
35+
---@field public callout? render.md.UserCallout
2836

2937
---@class render.md.UserConceal
3038
---@field public default? integer
@@ -47,6 +55,7 @@ local M = {}
4755
---@field public bullets? string[]
4856
---@field public checkbox? render.md.UserCheckbox
4957
---@field public quote? string
58+
---@field public callout? render.md.UserCallout
5059
---@field public conceal? render.md.UserConceal
5160
---@field public table_style? 'full'|'normal'|'none'
5261
---@field public highlights? render.md.UserHighlights
@@ -90,6 +99,8 @@ function M.setup(opts)
9099
]],
91100
inline_query = [[
92101
(code_span) @code
102+
103+
(shortcut_link) @callout
93104
]],
94105
log_level = 'error',
95106
file_types = { 'markdown' },
@@ -102,6 +113,13 @@ function M.setup(opts)
102113
checked = '',
103114
},
104115
quote = '',
116+
callout = {
117+
note = ' Note',
118+
tip = ' Tip',
119+
important = '󰅾 Important',
120+
warning = ' Warning',
121+
caution = '󰳦 Caution',
122+
},
105123
conceal = {
106124
default = vim.opt.conceallevel:get(),
107125
rendered = 3,
@@ -132,6 +150,13 @@ function M.setup(opts)
132150
},
133151
latex = '@markup.math',
134152
quote = '@markup.quote',
153+
callout = {
154+
note = 'DiagnosticInfo',
155+
tip = 'DiagnosticOk',
156+
important = 'DiagnosticHint',
157+
warning = 'DiagnosticWarn',
158+
caution = 'DiagnosticError',
159+
},
135160
},
136161
}
137162
state.config = vim.tbl_deep_extend('force', default_config, opts or {})

lua/render-markdown/state.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
---@class render.md.Callout
2+
---@field public note string
3+
---@field public tip string
4+
---@field public important string
5+
---@field public warning string
6+
---@field public caution string
7+
18
---@class render.md.TableHighlights
29
---@field public head string
310
---@field public row string
@@ -19,6 +26,7 @@
1926
---@field public table render.md.TableHighlights
2027
---@field public latex string
2128
---@field public quote string
29+
---@field public callout render.md.Callout
2230

2331
---@class render.md.Conceal
2432
---@field public default integer
@@ -41,6 +49,7 @@
4149
---@field public bullets string[]
4250
---@field public checkbox render.md.Checkbox
4351
---@field public quote string
52+
---@field public callout render.md.Callout
4453
---@field public conceal render.md.Conceal
4554
---@field public table_style 'full'|'normal'|'none'
4655
---@field public highlights render.md.Highlights

0 commit comments

Comments
 (0)