diff --git a/README.md b/README.md index 3548b99..eb2eb82 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Yet simple tool to paste images into markdown files ## Use Case You are editing a markdown file and have an image on the clipboard and want to paste it into the document as the text `![](img/image1.png)`. Instead of first copying it to that directory, you want to do it with a single `p` key press in Vim. So it hooks `p`, checks if you are editing a Markdown file, saves the image from the clipboard to the location `img/image1.png`, and inserts `![](img/image1.png)` into the file. -By default, the location of the saved file (`img/image1.png`) and the in-text reference (`![](img/image1.png`) are identical. You can change this behavior by specyfing an absolute path to save the file (`let g:mdip_imgdir_absolute = /absolute/path/to/imgdir` on linux) and a different path for in-text references (`let g:mdip_imgdir_intext = /relative/path/to/imgdir` on linux). +By default, the location of the saved file (`img/image1.png`) and the in-text reference (`![](img/image1.png`) are identical. You can change this behavior by specyfing an absolute path to save the file (`let g:mdip_imgdir_absolute = /absolute/path/to/imgdir` on linux) and a different path for in-text references (`let g:mdip_imgdir_intext = /relative/path/to/imgdir` on linux). ## Installation @@ -22,8 +22,18 @@ autocmd FileType markdown nmap p :call mdip#MarkdownCli " let g:mdip_imgname = 'image' ``` +## Prefix imgdir with file name + +To prefix `imgdir` with the name of the file, add the following to your .vimrc : +``` +let g:mdip_imgdir_filename_prefix=1 +``` +In this case, the images you copy into a file called `test.md` would be located in `test_img` + +/!\ You can't use both `mdip_imgdir_absolute` and `mdip_imgdir_filename_prefix` at the same time. + ### Extend to other markup languages ### -Simply add a custom paste function that accepts the relative path to the image as an argument, and set `g:PasteImageFunction` to the name of your function. E.g. +Simply add a custom paste function that accepts the relative path to the image as an argument, and set `g:PasteImageFunction` to the name of your function. E.g. ``` function! g:LatexPasteImage(relpath) execute "normal! i\\includegraphics{" . a:relpath . "}\r\\caption{I" diff --git a/plugin/mdip.vim b/plugin/mdip.vim index 0038a20..1b1a560 100644 --- a/plugin/mdip.vim +++ b/plugin/mdip.vim @@ -7,16 +7,17 @@ function! s:IsWSL() return 0 endfunction -function! s:SafeMakeDir() +function! s:SafeMakeDir(imgdir) if !exists('g:mdip_imgdir_absolute') if s:os == "Windows" - let outdir = expand('%:p:h') . '\' . g:mdip_imgdir - else - let outdir = expand('%:p:h') . '/' . g:mdip_imgdir + let outdir = expand('%:p:h') . '\' . a:imgdir + else + let outdir = expand('%:p:h') . '/' . a:imgdir endif else - let outdir = g:mdip_imgdir + let outdir = a:imgdir endif + if !isdirectory(outdir) call mkdir(outdir) endif @@ -138,7 +139,7 @@ endfunction function! s:SaveNewFile(imgdir, tmpfile) let extension = split(a:tmpfile, '\.')[-1] - let reldir = g:mdip_imgdir + let reldir = g:mdip_final_imgdir let cnt = 0 let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . extension let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . extension @@ -194,7 +195,7 @@ function! g:LatexPasteImage(relpath) endfunction function! g:EmptyPasteImage(relpath) - execute "normal! i" . a:relpath + execute "normal! i" . a:relpath endfunction let g:PasteImageFunction = 'g:MarkdownPasteImage' @@ -206,40 +207,49 @@ function! mdip#MarkdownClipboardImage() let s:os = substitute(system('uname'), '\n', '', '') endif - let workdir = s:SafeMakeDir() + " prefix dir name with filename_ (without extension) + if exists('g:mdip_imgdir_filename_prefix') && g:mdip_imgdir_filename_prefix == 1 + let g:mdip_final_imgdir = g:mdip_imgdir . '/' . expand("%:r") + else + let g:mdip_final_imgdir = g:mdip_imgdir + endif + + " allow a different intext reference for relative links + if !exists('g:mdip_imgdir_intext') + let g:mdip_final_imgdir_intext = g:mdip_final_imgdir + endif + + let workdir = s:SafeMakeDir(g:mdip_final_imgdir) " change temp-file-name and image-name let g:mdip_tmpname = s:InputName() if empty(g:mdip_tmpname) - let g:mdip_tmpname = g:mdip_imgname . '_' . s:RandomName() + let g:mdip_tmpname = g:mdip_imgname . '/' . s:RandomName() endif let tmpfile = s:SaveFileTMP(workdir, g:mdip_tmpname) if tmpfile == 1 return else - " let relpath = s:SaveNewFile(g:mdip_imgdir, tmpfile) + " let relpath = s:SaveNewFile(g:mdip_final_imgdir, tmpfile) let extension = split(tmpfile, '\.')[-1] - let relpath = g:mdip_imgdir_intext . '/' . g:mdip_tmpname . '.' . extension + let relpath = g:mdip_final_imgdir_intext . '/' . g:mdip_tmpname . '.' . extension if call(get(g:, 'PasteImageFunction'), [relpath]) return endif endif endfunction -if !exists('g:mdip_imgdir') && !exists('g:mdip_imgdir_absolute') - let g:mdip_imgdir = 'img' -endif -"allow absolute paths. E.g., on linux: /home/path/to/imgdir/ if exists('g:mdip_imgdir_absolute') + "allow absolute paths. E.g., on linux: /home/path/to/imgdir/ let g:mdip_imgdir = g:mdip_imgdir_absolute +elseif !exists('g:mdip_imgdir') + let g:mdip_imgdir = 'img' endif -"allow a different intext reference for relative links -if !exists('g:mdip_imgdir_intext') - let g:mdip_imgdir_intext = g:mdip_imgdir -endif + if !exists('g:mdip_tmpname') let g:mdip_tmpname = 'tmp' endif + if !exists('g:mdip_imgname') let g:mdip_imgname = 'image' endif