edluffy / hologram.nvim

👻 A cross platform terminal image viewer for Neovim. Extensible and fast, written in Lua and C. Works on macOS and Linux.
MIT License
1.28k stars 31 forks source link

How to auto-display images when opening a md file? #45

Open xorbuht opened 1 month ago

xorbuht commented 1 month ago

When I first open a markdown file with embedded images, it does not display these images. However it does leave empty space for these images to be displayed. It starts to display an image only after I edit a line with the link to the image. How do I auto-display images in a markdown file after I open it?

xorbuht commented 4 weeks ago

I've asked a Claude AI for help. It adviced me to modify the file ~/.local/share/nvim/lazy/hologram.nvim/lua/hologram/init.lua by adding the following code inside function hologram.setup(opts). Now it works as I wanted:

        vim.api.nvim_create_autocmd({"BufReadPost"}, {
            pattern = {"*.md", "*.markdown"},
            callback = function(au)
                vim.schedule(function()
                    hologram.buf_generate_images(au.buf, 0, -1)
                    hologram.buf_render_images(au.buf, 0, -1)
                end)
            end,
            group = augroup
        })
xorbuht commented 4 weeks ago

After some testing found that it does not always work. Sometimes pics briefly appear, then disappear.

zhuosongz commented 1 week ago

I got an error after I copy your codes in my setup function: Error executing vim.schedule lua callback: ...ocal/share/nvim/lazy/hologram.nvim/lua/hologram/init.lua:47: Invalid buffer id: 13