Skip to content

Commit 61850bf

Browse files
feat: expand default custom link icons
## Details Request: #245 Adds support for several URLs out of the box in addition to default web: - youtube.com - github.com - neovim.io - stackoverflow.com - discord.com - reddit.com 2 other changes have been made to make configuring custom links easier: - The highlight gets pulled from the top level `link.highlight` option if a link specific one is not specified. This makes it so you don't need to copy `RenderMarkownLink` repeatedly while being backwards compatible and allowing users to override behavior. - When multiple patterns match a link we break ties by picking the longer pattern, idea being more specific pattern = longer pattern. However patterns can be long for other reason like the original default web pattern `^http[s]?://`, to make overriding this value more likely the pattern has been shortened to `^http`.
1 parent f3cda24 commit 61850bf

File tree

10 files changed

+69
-37
lines changed

10 files changed

+69
-37
lines changed

Diff for: README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ require('render-markdown').setup({
523523
email = '󰀓 ',
524524
-- Fallback icon for 'inline_link' elements
525525
hyperlink = '󰌹 ',
526-
-- Applies to the fallback inlined icon
526+
-- Applies to the inlined icon as a fallback
527527
highlight = 'RenderMarkdownLink',
528528
-- Applies to WikiLink elements
529529
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
@@ -533,9 +533,15 @@ require('render-markdown').setup({
533533
-- The key in this case 'web' is for healthcheck and to allow users to change its values
534534
-- 'pattern': Matched against the destination text see :h lua-pattern
535535
-- 'icon': Gets inlined before the link text
536-
-- 'highlight': Highlight for the 'icon'
536+
-- 'highlight': Optional highlight for the 'icon', uses fallback highlight if not provided
537537
custom = {
538-
web = { pattern = '^http[s]?://', icon = '󰖟 ', highlight = 'RenderMarkdownLink' },
538+
web = { pattern = '^http', icon = '󰖟 ' },
539+
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
540+
github = { pattern = 'github%.com', icon = '󰊤 ' },
541+
neovim = { pattern = 'neovim%.io', icon = '' },
542+
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
543+
discord = { pattern = 'discord%.com', icon = '󰙯 ' },
544+
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
539545
},
540546
},
541547
sign = {
@@ -1076,7 +1082,7 @@ require('render-markdown').setup({
10761082
email = '󰀓 ',
10771083
-- Fallback icon for 'inline_link' elements
10781084
hyperlink = '󰌹 ',
1079-
-- Applies to the fallback inlined icon
1085+
-- Applies to the inlined icon as a fallback
10801086
highlight = 'RenderMarkdownLink',
10811087
-- Applies to WikiLink elements
10821088
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
@@ -1086,9 +1092,15 @@ require('render-markdown').setup({
10861092
-- The key in this case 'web' is for healthcheck and to allow users to change its values
10871093
-- 'pattern': Matched against the destination text see :h lua-pattern
10881094
-- 'icon': Gets inlined before the link text
1089-
-- 'highlight': Highlight for the 'icon'
1095+
-- 'highlight': Optional highlight for the 'icon', uses fallback highlight if not provided
10901096
custom = {
1091-
web = { pattern = '^http[s]?://', icon = '󰖟 ', highlight = 'RenderMarkdownLink' },
1097+
web = { pattern = '^http', icon = '󰖟 ' },
1098+
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
1099+
github = { pattern = 'github%.com', icon = '󰊤 ' },
1100+
neovim = { pattern = 'neovim%.io', icon = '' },
1101+
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
1102+
discord = { pattern = 'discord%.com', icon = '󰙯 ' },
1103+
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
10921104
},
10931105
},
10941106
})

Diff for: doc/render-markdown.txt

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 December 01
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 December 02
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -570,7 +570,7 @@ Default Configuration ~
570570
email = '󰀓 ',
571571
-- Fallback icon for 'inline_link' elements
572572
hyperlink = '󰌹 ',
573-
-- Applies to the fallback inlined icon
573+
-- Applies to the inlined icon as a fallback
574574
highlight = 'RenderMarkdownLink',
575575
-- Applies to WikiLink elements
576576
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
@@ -580,9 +580,15 @@ Default Configuration ~
580580
-- The key in this case 'web' is for healthcheck and to allow users to change its values
581581
-- 'pattern': Matched against the destination text see :h lua-pattern
582582
-- 'icon': Gets inlined before the link text
583-
-- 'highlight': Highlight for the 'icon'
583+
-- 'highlight': Optional highlight for the 'icon', uses fallback highlight if not provided
584584
custom = {
585-
web = { pattern = '^http[s]?://', icon = '󰖟 ', highlight = 'RenderMarkdownLink' },
585+
web = { pattern = '^http', icon = '󰖟 ' },
586+
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
587+
github = { pattern = 'github%.com', icon = '󰊤 ' },
588+
neovim = { pattern = 'neovim%.io', icon = ' ' },
589+
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
590+
discord = { pattern = 'discord%.com', icon = '󰙯 ' },
591+
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
586592
},
587593
},
588594
sign = {
@@ -1103,7 +1109,7 @@ Link Configuration ~
11031109
email = '󰀓 ',
11041110
-- Fallback icon for 'inline_link' elements
11051111
hyperlink = '󰌹 ',
1106-
-- Applies to the fallback inlined icon
1112+
-- Applies to the inlined icon as a fallback
11071113
highlight = 'RenderMarkdownLink',
11081114
-- Applies to WikiLink elements
11091115
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
@@ -1113,9 +1119,15 @@ Link Configuration ~
11131119
-- The key in this case 'web' is for healthcheck and to allow users to change its values
11141120
-- 'pattern': Matched against the destination text see :h lua-pattern
11151121
-- 'icon': Gets inlined before the link text
1116-
-- 'highlight': Highlight for the 'icon'
1122+
-- 'highlight': Optional highlight for the 'icon', uses fallback highlight if not provided
11171123
custom = {
1118-
web = { pattern = '^http[s]?://', icon = '󰖟 ', highlight = 'RenderMarkdownLink' },
1124+
web = { pattern = '^http', icon = '󰖟 ' },
1125+
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
1126+
github = { pattern = 'github%.com', icon = '󰊤 ' },
1127+
neovim = { pattern = 'neovim%.io', icon = ' ' },
1128+
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
1129+
discord = { pattern = 'discord%.com', icon = '󰙯 ' },
1130+
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
11191131
},
11201132
},
11211133
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.6.4'
7+
M.version = '7.6.5'
88

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

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ M.default_config = {
647647
email = '󰀓 ',
648648
-- Fallback icon for 'inline_link' elements
649649
hyperlink = '󰌹 ',
650-
-- Applies to the fallback inlined icon
650+
-- Applies to the inlined icon as a fallback
651651
highlight = 'RenderMarkdownLink',
652652
-- Applies to WikiLink elements
653653
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
@@ -657,9 +657,15 @@ M.default_config = {
657657
-- The key in this case 'web' is for healthcheck and to allow users to change its values
658658
-- 'pattern': Matched against the destination text see :h lua-pattern
659659
-- 'icon': Gets inlined before the link text
660-
-- 'highlight': Highlight for the 'icon'
660+
-- 'highlight': Optional highlight for the 'icon', uses fallback highlight if not provided
661661
custom = {
662-
web = { pattern = '^http[s]?://', icon = '󰖟 ', highlight = 'RenderMarkdownLink' },
662+
web = { pattern = '^http', icon = '󰖟 ' },
663+
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
664+
github = { pattern = 'github%.com', icon = '󰊤 ' },
665+
neovim = { pattern = 'neovim%.io', icon = '' },
666+
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
667+
discord = { pattern = 'discord%.com', icon = '󰙯 ' },
668+
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
663669
},
664670
},
665671
sign = {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Render:setup()
1818
return false
1919
end
2020

21-
local text, highlight, conceal = self.link.hyperlink, self.link.highlight, false
21+
local text, highlight, conceal = self.link.hyperlink, nil, false
2222
if self.node.type == 'email_autolink' then
2323
text, conceal = self.link.email .. self.node.text:sub(2, -2), true
2424
elseif self.node.type == 'image' then
@@ -30,7 +30,7 @@ function Render:setup()
3030
text, highlight = link_component.icon, link_component.highlight
3131
end
3232
end
33-
self.data = { text = text, highlight = highlight, conceal = conceal }
33+
self.data = { text = text, highlight = highlight or self.link.highlight, conceal = conceal }
3434

3535
return true
3636
end

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ function Render:wiki_link()
100100

101101
local parts = Str.split(self.node.text:sub(2, -2), '|')
102102
local link_component = self:link_component(parts[1])
103-
local icon, highlight = self.link.wiki.icon, self.link.wiki.highlight
103+
local icon, highlight = self.link.wiki.icon, nil
104104
if link_component ~= nil then
105105
icon, highlight = link_component.icon, link_component.highlight
106106
end
107+
highlight = highlight or self.link.wiki.highlight
107108
local link_text = icon .. parts[#parts]
108109
self.marks:add_over('link', self.node, {
109110
virt_text = { { link_text, highlight } },

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ function M.validate()
243243
:nested('custom', function(patterns)
244244
patterns
245245
:nested('ALL', function(pattern)
246-
pattern:type({ 'pattern', 'icon', 'highlight' }, 'string'):check()
246+
pattern
247+
:type({ 'pattern', 'icon' }, 'string')
248+
:type('highlight', { 'string', 'nil' })
249+
:check()
247250
end, false)
248251
:check()
249252
end)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
---@class (exact) render.md.LinkComponent
3636
---@field public pattern string
3737
---@field public icon string
38-
---@field public highlight string
38+
---@field public highlight? string
3939

4040
---@class (exact) render.md.WikiLink
4141
---@field public icon string

Diff for: scripts/update.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ def is_user(self) -> bool:
2424
return self.class_name.startswith("User")
2525

2626
def is_optional(self, field: str) -> bool:
27+
class_to_optional: dict[str, list[str]] = {
28+
"Handler": ["extends"],
29+
"UserCode": ["highlight_language"],
30+
"UserCustomCheckbox": ["scope_highlight"],
31+
"UserCheckboxComponent": ["scope_highlight"],
32+
"UserCustomCallout": ["quote_icon"],
33+
"UserLinkComponent": ["highlight"],
34+
}
35+
2736
# ---@field public extends? boolean -> extends
2837
# ---@field public start_row integer -> start_row
2938
# ---@field public attach? fun(buf: integer) -> attach
3039
field_name = field.split()[2].replace("?", "")
31-
return field_name in [
32-
"extends",
33-
"highlight_language",
34-
"quote_icon",
35-
"scope_highlight",
36-
]
40+
return field_name in class_to_optional.get(self.class_name, [])
3741

3842
def validate(self) -> None:
3943
for field in self.fields:

Diff for: tests/ad_hoc_spec.lua

+3-9
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ end
4949

5050
describe('ad_hoc.md', function()
5151
it('custom', function()
52-
util.setup('tests/data/ad_hoc.md', {
53-
link = {
54-
custom = {
55-
youtube = { pattern = 'www%.youtube%.com/', icon = '', highlight = util.hl('Link') },
56-
},
57-
},
58-
})
52+
util.setup('tests/data/ad_hoc.md')
5953

6054
local expected, row = {}, util.row()
6155

@@ -82,7 +76,7 @@ describe('ad_hoc.md', function()
8276

8377
vim.list_extend(expected, {
8478
util.bullet(row:increment(), 0, 1),
85-
link(row:get(), 2, 61, ' ', 'Link', nil),
79+
link(row:get(), 2, 61, '󰗃 ', 'Link', nil),
8680
})
8781

8882
vim.list_extend(expected, {
@@ -102,7 +96,7 @@ describe('ad_hoc.md', function()
10296
' 8 ● 󱗖 Basic One Then normal text',
10397
' 9 ● 󱗖 With Alias Something important',
10498
' 10 ● 󰀓 [email protected] Email',
105-
' 11 ● Youtube Link',
99+
' 11 ● 󰗃 Youtube Link',
106100
' 12 ● Footnote Link ¹ ᴵⁿᶠᵒ',
107101
' 13',
108102
' 14 ¹ ᴵⁿᶠᵒ: Some Info',

0 commit comments

Comments
 (0)