@@ -8,16 +8,57 @@ local OrgLink = {
8
8
[' link.end' ] = true ,
9
9
},
10
10
}
11
+ OrgLink .__index = OrgLink
11
12
12
13
--- @param opts { markup : OrgMarkupHighlighter }
13
14
function OrgLink :new (opts )
14
- local data = {
15
+ local this = setmetatable ( {
15
16
markup = opts .markup ,
16
17
has_extmark_url_support = vim .fn .has (' nvim-0.10.2' ) == 1 ,
17
- }
18
- setmetatable (data , self )
19
- self .__index = self
20
- return data
18
+ }, OrgLink )
19
+ this :_set_directive ()
20
+ return this
21
+ end
22
+
23
+ --- @private
24
+ function OrgLink :_set_directive ()
25
+ --- @diagnostic disable-next-line : undefined-field
26
+ if not _G .Snacks then
27
+ return
28
+ end
29
+
30
+ vim .treesitter .query .add_directive (' org-set-link!' , function (match , _ , source , pred , metadata )
31
+ --- @type TSNode
32
+ local capture_id = pred [2 ]
33
+ local node = match [capture_id ]
34
+
35
+ if not node or not self .has_extmark_url_support then
36
+ metadata [' image.ignore' ] = true
37
+ return
38
+ end
39
+
40
+ local start_row , start_col = node :range ()
41
+ local line_cache = self .markup :get_links_for_line (source , start_row )
42
+ if not line_cache or # line_cache == 0 then
43
+ metadata [' image.ignore' ] = true
44
+ return
45
+ end
46
+ local entry_for_node = vim .tbl_filter (function (item )
47
+ return item .url and item .from .start_col == start_col and item .metadata .type == ' link_end'
48
+ end , line_cache )[1 ]
49
+
50
+ if not entry_for_node then
51
+ metadata [' image.ignore' ] = true
52
+ return
53
+ end
54
+
55
+ local url = entry_for_node .url
56
+ local prefix = url :sub (1 , 5 )
57
+ if prefix == ' file:' then
58
+ url = url :sub (6 )
59
+ end
60
+ metadata [' image.src' ] = url
61
+ end , { force = true , all = false })
21
62
end
22
63
23
64
--- @param node TSNode
@@ -229,6 +270,7 @@ function OrgLink:highlight(highlights, bufnr)
229
270
conceal = ' ' ,
230
271
})
231
272
end
273
+ entry .url = link_opts .url
232
274
-- Conceal the end marker (marked with << and >>)
233
275
-- [[https://neovim.io][Neovim<<]]>>
234
276
vim .api .nvim_buf_set_extmark (bufnr , namespace , entry .from .line , entry .to .end_col - 2 , {
0 commit comments