Skip to content

Commit 501e5e0

Browse files
feature: Add inline icons to images & links
## Details Request: #55 Adds `link` configuration with icons for images and hyperlinks and associated highlight group. Allow feature to be disabled. Requires neovim >= 0.10.0 for inline extmarks.
1 parent fca2903 commit 501e5e0

File tree

13 files changed

+162
-7
lines changed

13 files changed

+162
-7
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Plugin to improve viewing Markdown files in Neovim
2929
- [Callouts](https://github.com/orgs/community/discussions/16925)
3030
- Github & Obsidian out of the box, supports user defined as well
3131
- Custom checkbox states, function similar to `callouts`
32+
- Adds icon before images / links, requires neovim >= `0.10.0`
3233
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
3334
- Disable rendering when file is larger than provided value
3435
- Support custom handlers which are ran identically to builtin handlers
@@ -149,6 +150,10 @@ require('render-markdown').setup({
149150
(code_span) @code
150151
151152
(shortcut_link) @callout
153+
154+
(inline_link) @link
155+
156+
(image) @image
152157
]],
153158
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
154159
-- Only intended to be used for plugin development / debugging
@@ -287,6 +292,16 @@ require('render-markdown').setup({
287292
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'DiagnosticHint' },
288293
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
289294
},
295+
link = {
296+
-- Turn on / off inline link icon behavior
297+
enabled = true,
298+
-- Inlined with 'image' elements
299+
image = '󰥶 ',
300+
-- Inlined with 'inline_link' elements
301+
hyperlink = '󰌹 ',
302+
-- Applies to the inlined icon
303+
highlight = '@markup.link.label.markdown_inline',
304+
},
290305
-- Window options to use that change between rendered and raw view
291306
win_options = {
292307
-- See :h 'conceallevel'
@@ -422,7 +437,7 @@ require('render-markdown').setup({
422437
})
423438
```
424439

425-
## Standard Quotes
440+
## Block Quotes
426441

427442
```lua
428443
require('render-markdown').setup({
@@ -496,6 +511,23 @@ require('render-markdown').setup({
496511
})
497512
```
498513

514+
## Links
515+
516+
```lua
517+
require('render-markdown').setup({
518+
link = {
519+
-- Turn on / off inline link icon behavior
520+
enabled = true,
521+
-- Inlined with 'image' elements
522+
image = '󰥶 ',
523+
-- Inlined with 'inline_link' elements
524+
hyperlink = '󰌹 ',
525+
-- Applies to the inlined icon
526+
highlight = '@markup.link.label.markdown_inline',
527+
},
528+
})
529+
```
530+
499531
# Additional Info
500532

501533
- [Limitations](doc/limitations.md): Known limitations of this plugin

demo/heading_code.gif

12.4 KB
Loading

demo/heading_code.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
###### Heading 6
1010

11+
![Image](example.png)
12+
1113
```python {filename="demo.py"}
1214
def main() -> None:
1315
sum = 0

demo/list_table.gif

-7.64 KB
Loading

doc/render-markdown.txt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ Table of Contents *render-markdown-table-of-contents*
1616
- Dashed Line |render-markdown-setup-dashed-line|
1717
- List Bullets |render-markdown-setup-list-bullets|
1818
- Checkboxes |render-markdown-setup-checkboxes|
19-
- Standard Quotes |render-markdown-setup-standard-quotes|
19+
- Block Quotes |render-markdown-setup-block-quotes|
2020
- Tables |render-markdown-setup-tables|
2121
- Callouts |render-markdown-setup-callouts|
22+
- Links |render-markdown-setup-links|
2223
7. Additional Info |render-markdown-additional-info|
2324

2425
==============================================================================
@@ -56,6 +57,7 @@ Plugin to improve viewing Markdown files in Neovim
5657
- Callouts <https://github.com/orgs/community/discussions/16925>
5758
- Github & Obsidian out of the box, supports user defined as well
5859
- Custom checkbox states, function similar to `callouts`
60+
- Adds icon before images / links, requires neovim >= `0.10.0`
5961
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
6062
- Disable rendering when file is larger than provided value
6163
- Support custom handlers which are ran identically to builtin handlers
@@ -185,6 +187,10 @@ Full Default Configuration ~
185187
(code_span) @code
186188

187189
(shortcut_link) @callout
190+
191+
(inline_link) @link
192+
193+
(image) @image
188194
]],
189195
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
190196
-- Only intended to be used for plugin development / debugging
@@ -323,6 +329,16 @@ Full Default Configuration ~
323329
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'DiagnosticHint' },
324330
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
325331
},
332+
link = {
333+
-- Turn on / off inline link icon behavior
334+
enabled = true,
335+
-- Inlined with 'image' elements
336+
image = '󰥶 ',
337+
-- Inlined with 'inline_link' elements
338+
hyperlink = '󰌹 ',
339+
-- Applies to the inlined icon
340+
highlight = '@markup.link.label.markdown_inline',
341+
},
326342
-- Window options to use that change between rendered and raw view
327343
win_options = {
328344
-- See :h 'conceallevel'
@@ -462,7 +478,7 @@ CHECKBOXES *render-markdown-setup-checkboxes*
462478
<
463479

464480

465-
STANDARD QUOTES *render-markdown-setup-standard-quotes*
481+
BLOCK QUOTES *render-markdown-setup-block-quotes*
466482

467483
>lua
468484
require('render-markdown').setup({
@@ -539,6 +555,24 @@ CALLOUTS *render-markdown-setup-callouts*
539555
<
540556

541557

558+
LINKS *render-markdown-setup-links*
559+
560+
>lua
561+
require('render-markdown').setup({
562+
link = {
563+
-- Turn on / off inline link icon behavior
564+
enabled = true,
565+
-- Inlined with 'image' elements
566+
image = '󰥶 ',
567+
-- Inlined with 'inline_link' elements
568+
hyperlink = '󰌹 ',
569+
-- Applies to the inlined icon
570+
highlight = '@markup.link.label.markdown_inline',
571+
},
572+
})
573+
<
574+
575+
542576
==============================================================================
543577
7. Additional Info *render-markdown-additional-info*
544578

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ M.render_node = function(namespace, buf, capture, node)
6161
conceal = '',
6262
})
6363
end
64+
elseif vim.tbl_contains({ 'link', 'image' }, capture) then
65+
local link = state.config.link
66+
if not link.enabled then
67+
return
68+
end
69+
-- Requires inline extmarks
70+
if not util.has_10 then
71+
return
72+
end
73+
local icon = link.hyperlink
74+
if capture == 'image' then
75+
icon = link.image
76+
end
77+
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
78+
end_row = end_row,
79+
end_col = end_col,
80+
virt_text = { { icon, link.highlight } },
81+
virt_text_pos = 'inline',
82+
})
6483
else
6584
-- Should only get here if user provides custom capture, currently unhandled
6685
logger.error('Unhandled inline capture: ' .. capture)

lua/render-markdown/health.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function M.check_config(config)
127127
checkbox = { config.checkbox, 'table' },
128128
quote = { config.quote, 'table' },
129129
callout = { config.callout, 'table' },
130+
link = { config.link, 'table' },
130131
win_options = { config.win_options, 'table' },
131132
custom_handlers = { config.custom_handlers, 'table' },
132133
})
@@ -220,6 +221,14 @@ function M.check_config(config)
220221
})
221222
end
222223

224+
local link = config.link
225+
append_errors('link', {
226+
enabled = { link.enabled, 'boolean' },
227+
image = { link.image, 'string' },
228+
hyperlink = { link.hyperlink, 'string' },
229+
highlight = { link.highlight, 'string' },
230+
})
231+
223232
for name, win_option in pairs(config.win_options) do
224233
append_errors('win_options.' .. name, {
225234
default = { win_option.default, { 'number', 'string' } },

lua/render-markdown/init.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ local M = {}
1111
---@field public default any
1212
---@field public rendered any
1313

14+
---@class render.md.UserLink
15+
---@field public enabled? boolean
16+
---@field public image? string
17+
---@field public hyperlink? string
18+
---@field public highlight? string
19+
1420
---@class render.md.UserPipeTable
1521
---@field public style? 'full'|'normal'|'none'
1622
---@field public cell? 'overlay'|'raw'
@@ -69,6 +75,7 @@ local M = {}
6975
---@field public quote? render.md.UserBasicComponent
7076
---@field public pipe_table? render.md.UserPipeTable
7177
---@field public callout? table<string, render.md.UserCustomComponent>
78+
---@field public link? render.md.UserLink
7279
---@field public win_options? table<string, render.md.WindowOption>
7380
---@field public custom_handlers? table<string, render.md.Handler>
7481

@@ -123,6 +130,10 @@ M.default_config = {
123130
(code_span) @code
124131
125132
(shortcut_link) @callout
133+
134+
(inline_link) @link
135+
136+
(image) @image
126137
]],
127138
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
128139
-- Only intended to be used for plugin development / debugging
@@ -261,6 +272,16 @@ M.default_config = {
261272
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'DiagnosticHint' },
262273
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
263274
},
275+
link = {
276+
-- Turn on / off inline link icon behavior
277+
enabled = true,
278+
-- Inlined with 'image' elements
279+
image = '󰥶 ',
280+
-- Inlined with 'inline_link' elements
281+
hyperlink = '󰌹 ',
282+
-- Applies to the inlined icon
283+
highlight = '@markup.link.label.markdown_inline',
284+
},
264285
-- Window options to use that change between rendered and raw view
265286
win_options = {
266287
-- See :h 'conceallevel'

lua/render-markdown/types.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---@class render.md.Link
2+
---@field public enabled boolean
3+
---@field public image string
4+
---@field public hyperlink string
5+
---@field public highlight string
6+
17
---@class render.md.PipeTable
28
---@field public style 'full'|'normal'|'none'
39
---@field public cell 'overlay'|'raw'
@@ -56,5 +62,6 @@
5662
---@field public quote render.md.BasicComponent
5763
---@field public pipe_table render.md.PipeTable
5864
---@field public callout table<string, render.md.CustomComponent>
65+
---@field public link render.md.Link
5966
---@field public win_options table<string, render.md.WindowOption>
6067
---@field public custom_handlers table<string, render.md.Handler>

scripts/update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def update_readme(init_file: Path, readme_file: Path) -> None:
7070
text = readme_file.read_text().replace(old_config, new_config)
7171

7272
parameters: list[str] = ["heading", "code", "dash", "bullet"]
73-
parameters.extend(["checkbox", "quote", "pipe_table", "callout"])
73+
parameters.extend(["checkbox", "quote", "pipe_table", "callout", "link"])
7474
for parameter in parameters:
7575
old_param = get_code_block(readme_file, f"\n {parameter} = ", 2)
7676
new_param = wrap_setup(get_config_for(new_config, f"{parameter} = "))

0 commit comments

Comments
 (0)