Skip to content

feat(images): add support for image previewing with Snacks #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lua/orgmode/colors/highlighter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
---@field private _ephemeral boolean
---@field private buffers table<number, { language_tree: vim.treesitter.LanguageTree, tree: TSTree }>
local OrgHighlighter = {}
local config = require('orgmode.config')

function OrgHighlighter:new()
local data = {
Expand Down
8 changes: 8 additions & 0 deletions lua/orgmode/colors/highlighter/markup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,12 @@ function OrgMarkup:use_ephemeral()
return self.highlighter._ephemeral
end

function OrgMarkup:get_links_for_line(bufnr, line)
local cache = self.cache[bufnr]
if not cache or not cache[line] then
return
end
return cache[line].highlights.link
end

return OrgMarkup
52 changes: 47 additions & 5 deletions lua/orgmode/colors/highlighter/markup/link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,57 @@ local OrgLink = {
['link.end'] = true,
},
}
OrgLink.__index = OrgLink

---@param opts { markup: OrgMarkupHighlighter }
function OrgLink:new(opts)
local data = {
local this = setmetatable({
markup = opts.markup,
has_extmark_url_support = vim.fn.has('nvim-0.10.2') == 1,
}
setmetatable(data, self)
self.__index = self
return data
}, OrgLink)
this:_set_directive()
return this
end

---@private
function OrgLink:_set_directive()
---@diagnostic disable-next-line: undefined-field
if not _G.Snacks then
return
end

vim.treesitter.query.add_directive('org-set-link!', function(match, _, source, pred, metadata)
---@type TSNode
local capture_id = pred[2]
local node = match[capture_id]

if not node or not self.has_extmark_url_support then
metadata['image.ignore'] = true
return
end

local start_row, start_col = node:range()
local line_cache = self.markup:get_links_for_line(source, start_row)
if not line_cache or #line_cache == 0 then
metadata['image.ignore'] = true
return
end
local entry_for_node = vim.tbl_filter(function(item)
return item.url and item.from.start_col == start_col and item.metadata.type == 'link_end'
end, line_cache)[1]

if not entry_for_node then
metadata['image.ignore'] = true
return
end

local url = entry_for_node.url
local prefix = url:sub(1, 5)
if prefix == 'file:' then
url = url:sub(6)
end
metadata['image.src'] = url
end, { force = true, all = false })
end

---@param node TSNode
Expand Down Expand Up @@ -229,6 +270,7 @@ function OrgLink:highlight(highlights, bufnr)
conceal = '',
})
end
entry.url = link_opts.url
-- Conceal the end marker (marked with << and >>)
-- [[https://neovim.io][Neovim<<]]>>
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - 2, {
Expand Down
1 change: 1 addition & 0 deletions queries/org/images.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((expr "[") @image (#org-set-link! @image))
Loading