From e3e841f335f48f7565acce0cfda78ae7f4cb2eb6 Mon Sep 17 00:00:00 2001 From: Rajdeep Date: Sun, 26 Jan 2020 20:39:03 -0500 Subject: [PATCH] Feature: Add handling of absolute path to vimwiki #811 from @justrajdeep - update vimwiki link handling so that if using env variables it is resolved - add [[//absolute_path]] to handle absolute path - updated doc to reflect the changes --- autoload/vimwiki/base.vim | 23 +++++++++----- doc/vimwiki.txt | 15 ++++++++-- test/link_creation.vader | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 10 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index a852708..65e3a45 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -131,7 +131,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort let source_file = vimwiki#path#current_wiki_file() endif - " get rid of '\' in escaped characters in []() style markdown links + " Get rid of '\' in escaped characters in []() style markdown links " other style links don't allow '\' let link_text = substitute(a:link_text, '\(\\\)\(\W\)\@=', '', 'g') @@ -163,7 +163,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort let is_wiki_link = s:is_wiki_link(link_infos) - " extract anchor + " Extract anchor if is_wiki_link let split_lnk = split(link_text, '#', 1) let link_text = split_lnk[0] @@ -181,10 +181,17 @@ function! vimwiki#base#resolve_link(link_text, ...) abort endif endif - " check if absolute or relative path + " Check if absolute or relative path + let is_absolute = 0 if is_wiki_link && link_text[0] ==# '/' if link_text !=# '/' - let link_text = link_text[1:] + if link_text !=# '//' && link_text[0:1] ==# '//' + let link_text = resolve(expand(link_text)) + let link_text = link_text[2:] + let is_absolute = 1 + else + let link_text = link_text[1:] + endif endif let is_relative = 0 elseif !is_wiki_link && vimwiki#path#is_absolute(link_text) @@ -195,7 +202,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort endif - " extract the other items depending on the scheme + " Extract the other items depending on the scheme if link_infos.scheme =~# '\mwiki\d\+' " interwiki link named wiki 'wn.name:link' format @@ -227,8 +234,9 @@ function! vimwiki#base#resolve_link(link_text, ...) abort return link_infos endif endif - - if !is_relative || link_infos.index != source_wiki + if is_absolute + let root_dir = '' + elseif !is_relative || link_infos.index != source_wiki let root_dir = vimwiki#vars#get_wikilocal('path', link_infos.index) endif @@ -265,6 +273,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort endif let link_infos.filename = vimwiki#path#normalize(link_infos.filename) + return link_infos endfunction diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt index 10d79d1..2d9b176 100644 --- a/doc/vimwiki.txt +++ b/doc/vimwiki.txt @@ -1009,16 +1009,24 @@ With description: > Wiki files don't need to be in the root directory of your wiki, you can put them in subdirectories as well: > - [[projects/Important Project 1]] + [[projects/Important Project 1]] To jump from that file back to the index file, use this link: > - [[../index]] + [[../index]] or: > - [[/index]] + [[/index]] The latter works because wiki links starting with "/" are considered to be absolute to the wiki root directory, that is, the link [[/index]] always opens the file /path/to/your/wiki/index.wiki, no matter in which subdirectory you are currently in. +If you want an absolute path in your local box you can prefix the path +with // > + [[//absolute_path]] +For example: > + [[///tmp/in_root_tmp]] + [[//~/in_home_dir]] + [[//$HOME/in_home_dir]] + Links to subdirectories inside the wiki directory are also supported. They end with a "/": > [[a subdirectory/|Other files]] @@ -3636,6 +3644,7 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from https://github.com/vimwiki-backup/vimwiki/issues. New:~ + * PR #811: Feature: Added handling of absolute path to vimwiki (with //) * PR #919: Fix duplicate helptag * PR #907: Cycle bullets * PR #900: conceallevel is now setted locally for vimwiki buffers diff --git a/test/link_creation.vader b/test/link_creation.vader index 0970702..62ecd1a 100644 --- a/test/link_creation.vader +++ b/test/link_creation.vader @@ -1,8 +1,68 @@ Include: vader_includes/vader_setup.vader +# Absolute links {{{1 +#################### + +Execute (Log): + Log 'Absolute links' + +# For markdown {{{2 +# ------------------ + +Execute (Set filename wiki_test.md): + Log '>> Absolute link, markdown syntax' + file wiki_test.md + call SetSyntax('markdown') + +Given vimwiki(some wiki link): + [test1](//$HOME/in_home1) + [test2](//~/in_home2) + [test3](///tmp/in_tmp) + +Do (Check in_home1): + \ + :AssertEqual expand('%'), $HOME.'/in_home1.md'\ + +Do (Check in_home2): + j\ + :AssertEqual expand('%'), $HOME.'/in_home2.md'\ + +Do (Check in_tmp): + jj\ + :AssertEqual expand('%'), '/tmp/in_tmp.md'\ + +# For Wiki {{{2 +# ------------------ + +Execute (Set filename wiki_test.wiki): + Log '>> Absolute link, wiki syntax' + file wiki_test.wiki + call SetSyntax('default') + +Given vimwiki(some wiki link): + [[//$HOME/in_home1]] + [[//~/in_home2]] + [[///tmp/in_tmp]] + +Do (Check in_home1): + \ + :AssertEqual expand('%'), $HOME.'/in_home1.wiki'\ + +Do (Check in_home2): + j\ + :AssertEqual expand('%'), $HOME.'/in_home2.wiki'\ + +Do (Check in_tmp): + jj\ + :AssertEqual expand('%'), '/tmp/in_tmp.wiki'\ + + # Link with dot {{{1 #################### +Execute (Log): + Log 'Link with dot' + Given vimwiki (filnames with dots): part1.part2.part3 part1.part2.part3.md @@ -39,6 +99,9 @@ Do (j): # Rest {{{1 ########################## +Execute (Log): + Log 'And more' + Given vimwiki (Text that is not a wikilink): test www.google.com