Skip to content

Commit c46468f

Browse files
chore: minor refactor to link logic
1 parent 4d2aea3 commit c46468f

File tree

4 files changed

+55
-48
lines changed

4 files changed

+55
-48
lines changed

CHANGELOG.md

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

77
- add missing obsidian aliases [74b77c7](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/74b77c794d64d9d5a27c2a38ac254d9654fcad1f)
88
- store components in context, avoids duplicate queries [d228a3c](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/d228a3cb40f9e9687c3142cca1f46c4d3e985f7a)
9+
- improve health check for obsidian.nvim conflict [4d2aea3](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/4d2aea341a5d0bf2a01adc0ad4ecf5d4877e1bd0)
10+
- anyone using `acknowledge_conflicts` in their config should remove it
911

1012
### Bug Fixes
1113

lua/render-markdown/core/context.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ end
7474
---@param info render.md.NodeInfo
7575
---@param amount integer
7676
function Context:add_offset(info, amount)
77+
if amount == 0 then
78+
return
79+
end
7780
local row = info.start_row
7881
if self.links[row] == nil then
7982
self.links[row] = {}

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ function Handler:callout(info, callout)
109109
end
110110

111111
---Support for overriding title: https://help.obsidian.md/Editing+and+formatting/Callouts#Change+the+title
112-
---@return string, string?
112+
---@return string, boolean
113113
local function custom_title()
114114
local content = info:parent('inline')
115115
if content ~= nil then
116116
local line = str.split(content.text, '\n')[1]
117117
if #line > #callout.raw and vim.startswith(line:lower(), callout.raw:lower()) then
118118
local icon = str.split(callout.rendered, ' ')[1]
119119
local title = vim.trim(line:sub(#callout.raw + 1))
120-
return icon .. ' ' .. title, ''
120+
return icon .. ' ' .. title, true
121121
end
122122
end
123-
return callout.rendered, nil
123+
return callout.rendered, false
124124
end
125125

126126
local text, conceal = custom_title()
@@ -129,7 +129,7 @@ function Handler:callout(info, callout)
129129
end_col = info.end_col,
130130
virt_text = { { text, callout.highlight } },
131131
virt_text_pos = 'overlay',
132-
conceal = conceal,
132+
conceal = conceal and '' or nil,
133133
})
134134
if added then
135135
self.context:add_component(info, callout)
@@ -160,12 +160,18 @@ end
160160
---@private
161161
---@param info render.md.NodeInfo
162162
function Handler:wiki_link(info)
163-
if not self.config.link.enabled then
163+
local link = self.config.link
164+
if not link.enabled then
164165
return
165166
end
166-
local text = info.text:sub(2, -2)
167-
local parts = str.split(text, '|')
168-
local icon, highlight = self:dest_virt_text(parts[1])
167+
168+
local parts = str.split(info.text:sub(2, -2), '|')
169+
local link_component = self:link_component(parts[1])
170+
171+
local icon, highlight = link.hyperlink, link.highlight
172+
if link_component ~= nil then
173+
icon, highlight = link_component.icon, link_component.highlight
174+
end
169175
local link_text = icon .. parts[#parts]
170176
local added = self.marks:add(true, info.start_row, info.start_col - 1, {
171177
end_row = info.end_row,
@@ -182,60 +188,56 @@ end
182188
---@private
183189
---@param info render.md.NodeInfo
184190
function Handler:link(info)
185-
if not self.config.link.enabled then
191+
local link = self.config.link
192+
if not link.enabled then
186193
return
187194
end
188-
local link_text, highlight, conceal = self:link_virt_text(info)
189-
local added = self.marks:add(true, info.start_row, info.start_col, {
190-
end_row = info.end_row,
191-
end_col = info.end_col,
192-
virt_text = { { link_text, highlight } },
193-
virt_text_pos = 'inline',
194-
conceal = conceal,
195-
})
196-
if conceal == nil and added then
197-
self.context:add_offset(info, str.width(link_text))
198-
end
199-
end
200195

201-
---@private
202-
---@param info render.md.NodeInfo
203-
---@return string, string, string?
204-
function Handler:link_virt_text(info)
205-
local link = self.config.link
206-
if info.type == 'image' then
207-
return link.image, link.highlight
208-
elseif info.type == 'email_autolink' then
209-
return link.email .. info.text:sub(2, -2), link.highlight, ''
210-
elseif info.type == 'inline_link' then
211-
local destination = info:child('link_destination')
212-
if destination ~= nil then
213-
return self:dest_virt_text(destination.text)
196+
if info.type == 'email_autolink' then
197+
local link_text = link.email .. info.text:sub(2, -2)
198+
self.marks:add(true, info.start_row, info.start_col, {
199+
end_row = info.end_row,
200+
end_col = info.end_col,
201+
virt_text = { { link_text, link.highlight } },
202+
virt_text_pos = 'inline',
203+
conceal = '',
204+
})
205+
else
206+
local link_text, highlight = link.hyperlink, link.highlight
207+
if info.type == 'image' then
208+
link_text = link.image
209+
elseif info.type == 'inline_link' then
210+
local destination = info:child('link_destination')
211+
local link_component = destination ~= nil and self:link_component(destination.text) or nil
212+
if link_component ~= nil then
213+
link_text, highlight = link_component.icon, link_component.highlight
214+
end
215+
end
216+
217+
local added = self.marks:add(true, info.start_row, info.start_col, {
218+
end_row = info.end_row,
219+
end_col = info.end_col,
220+
virt_text = { { link_text, highlight } },
221+
virt_text_pos = 'inline',
222+
})
223+
if added then
224+
self.context:add_offset(info, str.width(link_text))
214225
end
215226
end
216-
return link.hyperlink, link.highlight
217227
end
218228

219229
---@private
220230
---@param destination string
221-
---@return string, string
222-
function Handler:dest_virt_text(destination)
223-
local link = self.config.link
224-
231+
---@return render.md.LinkComponent?
232+
function Handler:link_component(destination)
225233
---@type render.md.LinkComponent[]
226234
local link_components = vim.tbl_filter(function(link_component)
227235
return destination:find(link_component.pattern) ~= nil
228-
end, link.custom)
236+
end, self.config.link.custom)
229237
table.sort(link_components, function(a, b)
230238
return str.width(a.pattern) < str.width(b.pattern)
231239
end)
232-
233-
if #link_components > 0 then
234-
local link_component = link_components[#link_components]
235-
return link_component.icon, link_component.highlight
236-
else
237-
return link.hyperlink, link.highlight
238-
end
240+
return link_components[#link_components]
239241
end
240242

241243
---@class render.md.handler.MarkdownInline: render.md.Handler

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.3.4'
8+
M.version = '6.3.5'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

0 commit comments

Comments
 (0)