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

Implement imgdir_filename_prefix #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<leader>p` key press in Vim. So it hooks `<leader>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

Expand All @@ -22,8 +22,18 @@ autocmd FileType markdown nmap <buffer><silent> <leader>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"
Expand Down
48 changes: 29 additions & 19 deletions plugin/mdip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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