3rd / image.nvim

🖼️ Bringing images to Neovim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempting to render videos, even when not matching hijack_file_patterns, causes crash

e2r2fx opened this issue · comments

Trying to reference a video file, from a markdown link, shouldn't attempt render.
Expected behaviour: display link without attempting render.
Actual behaviour: image.nvim attempts render and then crashes.

Workaround: remove file extension from file avoids the issue.

Tested on Nobara 38, on minimal-setup.lua

image.mp4

I've tried recreating this with a very small mkv (about 200kb) and it doesn't crash my nvim, however, does significantly slow down my system - while actively editing the line that it appears on. My memory usage goes up about 200MB. Once I change to a different line and start editing the rest of the document it returns to normal.

My guess is that the file gets labelled to not be rendered, but it is still trying to load the file, but I'll have a more detailed look.

I think the filesize and system specs are key variables here. The file I used to test is 500 MB. The use case in case you're wondering is trying to link video meetings on a markdown wiki.

In integrations/markdown.lua images are found using treesitter - which determines the url of the image. MKV files in this case are being labelled as "images" and as such being set "to_render" in utils/document.lua. As far as I can tell, there is no filtering of filetypes before the call to Image:render().

I'm unsure if this is intended, but this could be fixed by either forcing the integrations to follow the hijack_file_patterns, or if we want to keep the treesitter labels, setting an exclusion list explicitly for the integrations.

Using hijack_file_patterns to make sure only the URLs that match are set "to_render" seems like a good idea to me. Its what I incorrectly assumed was the intended behaviour in the first place.

This is a possible solution inside image/utils/document.lua from line 54.

          local is_glob_match = function(glob,str)
            eval_str = '"'..str..'" =~ glob2regpat("'..glob..'")'
            return vim.api.nvim_eval(eval_str) ~= 0
           end
           for _, match in ipairs(matches) do
             local id = string.format("%d:%d:%d:%s", window.id, window.buffer, match.range.start_row, match.url)
 
             if ctx.options.only_render_image_at_cursor and match.range.start_row ~= cursor_row then goto continue end
 
            local valid_ftype = false
            for _,ctype in pairs(ctx.state.options.hijack_file_patterns) do
              if not valid_ftype then
                valid_ftype = is_glob_match(ctype,match.url)
              end
             end
            if not valid_ftype then goto continue end
             local to_render = {
               id = id,
               match = match,

However, I'll leave the PR for now as I'm sure there's a more elegant solution.

Fixed in 4c1c903 by trying to match a fixed list of image formats. We can easily extend that if needed.
I was scared to go the file extension route, the file header should be more reliable.
Hope it covers all the mainstream formats 🤞