diff --git a/lua/image/image.lua b/lua/image/image.lua index ff38c85..f405669 100644 --- a/lua/image/image.lua +++ b/lua/image/image.lua @@ -353,7 +353,7 @@ local from_url = function(url, options, callback, state) return end - local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(url) .. ".png" + local tmp_path = state.tmp_dir .. "/" .. utils.hash.simple(url) .. ".png" local stdout = vim.loop.new_pipe() vim.loop.spawn("curl", { diff --git a/lua/image/utils/document.lua b/lua/image/utils/document.lua index 20633f9..7331880 100644 --- a/lua/image/utils/document.lua +++ b/lua/image/utils/document.lua @@ -53,7 +53,13 @@ local create_document_integration = function(config) local cursor_row = vim.api.nvim_win_get_cursor(window.id)[1] - 1 -- 0-indexed row for _, match in ipairs(matches) do - local id = string.format("%d:%d:%d:%s", window.id, window.buffer, match.range.start_row, match.url) + local id = string.format( + "%d:%d:%d:%s", + window.id, + window.buffer, + match.range.start_row, + utils.hash.simple(match.url) + ) if ctx.options.only_render_image_at_cursor and match.range.start_row ~= cursor_row then goto continue end diff --git a/lua/image/utils/hash.lua b/lua/image/utils/hash.lua new file mode 100644 index 0000000..b68d1b3 --- /dev/null +++ b/lua/image/utils/hash.lua @@ -0,0 +1,14 @@ +local bit = require("bit") + +local filename = function(str) + local hash = 5381 + for i = 1, #str do + local char = string.byte(str, i) + hash = bit.lshift(hash, 5) + hash + char + end + return hash +end + +return { + simple = filename, +} diff --git a/lua/image/utils/init.lua b/lua/image/utils/init.lua index d27a626..9b2570f 100644 --- a/lua/image/utils/init.lua +++ b/lua/image/utils/init.lua @@ -1,4 +1,5 @@ local base64 = require("image/utils/base64") +local hash = require("image/utils/hash") local logger = require("image/utils/logger") local magic = require("image/utils/magic") local math = require("image/utils/math") @@ -20,4 +21,5 @@ return { offsets = offsets, tmux = tmux, magic = magic, + hash = hash, }