feat(image): added opts.img_dirs to configure the search path for resolving images. Closes #1222

This commit is contained in:
Folke Lemaitre 2025-02-17 13:12:42 +01:00
parent 81395af12a
commit ad0b88dc08
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 22 additions and 7 deletions

View file

@ -40,6 +40,7 @@ M.transforms = {
local hover ---@type snacks.image.Hover?
local uv = vim.uv or vim.loop
local dir_cache = {} ---@type table<string, boolean>
---@param str string
function M.url_decode(str)
@ -48,6 +49,14 @@ function M.url_decode(str)
end)
end
---@param dir string
function M.is_dir(dir)
if dir_cache[dir] == nil then
dir_cache[dir] = vim.fn.isdirectory(dir) == 1
end
return dir_cache[dir]
end
---@param buf number
---@param src string
function M.resolve(buf, src)
@ -58,13 +67,18 @@ function M.resolve(buf, src)
return s
end
if not src:find("^%w%w+://") then
if src:find("^%.") or src:find("^%w") then
for _, dir in ipairs({ vim.fs.dirname(file), uv.cwd() }) do
local path = dir .. "/" .. src
if vim.fn.filereadable(path) == 1 then
src = path
break
end
local cwd = uv.cwd() or "."
local checks = { src, vim.fs.dirname(file) .. "/" .. src }
for _, dir in ipairs(Snacks.image.config.img_dirs) do
dir = cwd .. "/" .. dir
if M.is_dir(dir) then
checks[#checks + 1] = dir .. "/" .. src
end
end
for _, f in ipairs(checks) do
if vim.fn.filereadable(f) == 1 then
src = uv.fs_realpath(f) or f
break
end
end
src = vim.fs.normalize(src)