Skip to content

Commit

Permalink
feat: support bare URLs in angle brackets
Browse files Browse the repository at this point in the history
## Details

Request: #244

Bare URLs in angle brackets are parsed by treesitter into a uri_autolink
node. Add support for this node type in our link renderer. Use the icon
based on the destination. Functions similarly to emails in that the
angle brackets are hidden while the link contents are still displayed
with the addition of the icon.
  • Loading branch information
MeanderingProgrammer committed Dec 3, 2024
1 parent 61850bf commit 401a6c9
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- footnote text superscript rendering [#241](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/241)
[634acd5](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/634acd5da964c32f6947cd0c7802d7a116662665)
[1b5d117](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/1b5d11734122d9451d2e5e2e567fd61a62822293)
- code border none [#246](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/246)
[f3cda24](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/f3cda24c71261f6a52f5ddafb95786684d862d87)
- expand default custom links [#245](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/245)
[61850bf](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/61850bf7df4af8398e97559a35b62378ba8435b1)

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion doc/render-markdown.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*render-markdown.txt* For 0.10.0 Last change: 2024 December 02
*render-markdown.txt* For 0.10.0 Last change: 2024 December 03

==============================================================================
Table of Contents *render-markdown-table-of-contents*
Expand Down
1 change: 1 addition & 0 deletions lua/render-markdown/handler/markdown_inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function Handler.new(buf)
(full_reference_link)
(image)
(inline_link)
(uri_autolink)
] @link
((inline) @inline_highlight
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local state = require('render-markdown.state')
local M = {}

---@private
M.version = '7.6.5'
M.version = '7.6.6'

function M.check()
M.start('version')
Expand Down
47 changes: 40 additions & 7 deletions lua/render-markdown/render/link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,52 @@ function Render:setup()
return false
end

local text, highlight, conceal = self.link.hyperlink, nil, false
if self.node.type == 'email_autolink' then
text, conceal = self.link.email .. self.node.text:sub(2, -2), true
local email = self.node.text:sub(2, -2)
self.data = {
text = self.link.email .. email,
highlight = self.link.highlight,
conceal = true,
}
elseif self.node.type == 'full_reference_link' then
self.data = {
text = self.link.hyperlink,
highlight = self.link.highlight,
conceal = false,
}
elseif self.node.type == 'image' then
text = self.link.image
self.data = {
text = self.link.image,
highlight = self.link.highlight,
conceal = false,
}
elseif self.node.type == 'inline_link' then
local destination = self.node:child('link_destination')
local link_component = destination ~= nil and self:link_component(destination.text) or nil
if link_component ~= nil then
text, highlight = link_component.icon, link_component.highlight
local component = destination ~= nil and self:link_component(destination.text) or nil
local text, highlight = self.link.hyperlink, nil
if component ~= nil then
text, highlight = component.icon, component.highlight
end
self.data = {
text = text,
highlight = highlight or self.link.highlight,
conceal = false,
}
elseif self.node.type == 'uri_autolink' then
local destination = self.node.text:sub(2, -2)
local component = self:link_component(destination)
local text, highlight = self.link.hyperlink, nil
if component ~= nil then
text, highlight = component.icon, component.highlight
end
self.data = {
text = text .. destination,
highlight = highlight or self.link.highlight,
conceal = true,
}
else
return false
end
self.data = { text = text, highlight = highlight or self.link.highlight, conceal = conceal }

return true
end
Expand Down
10 changes: 4 additions & 6 deletions lua/render-markdown/render/shortcut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,13 @@ function Render:wiki_link()
end

local parts = Str.split(self.node.text:sub(2, -2), '|')
local link_component = self:link_component(parts[1])
local component = self:link_component(parts[1])
local icon, highlight = self.link.wiki.icon, nil
if link_component ~= nil then
icon, highlight = link_component.icon, link_component.highlight
if component ~= nil then
icon, highlight = component.icon, component.highlight
end
highlight = highlight or self.link.wiki.highlight
local link_text = icon .. parts[#parts]
self.marks:add_over('link', self.node, {
virt_text = { { link_text, highlight } },
virt_text = { { icon .. parts[#parts], highlight or self.link.wiki.highlight } },
virt_text_pos = 'inline',
conceal = '',
}, { 0, -1, 0, 1 })
Expand Down
14 changes: 10 additions & 4 deletions tests/ad_hoc_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('ad_hoc.md', function()
link(row:get(), 2, 20, '󰀓 [email protected]', 'Link', ''),
})

vim.list_extend(expected, {
util.bullet(row:increment(), 0, 1),
link(row:get(), 2, 26, '󰊤 http://www.github.com/', 'Link', ''),
})

vim.list_extend(expected, {
util.bullet(row:increment(), 0, 1),
link(row:get(), 2, 61, '󰗃 ', 'Link', nil),
Expand All @@ -96,10 +101,11 @@ describe('ad_hoc.md', function()
' 8 ● 󱗖 Basic One Then normal text',
' 9 ● 󱗖 With Alias Something important',
' 10 ● 󰀓 [email protected] Email',
' 11 ● 󰗃 Youtube Link',
' 12 ● Footnote Link ¹ ᴵⁿᶠᵒ',
' 13',
' 14 ¹ ᴵⁿᶠᵒ: Some Info',
' 11 ● 󰊤 http://www.github.com/ Bare URL',
' 12 ● 󰗃 Youtube Link',
' 13 ● Footnote Link ¹ ᴵⁿᶠᵒ',
' 14',
' 15 ¹ ᴵⁿᶠᵒ: Some Info',
})
end)
end)
1 change: 1 addition & 0 deletions tests/data/ad_hoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Heading 2 Line 2
- [[Basic One]] Then normal text
- [[Nickname|With Alias]] Something important
- <[email protected]> Email
- <http://www.github.com/> Bare URL
- [Youtube Link](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
- Footnote Link [^1 Info]

Expand Down

0 comments on commit 401a6c9

Please sign in to comment.