From 0181009dba3c5a353fa695c2fb82a4a502be1cf7 Mon Sep 17 00:00:00 2001 From: Tinmarino Date: Wed, 9 Jun 2021 09:26:52 -0400 Subject: [PATCH] Feature: vimwiki#base#linkify() with markdown syntax setting #994 --- autoload/vimwiki/base.vim | 75 +++++++++++++++++++++------------------ test/link_creation.vader | 20 +++++++++++ 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 69d51e9..a2f226f 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -2882,50 +2882,55 @@ endfunction function! vimwiki#base#linkify() abort " 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 - let l:save_reg = @u - exe 'normal! "udiW' + " Save existing value of @u and delete url under the cursor into @u + let l:save_reg = @u + exe 'normal! "udiW' - " create a scratch buffer and switch to it - let current_buf = bufnr('') - let scratch_buf = bufnr('scratch',1) - exe 'sil! ' . scratch_buf . 'buffer' + " Create a scratch buffer and switch to it + let current_buf = bufnr('') + let scratch_buf = bufnr('scratch', 1) + exe 'sil! ' . scratch_buf . 'buffer' - " 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) - exe 'sil! :2Nread ' . @u + " 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) + exe 'sil! :2Nread ' . @u - " extract title from html - " Note: if URL cannot be downloaded the buffer is empty or contains a single - " line: 'Not found' - let page_ok=0 - if (wordcount().chars !=0 && getline(1) !=? 'Not found') - let page_ok=1 - " regex seems to work fine, but may not cover all cases - exe 'sil! :keepp %s/\v\((.|\r)+)\<\/title\>/\=s:get_title(submatch(1))/n' - endif + " Extract title from html + " Note: if URL cannot be downloaded the buffer is empty or contains a single + " line: 'Not found' + let page_ok=0 + if (wordcount().chars !=0 && getline(1) !=? 'Not found') + let page_ok=1 + " regex seems to work fine, but may not cover all cases + exe 'sil! :keepp %s/\v\((.|\r)+)\<\/title\>/\=s:get_title(submatch(1))/n' + endif - " wipeout scratch buffer and switch to current - exe scratch_buf . 'bwipeout' - exe current_buf . 'buffer' + " wipeout scratch buffer and switch to current + exe scratch_buf . 'bwipeout' + exe current_buf . 'buffer' - if (page_ok) - " use template [[URL|DESCRIPTION]] - let template = g:vimwiki_global_vars.WikiLinkTemplate2 - let link = substitute(template, '__LinkUrl__', @u, '') - let link = substitute(link, '__LinkDescription__', g:page_title==#'' ? @u : g:page_title, '') - exe 'normal! i' . link + if (page_ok) + if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' + " [DESC](URL) + let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink2Template') 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 + " [[URL]] + let link_tpl = g:vimwiki_global_vars.WikiLinkTemplate2 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 - let @u = l:save_reg + " restore initial value of @u + let @u = l:save_reg endfunction diff --git a/test/link_creation.vader b/test/link_creation.vader index c20c341..1620a61 100644 --- a/test/link_creation.vader +++ b/test/link_creation.vader @@ -4,6 +4,26 @@ # in OS windows, linux # 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()\ + +Expect(): + [[https://github.com/vimwiki/vimwiki|GitHub - vimwiki/vimwiki: Personal Wiki for Vim]] + + +Do(md: call linkify): + :call SetSyntax('markdown')\ + :call vimwiki#base#linkify()\ + +Expect(): + [GitHub - vimwiki/vimwiki: Personal Wiki for Vim](https://github.com/vimwiki/vimwiki) # Link Normalisation {{{1 # And configuration