Skip to content

Commit 80ca17e

Browse files
feat(images): add support for image previewing with Snacks (#907)
1 parent f3f888a commit 80ca17e

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

lua/orgmode/colors/highlighter/init.lua

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
---@field private _ephemeral boolean
88
---@field private buffers table<number, { language_tree: vim.treesitter.LanguageTree, tree: TSTree }>
99
local OrgHighlighter = {}
10-
local config = require('orgmode.config')
1110

1211
function OrgHighlighter:new()
1312
local data = {

lua/orgmode/colors/highlighter/markup/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,12 @@ function OrgMarkup:use_ephemeral()
260260
return self.highlighter._ephemeral
261261
end
262262

263+
function OrgMarkup:get_links_for_line(bufnr, line)
264+
local cache = self.cache[bufnr]
265+
if not cache or not cache[line] then
266+
return
267+
end
268+
return cache[line].highlights.link
269+
end
270+
263271
return OrgMarkup

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

+47-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,57 @@ local OrgLink = {
88
['link.end'] = true,
99
},
1010
}
11+
OrgLink.__index = OrgLink
1112

1213
---@param opts { markup: OrgMarkupHighlighter }
1314
function OrgLink:new(opts)
14-
local data = {
15+
local this = setmetatable({
1516
markup = opts.markup,
1617
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 })
2162
end
2263

2364
---@param node TSNode
@@ -229,6 +270,7 @@ function OrgLink:highlight(highlights, bufnr)
229270
conceal = '',
230271
})
231272
end
273+
entry.url = link_opts.url
232274
-- Conceal the end marker (marked with << and >>)
233275
-- [[https://neovim.io][Neovim<<]]>>
234276
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - 2, {

queries/org/images.scm

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
((expr "[") @image (#org-set-link! @image))

0 commit comments

Comments
 (0)