Skip to content

Commit 1fdd91e

Browse files
feat(images): add support for image previewing with Snacks
1 parent f3f888a commit 1fdd91e

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

lua/orgmode/colors/highlighter/init.lua

Lines changed: 0 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,56 @@ 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 self.has_extmark_url_support or 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 then
36+
return
37+
end
38+
39+
local start_row, start_col = node:range()
40+
local line_cache = self.markup:get_links_for_line(source, start_row)
41+
if not line_cache or #line_cache == 0 then
42+
metadata['image.ignore'] = true
43+
return
44+
end
45+
local entry_for_node = vim.tbl_filter(function(item)
46+
return item.url and item.from.start_col == start_col and item.metadata.type == 'link_end'
47+
end, line_cache)[1]
48+
49+
if not entry_for_node then
50+
metadata['image.ignore'] = true
51+
return
52+
end
53+
54+
local url = entry_for_node.url
55+
local prefix = url:sub(1, 5)
56+
if prefix == 'file:' then
57+
url = url:sub(6)
58+
end
59+
metadata['image.src'] = url
60+
end, { force = true, all = false })
2161
end
2262

2363
---@param node TSNode
@@ -229,6 +269,7 @@ function OrgLink:highlight(highlights, bufnr)
229269
conceal = '',
230270
})
231271
end
272+
entry.url = link_opts.url
232273
-- Conceal the end marker (marked with << and >>)
233274
-- [[https://neovim.io][Neovim<<]]>>
234275
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - 2, {

queries/org/images.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
((expr "[") @image (#org-set-link! @image))

0 commit comments

Comments
 (0)