Feature: vimwiki#base#linkify() with markdown syntax setting #994

This commit is contained in:
Tinmarino
2021-06-09 09:26:52 -04:00
parent 573e3d1952
commit 0181009dba
2 changed files with 60 additions and 35 deletions
+40 -35
View File
@@ -2882,50 +2882,55 @@ endfunction
function! vimwiki#base#linkify() abort function! vimwiki#base#linkify() abort
" Transform: the url under the cursor to a wiki link " Transform: the url under the cursor to a wiki link
let g:page_title = '' let g:page_title = ''
" save existing value of @u and delete url under the cursor into @u " Save existing value of @u and delete url under the cursor into @u
let l:save_reg = @u let l:save_reg = @u
exe 'normal! "udiW' exe 'normal! "udiW'
" create a scratch buffer and switch to it " Create a scratch buffer and switch to it
let current_buf = bufnr('') let current_buf = bufnr('')
let scratch_buf = bufnr('scratch',1) let scratch_buf = bufnr('scratch', 1)
exe 'sil! ' . scratch_buf . 'buffer' exe 'sil! ' . scratch_buf . 'buffer'
" load web page into scratch buffer using Nread with mode=2 " Load web page into scratch buffer using Nread with mode=2
" FIXME: on Windows, with vim 7/8 (not with nvim), makes the cmd.exe window show up (annoying) " FIXME: on Windows, with vim 7/8 (not with nvim), makes the cmd.exe window show up (annoying)
exe 'sil! :2Nread ' . @u exe 'sil! :2Nread ' . @u
" extract title from html " Extract title from html
" Note: if URL cannot be downloaded the buffer is empty or contains a single " Note: if URL cannot be downloaded the buffer is empty or contains a single
" line: 'Not found' " line: 'Not found'
let page_ok=0 let page_ok=0
if (wordcount().chars !=0 && getline(1) !=? 'Not found') if (wordcount().chars !=0 && getline(1) !=? 'Not found')
let page_ok=1 let page_ok=1
" regex seems to work fine, but may not cover all cases " regex seems to work fine, but may not cover all cases
exe 'sil! :keepp %s/\v\<title.{-}\>((.|\r)+)\<\/title\>/\=s:get_title(submatch(1))/n' exe 'sil! :keepp %s/\v\<title.{-}\>((.|\r)+)\<\/title\>/\=s:get_title(submatch(1))/n'
endif endif
" wipeout scratch buffer and switch to current " wipeout scratch buffer and switch to current
exe scratch_buf . 'bwipeout' exe scratch_buf . 'bwipeout'
exe current_buf . 'buffer' exe current_buf . 'buffer'
if (page_ok) if (page_ok)
" use template [[URL|DESCRIPTION]] if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
let template = g:vimwiki_global_vars.WikiLinkTemplate2 " [DESC](URL)
let link = substitute(template, '__LinkUrl__', @u, '') let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink2Template')
let link = substitute(link, '__LinkDescription__', g:page_title==#'' ? @u : g:page_title, '')
exe 'normal! i' . link
else else
"if URL could not be downloaded, undo and display message " [[URL]]
"TODO: other behaviours may be possible (user options?) let link_tpl = g:vimwiki_global_vars.WikiLinkTemplate2
exe 'normal! u'
echomsg 'Error downloading URL: ' . @u
endif endif
let link = substitute(link_tpl, '__LinkUrl__', @u, '')
let link = substitute(link, '__LinkDescription__', g:page_title==#'' ? @u : g:page_title, '')
exe 'normal! i' . link
else
"if URL could not be downloaded, undo and display message
"TODO: other behaviours may be possible (user options?)
exe 'normal! u'
echomsg 'Error downloading URL: ' . @u
endif
" restore initial value of @u " restore initial value of @u
let @u = l:save_reg let @u = l:save_reg
endfunction endfunction
+20
View File
@@ -4,6 +4,26 @@
# in OS windows, linux # in OS windows, linux
# Seems easy but tests are reaaly needed here # Seems easy but tests are reaaly needed here
# Linkify function {{{1
# Issue #994
####################
Given vimwiki (abc def ghi jkl):
https://github.com/vimwiki/vimwiki
Do(call linkify):
:call vimwiki#base#linkify()\<CR>
Expect():
[[https://github.com/vimwiki/vimwiki|GitHub - vimwiki/vimwiki: Personal Wiki for Vim]]
Do(md: call linkify):
:call SetSyntax('markdown')\<CR>
:call vimwiki#base#linkify()\<CR>
Expect():
[GitHub - vimwiki/vimwiki: Personal Wiki for Vim](https://github.com/vimwiki/vimwiki)
# Link Normalisation {{{1 # Link Normalisation {{{1
# And configuration # And configuration