Skip to content

Commit 7b1b15f

Browse files
feat: improve wiki link rendering
## Details Discussion: #284 Wiki links are rendered using a single extmark which conceals all content and then adds inline text as an overlay. This works but means that any other styles such as a strike through are removed. To improve this handle each component of the wiki link separately, similarly to the way autolinks are handled. This means we: - Hide the opening bracket - Inline the icon - Hide the link destination if an alias exists - Hide the closing bracket As 4 separate individual marks. This means the contents of the alias & destination will keep the style they had before rendering.
1 parent 1636954 commit 7b1b15f

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 January 04
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 January 06
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/health.lua

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

77
---@private
8-
M.version = '7.8.0'
8+
M.version = '7.8.1'
99

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

lua/render-markdown/render/shortcut.lua

+28-4
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,37 @@ function Render:wiki_link()
9898
return
9999
end
100100

101-
local parts = Str.split(self.node.text:sub(2, -2), '|')
102-
local icon, highlight = self:from_destination(self.link.wiki.icon, self.link.wiki.highlight, parts[1])
101+
local wiki = self.link.wiki
102+
local start_col, end_col = self.node.start_col, self.node.end_col
103+
local values = Str.split(self.node.text:sub(2, -2), '|')
104+
105+
-- Hide opening outer bracket
106+
self:hide(start_col - 1, start_col)
107+
108+
-- Add icon
109+
local icon, highlight = self:from_destination(wiki.icon, wiki.highlight, values[1])
103110
self.marks:add_over('link', self.node, {
104-
virt_text = { { icon .. parts[#parts], highlight } },
111+
virt_text = { { icon, highlight } },
105112
virt_text_pos = 'inline',
113+
})
114+
115+
-- Hide destination if there is an alias
116+
if #values > 1 then
117+
self:hide(start_col + 1, start_col + 1 + #values[1] + 1)
118+
end
119+
120+
-- Hide closing outer bracket
121+
self:hide(end_col, end_col + 1)
122+
end
123+
124+
---@private
125+
---@param start_col integer
126+
---@param end_col integer
127+
function Render:hide(start_col, end_col)
128+
self.marks:add(true, self.node.start_row, start_col, {
129+
end_col = end_col,
106130
conceal = '',
107-
}, { 0, -1, 0, 1 })
131+
})
108132
end
109133

110134
---@private

tests/ad_hoc_spec.lua

+15-18
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,6 @@ local function link(row, start_col, end_col, text, highlight, conceal)
4747
}
4848
end
4949

50-
---@param row integer
51-
---@param start_col integer
52-
---@param end_col integer
53-
---@param icon string
54-
---@return render.md.MarkInfo[]
55-
local function auto_link(row, start_col, end_col, icon)
56-
return {
57-
util.conceal(row, start_col, start_col + 1),
58-
link(row, start_col, end_col, icon, 'Link', nil),
59-
util.highlight(row, start_col, end_col, 'Link'),
60-
util.conceal(row, end_col - 1, end_col),
61-
}
62-
end
63-
6450
describe('ad_hoc.md', function()
6551
it('custom', function()
6652
util.setup('tests/data/ad_hoc.md')
@@ -75,22 +61,33 @@ describe('ad_hoc.md', function()
7561

7662
vim.list_extend(expected, {
7763
util.bullet(row:increment(), 0, 1),
78-
link(row:get(), 2, 15, '󱗖 Basic One', 'WikiLink', ''),
64+
util.conceal(row:get(), 2, 3),
65+
link(row:get(), 3, 14, '󱗖 ', 'WikiLink', nil),
66+
util.conceal(row:get(), 14, 15),
7967
})
8068

8169
vim.list_extend(expected, {
8270
util.bullet(row:increment(), 0, 1),
83-
link(row:get(), 2, 25, '󱗖 With Alias', 'WikiLink', ''),
71+
util.conceal(row:get(), 2, 3),
72+
link(row:get(), 3, 24, '󱗖 ', 'WikiLink', nil),
73+
util.conceal(row:get(), 4, 13),
74+
util.conceal(row:get(), 24, 25),
8475
})
8576

8677
vim.list_extend(expected, {
8778
util.bullet(row:increment(), 0, 1),
88-
auto_link(row:get(), 2, 20, '󰀓 '),
79+
util.conceal(row:get(), 2, 3),
80+
link(row:get(), 2, 20, '󰀓 ', 'Link', nil),
81+
util.highlight(row:get(), 2, 20, 'Link'),
82+
util.conceal(row:get(), 19, 20),
8983
})
9084

9185
vim.list_extend(expected, {
9286
util.bullet(row:increment(), 0, 1),
93-
auto_link(row:get(), 2, 26, '󰊤 '),
87+
util.conceal(row:get(), 2, 3),
88+
link(row:get(), 2, 26, '󰊤 ', 'Link', nil),
89+
util.highlight(row:get(), 2, 26, 'Link'),
90+
util.conceal(row:get(), 25, 26),
9491
})
9592

9693
vim.list_extend(expected, {

0 commit comments

Comments
 (0)