Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0683da9

Browse files
feat(hyperlinks): Add OSC 8 hyperlink support
1 parent 38a02a0 commit 0683da9

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lua/orgmode/colors/highlighter/markup/link.lua

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
local ts_utils = require('orgmode.utils.treesitter')
2+
local utils = require('orgmode.utils')
23

34
---@class OrgLinkHighlighter : OrgMarkupHighlighter
45
---@field private markup OrgMarkupHighlighter
6+
---@field private has_extmark_url_support boolean
57
local OrgLink = {}
68

79
---@param opts { markup: OrgMarkupHighlighter }
810
function OrgLink:new(opts)
911
local data = {
1012
markup = opts.markup,
13+
has_extmark_url_support = vim.fn.has('nvim-0.10.2') == 1,
1114
}
1215
setmetatable(data, self)
1316
self.__index = self
@@ -100,12 +103,18 @@ function OrgLink:highlight(highlights, bufnr)
100103
local alias = link:find('%]%[') or 1
101104
local link_end = link:find('%]%[') or (link:len() - 1)
102105

103-
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
106+
local link_opts = {
104107
ephemeral = ephemeral,
105108
end_col = entry.to.end_col,
106109
hl_group = '@org.hyperlink',
107110
priority = 110,
108-
})
111+
}
112+
113+
if self.has_extmark_url_support then
114+
link_opts.url = alias > 1 and link:sub(3, alias - 1) or link:sub(3, -3)
115+
end
116+
117+
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, link_opts)
109118

110119
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
111120
ephemeral = ephemeral,

tests/plenary/colors/highlighter_spec.lua

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local api = vim.api
55

66
describe('highlighter', function()
77
local ns_id = api.nvim_create_namespace('org_custom_highlighter')
8+
local has_extmarks_url_support = vim.fn.has('nvim-0.10.2') == 1
9+
810
local get_extmarks = function(content)
911
---@diagnostic disable-next-line: inject-field
1012
config.ts_hl_enabled = true
@@ -33,6 +35,9 @@ describe('highlighter', function()
3335
if opts.spell ~= nil then
3436
assert.are.same(opts.spell, details.spell, 'spell is not matching')
3537
end
38+
if has_extmarks_url_support and opts.url ~= nil then
39+
assert.are.same(opts.url, details.url, 'url is not matching')
40+
end
3641
end
3742

3843
after_each(function()
@@ -209,7 +214,10 @@ describe('highlighter', function()
209214
'I have [[https://google.com]] link',
210215
})
211216
assert.are.same(4, #extmarks)
212-
assert_extmark(extmarks[1], { line = 0, start_col = 7, end_col = 29, hl_group = '@org.hyperlink' })
217+
assert_extmark(
218+
extmarks[1],
219+
{ line = 0, start_col = 7, end_col = 29, hl_group = '@org.hyperlink', url = 'https://google.com' }
220+
)
213221
assert_extmark(extmarks[2], { line = 0, start_col = 7, end_col = 9, conceal = '' })
214222
assert_extmark(extmarks[3], { line = 0, start_col = 9, end_col = 27, spell = false })
215223
assert_extmark(extmarks[4], { line = 0, start_col = 27, end_col = 29, conceal = '' })
@@ -220,7 +228,10 @@ describe('highlighter', function()
220228
'I have [[https://google.com][google]] link',
221229
})
222230
assert.are.same(4, #extmarks)
223-
assert_extmark(extmarks[1], { line = 0, start_col = 7, end_col = 37, hl_group = '@org.hyperlink' })
231+
assert_extmark(
232+
extmarks[1],
233+
{ line = 0, start_col = 7, end_col = 37, hl_group = '@org.hyperlink', url = 'https://google.com' }
234+
)
224235
assert_extmark(extmarks[2], { line = 0, start_col = 7, end_col = 29, conceal = '' })
225236
assert_extmark(extmarks[3], { line = 0, start_col = 9, end_col = 27, spell = false })
226237
assert_extmark(extmarks[4], { line = 0, start_col = 35, end_col = 37, conceal = '' })
@@ -231,7 +242,10 @@ describe('highlighter', function()
231242
'I have [[https://google.com][google I am *not bold*]] link',
232243
})
233244
assert.are.same(4, #extmarks)
234-
assert_extmark(extmarks[1], { line = 0, start_col = 7, end_col = 53, hl_group = '@org.hyperlink' })
245+
assert_extmark(
246+
extmarks[1],
247+
{ line = 0, start_col = 7, end_col = 53, hl_group = '@org.hyperlink', url = 'https://google.com' }
248+
)
235249
assert_extmark(extmarks[2], { line = 0, start_col = 7, end_col = 29, conceal = '' })
236250
assert_extmark(extmarks[3], { line = 0, start_col = 9, end_col = 27, spell = false })
237251
assert_extmark(extmarks[4], { line = 0, start_col = 51, end_col = 53, conceal = '' })

0 commit comments

Comments
 (0)