Forked from KainokiKaede/CreateMarkdownHyperLink.vim
Last active
April 2, 2019 03:10
-
-
Save nora75/bd3c5a58488baf1ba077efe88a06cdfa to your computer and use it in GitHub Desktop.
Create Markdown hyperlink automatically with title. Usage: type ``:MDURL`` over a URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Create Markdown Hyperlink Automatically | |
" Requires mattn/webapi-vim (or vital.vim), tpope/vim-surround, | |
" kana/vim-textobj-user, mattn/vim-textobj-url | |
" command! MDURL call CreateMarkdownHyperLink() | |
" command! MDURLTitle call CreateMarkdownHyperLinkWithTitle() | |
function! GetWebPageTitle(url) | |
let url = substitute(a:url,'\n\|\r\|\r\n','','g') | |
let res = webapi#http#get(url) | |
let dom = webapi#html#parse(res.content) | |
return dom.childNode('head').childNode('title').value() | |
endfunction | |
function! CreateMarkdownHyperLinkWithTitle() | |
let areg = @a | |
normal viu | |
normal! "ay | |
let url = @a | |
try | |
let title = GetWebPageTitle(url) | |
catch | |
echo "Can't get title.Please input title by yourself." | |
call CreateMarkdownHyperLink() | |
return | |
endtry | |
execute 'normal viuS)i['.title.']' | |
let @a = areg | |
endfunction | |
function! CreateMarkdownHyperLink() | |
execute 'normal viuS)i[]' | |
startinsert | |
endfunction | |
aug markdown | |
au! | |
autocmd FileType markdown command! -buffer MDURL call CreateMarkdownHyperLinkWithTitle() | |
" autocmd FileType markdown nnoremap <buffer><silent> [Markdown]u :<C-u>call CreateMarkdownHyperLinkWithTitle()<CR> | |
aug END |
デバッグ用のechomをechoに戻し、コピペ出来てなかったせいでechohlがwarningになったままだったのを修正。
単純にタイトルを挿入している為imapが適用されてしまうバグ直します。そのうち。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
バグ修正済み。そのうち強化してwebapi以外依存無しにする予定。そのうち、いつかは知らん。
非同期通信もしたい。