Skip to content
Draft
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
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# img-paste.vim

Yet simple tool to paste images into markdown files

* [Use Case](#use-case)
* [Installation](#installation)
* [Usage](#usage)
* [Extend to other markup languages](#extend-to-other-markup-languages)
* [Asciidoctor](#asciidoctor)
* [For linux user](#for-linux-user)
* [Acknowledgements](#acknowledgements)

## 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.

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

Using Vundle
```
Plugin 'img-paste-devs/img-paste.vim'
Plugin 'ferrine/md-img-paste.vim'
```

## Usage
Expand Down Expand Up @@ -46,15 +55,49 @@ autocmd FileType markdown,tex nmap <buffer><silent> <leader>p :call mdip#Markdow
'----'
```

### Asciidoctor

For [Asciidoctor](https://asciidoctor.org/), something like this should get you started:

```viml
""
" Paste image inside an `.adoc` (Asciidoc[tor]) document.
"
" image::./img/<name.png>[Image description]
"
function! g:AsciidocPasteImage(relpath)
execute "normal! iimage::./" . a:relpath . "[I"
let ipos = getcurpos()
execute "normal! a" . "mage description]"
call setpos('.', ipos)
execute "normal! vi[\<C-g>"
endfunction

""
" Set `AsciidocPastImage` as the paste function for Asciidoc
" (or Asciidoctor) buffers.
"
autocmd FileType asciidoctor
\ let g:PasteImageFunction = 'g:AsciidocPasteImage'

""
" Type <Leader>p to paste the image.
"
autocmd FileType asciidoctor
\ nmap <buffer><silent> <leader>p
\ :call mdip#MarkdownClipboardImage()<CR>
```

| Filetype | Function name | Content |
|----------|---------------|---------|
| Markdown | MarkdownPasteImage | `![Image](path)` |
| Latex | LatexPasteImage | `\includegraphics{path} \caption{Image}` |
| Asciidoc | AsciidocPasteImage | `image::./path[Image]` |
| N/A | EmptyPasteImage | `path` |

PRs welcome

### For linux user
### For Linux user
This plugin gets clipboard content by running the `xclip` command.

install `xclip` first.
Expand Down
40 changes: 29 additions & 11 deletions plugin/mdip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ function! s:IsWSL()
endfunction

function! s:SafeMakeDir()
if !exists('g:mdip_imgdir_absolute')
""
" Assets directory based on the basename of the current buffer.
"
if exists('g:mdip_imgdir_bufname')
""
" The full path to the current buffer without the extension.
"
let path = expand('%:p:r')
let outdir = path . '.assets'
elseif !exists('g:mdip_imgdir_absolute')
if s:os == "Windows"
let outdir = expand('%:p:h') . '\' . g:mdip_imgdir
else
else
let outdir = expand('%:p:h') . '/' . g:mdip_imgdir
endif
else
let outdir = g:mdip_imgdir
let outdir = g:mdip_imgdir
endif

if !isdirectory(outdir)
call mkdir(outdir,"p",0700)
call mkdir(outdir, "p", 0700)
endif
if s:os == "Darwin"
return outdir
Expand Down Expand Up @@ -137,20 +147,20 @@ function! s:SaveFileTMP(imgdir, tmpname)
endfunction

function! s:SaveNewFile(imgdir, tmpfile)
let extension = split(a:tmpfile, '\.')[-1]
let ext = split(a:tmpfile, '\.')[-1]
let reldir = g:mdip_imgdir
let cnt = 0
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . extension
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . extension
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . ext
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . ext
while filereadable(filename)
call system('diff ' . a:tmpfile . ' ' . filename)
if !v:shell_error
call delete(a:tmpfile)
return relpath
endif
let cnt += 1
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . extension
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . extension
let filename = a:imgdir . '/' . g:mdip_imgname . cnt . '.' . ext
let relpath = reldir . '/' . g:mdip_imgname . cnt . '.' . ext
endwhile
if filereadable(a:tmpfile)
call rename(a:tmpfile, filename)
Expand Down Expand Up @@ -194,7 +204,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 Down Expand Up @@ -244,7 +254,15 @@ if exists('g:mdip_imgdir_absolute')
endif
"allow a different intext reference for relative links
if !exists('g:mdip_imgdir_intext')
let g:mdip_imgdir_intext = g:mdip_imgdir
""
" Assets directory based on the name of the current buffer.
"
if exists('g:mdip_imgdir_bufname')
let path = expand('%:r')
let g:mdip_imgdir_intext = path . '.assets'
else
let g:mdip_imgdir_intext = g:mdip_imgdir
endif
endif
if !exists('g:mdip_tmpname')
let g:mdip_tmpname = 'tmp'
Expand Down