img-paste-devs / img-paste.vim

paste image to markdown
383 stars 86 forks source link

WIP: Add setting to save image based on buffer name #70

Open FernandoBasso opened 2 years ago

FernandoBasso commented 2 years ago

NOTE: This is a work in progress.

Sometimes, we want to move a document around and then we have to search for its related images and move them around too, individually, or update image paths inside documents.

This PR allows for one extra setting which will make it possible to insert images in a directory named after the current buffer name.

For example, if the current buffer is docs/intro.adoc, the image will be copied to docs/intro.assets/<img-name>.png.

Or, if the current buffer is notes/fp/concepts.md, then the image will be notes/fp/concepts.assets/<img-name>.png.

It should work the same no matter the document file extension.

This allows for the document file and its assets directory to be moved together so that wherever the document is moved too, the images (or other assets, potentially) are moved together.

PS: I copied this idea from Typora (not sure if Typora invented it, but it is where I saw it and found it a useful approach for certain scenarios).

FernandoBasso commented 2 years ago

OK, I tested this a good deal and found out a few things:

So, these are the challenges to overcome next.

DavidAnderegg commented 1 year ago

If you use neovim with lua, you can achieve this functionality with the vanilla 'img-paste' like this:

vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
    pattern = {"*.wiki"},
    callback = function(ev)

        -- set the image directory to the same of the current filename
        local current_file = vim.fn.expand('%:t:r')
        vim.g.mdip_imgdir = current_file
        vim.g.mdip_imgdir_intext = current_file
    end
})

This only works when opening a *.wiki file but it would be easy to adapt it.