Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FernandoBasso
Copy link

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
Copy link
Author

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

  • Originally, the plugin sets a path for the image and doesn't change it anymore for the lifetime of a vim session.
  • This PR introduces a setting that allows the image directory to be crated after the name of the current buffer.
    • Except if we open vim without a (vim path/to/intro.{md,adoc,etc}), the path is not updated later when actually opening a file.
    • Changing from one buffer to another does not update the path where to save the image the new setting is in place.

So, these are the challenges to overcome next.

@FernandoBasso FernandoBasso force-pushed the add-bufname-based-imgdir branch 2 times, most recently from 9608cfd to 1092681 Compare November 1, 2022 09:27
@FernandoBasso FernandoBasso force-pushed the add-bufname-based-imgdir branch from 1092681 to e0de63d Compare November 1, 2022 09:56
@DavidAnderegg
Copy link

DavidAnderegg commented May 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants