fix(image): work-around for sha256 not allowed to be a Blob

This commit is contained in:
Folke Lemaitre 2025-10-21 23:39:36 +02:00
parent 080320bb82
commit 92a08cece7
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -23,6 +23,7 @@ local M = {}
---@field pos snacks.image.Pos
---@field src? string
---@field content? string
---@field content_id? string
---@field ext? string
---@field range? Range4
---@field lang string
@ -63,6 +64,7 @@ M.transforms = {
return
end
img.content = vim.base64.decode(data)
img.content_id = data:sub(1, 20)
img.src = nil
img.ext = ft:match("^image/(%w+)$") or "png"
end,
@ -319,7 +321,11 @@ function M._img(ctx)
if img.content and not img.src then
local root = Snacks.image.config.cache
vim.fn.mkdir(root, "p")
img.src = root .. "/" .. vim.fn.sha256(img.content):sub(1, 8) .. "-content." .. (img.ext or "png")
img.src = root
.. "/"
.. (img.content_id or vim.fn.sha256(img.content):sub(1, 8))
.. "-content."
.. (img.ext or "png")
if vim.fn.filereadable(img.src) == 0 then
local fd = assert(io.open(img.src, "w"), "failed to open " .. img.src)
fd:write(img.content)