Skip to content

Commit 1092681

Browse files
committed
WIP: Add setting to save image based on buffer name
1 parent 787be15 commit 1092681

2 files changed

Lines changed: 69 additions & 12 deletions

File tree

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# img-paste.vim
2+
23
Yet simple tool to paste images into markdown files
34

5+
* [Use Case](#use-case)
6+
* [Installation](#installation)
7+
* [Usage](#usage)
8+
* [Extend to other markup languages](#extend-to-other-markup-languages)
9+
* [Asciidoctor](#asciidoctor)
10+
* [For linux user](#for-linux-user)
11+
* [Acknowledgements](#acknowledgements)
12+
413
## Use Case
514
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.
615

@@ -10,7 +19,7 @@ By default, the location of the saved file (`img/image1.png`) and the in-text re
1019

1120
Using Vundle
1221
```
13-
Plugin 'img-paste-devs/img-paste.vim'
22+
Plugin 'ferrine/md-img-paste.vim'
1423
```
1524

1625
## Usage
@@ -46,10 +55,40 @@ autocmd FileType markdown,tex nmap <buffer><silent> <leader>p :call mdip#Markdow
4655
'----'
4756
```
4857

58+
### Asciidoctor
59+
60+
For [Asciidoctor](https://asciidoctor.org/), something like this should get you started:
61+
62+
```viml
63+
"
64+
" Paste image inside an `.adoc` (Asciidoc[tor]) document.
65+
"
66+
" image::./img/<name.png>[Image Description]
67+
"
68+
function! g:AsciidocPasteImage(relpath)
69+
execute "normal! iimage::./" . a:relpath . "[I"
70+
let ipos = getcurpos()
71+
execute "normal! a" . "mage Description]"
72+
call setpos('.', ipos)
73+
execute "normal! vi[\<C-g>"
74+
endfunction
75+
76+
autocmd FileType asciidoctor
77+
\ let g:PasteImageFunction = 'g:AsciidocPasteImage'
78+
79+
"
80+
" Type <Leader>p to paste the image.
81+
"
82+
autocmd FileType asciidoctor
83+
\ nmap <buffer><silent> <leader>p
84+
\ :call mdip#MarkdownClipboardImage()<CR>
85+
```
86+
4987
| Filetype | Function name | Content |
5088
|----------|---------------|---------|
5189
| Markdown | MarkdownPasteImage | `![Image](path)` |
5290
| Latex | LatexPasteImage | `\includegraphics{path} \caption{Image}` |
91+
| Asciidoc | AsciidocPastImage | `image::./path[Image]` |
5392
| N/A | EmptyPasteImage | `path` |
5493

5594
PRs welcome

plugin/mdip.vim

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@ function! s:IsWSL()
88
endfunction
99

1010
function! s:SafeMakeDir()
11-
if !exists('g:mdip_imgdir_absolute')
11+
""
12+
" Assets directory based on the basename of the current buffer.
13+
"
14+
if exists('g:mdip_imgdir_bufname')
15+
""
16+
" The full path to the current buffer without the extension.
17+
"
18+
let path = expand('%:p:r')
19+
let outdir = path . '.assets'
20+
elseif !exists('g:mdip_imgdir_absolute')
1221
if s:os == "Windows"
1322
let outdir = expand('%:p:h') . '\' . g:mdip_imgdir
14-
else
23+
else
1524
let outdir = expand('%:p:h') . '/' . g:mdip_imgdir
1625
endif
1726
else
18-
let outdir = g:mdip_imgdir
27+
let outdir = g:mdip_imgdir
1928
endif
29+
2030
if !isdirectory(outdir)
21-
call mkdir(outdir,"p",0700)
31+
call mkdir(outdir, "p", 0700)
2232
endif
2333
if s:os == "Darwin"
2434
return outdir
@@ -137,20 +147,20 @@ function! s:SaveFileTMP(imgdir, tmpname)
137147
endfunction
138148

139149
function! s:SaveNewFile(imgdir, tmpfile)
140-
let extension = split(a:tmpfile, '\.')[-1]
150+
let ext = split(a:tmpfile, '\.')[-1]
141151
let reldir = g:mdip_imgdir
142152
let cnt = 0
143-
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . extension
144-
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . extension
153+
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . ext
154+
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . ext
145155
while filereadable(filename)
146156
call system('diff ' . a:tmpfile . ' ' . filename)
147157
if !v:shell_error
148158
call delete(a:tmpfile)
149159
return relpath
150160
endif
151161
let cnt += 1
152-
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . extension
153-
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . extension
162+
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . ext
163+
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . ext
154164
endwhile
155165
if filereadable(a:tmpfile)
156166
call rename(a:tmpfile, filename)
@@ -194,7 +204,7 @@ function! g:LatexPasteImage(relpath)
194204
endfunction
195205

196206
function! g:EmptyPasteImage(relpath)
197-
execute "normal! i" . a:relpath
207+
execute "normal! i" . a:relpath
198208
endfunction
199209

200210
let g:PasteImageFunction = 'g:MarkdownPasteImage'
@@ -244,7 +254,15 @@ if exists('g:mdip_imgdir_absolute')
244254
endif
245255
"allow a different intext reference for relative links
246256
if !exists('g:mdip_imgdir_intext')
247-
let g:mdip_imgdir_intext = g:mdip_imgdir
257+
""
258+
" Assets directory based on the name of the current buffer.
259+
"
260+
if exists('g:mdip_imgdir_bufname')
261+
let path = expand('%:r')
262+
let g:mdip_imgdir_intext = path . '.assets'
263+
else
264+
let g:mdip_imgdir_intext = g:mdip_imgdir
265+
endif
248266
endif
249267
if !exists('g:mdip_tmpname')
250268
let g:mdip_tmpname = 'tmp'

0 commit comments

Comments
 (0)