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
This commit is contained in:
Rajdeep 2020-01-26 20:39:03 -05:00 committed by Tinmarino
parent 8cf2789840
commit e3e841f335
3 changed files with 91 additions and 10 deletions

View File

@ -131,7 +131,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort
let source_file = vimwiki#path#current_wiki_file() let source_file = vimwiki#path#current_wiki_file()
endif 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 '\' " other style links don't allow '\'
let link_text = substitute(a:link_text, '\(\\\)\(\W\)\@=', '', 'g') 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) let is_wiki_link = s:is_wiki_link(link_infos)
" extract anchor " Extract anchor
if is_wiki_link if is_wiki_link
let split_lnk = split(link_text, '#', 1) let split_lnk = split(link_text, '#', 1)
let link_text = split_lnk[0] let link_text = split_lnk[0]
@ -181,11 +181,18 @@ function! vimwiki#base#resolve_link(link_text, ...) abort
endif endif
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 is_wiki_link && link_text[0] ==# '/'
if link_text !=# '/' if link_text !=# '/'
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:] let link_text = link_text[1:]
endif endif
endif
let is_relative = 0 let is_relative = 0
elseif !is_wiki_link && vimwiki#path#is_absolute(link_text) elseif !is_wiki_link && vimwiki#path#is_absolute(link_text)
let is_relative = 0 let is_relative = 0
@ -195,7 +202,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort
endif endif
" extract the other items depending on the scheme " Extract the other items depending on the scheme
if link_infos.scheme =~# '\mwiki\d\+' if link_infos.scheme =~# '\mwiki\d\+'
" interwiki link named wiki 'wn.name:link' format " interwiki link named wiki 'wn.name:link' format
@ -227,8 +234,9 @@ function! vimwiki#base#resolve_link(link_text, ...) abort
return link_infos return link_infos
endif endif
endif endif
if is_absolute
if !is_relative || link_infos.index != source_wiki let root_dir = ''
elseif !is_relative || link_infos.index != source_wiki
let root_dir = vimwiki#vars#get_wikilocal('path', link_infos.index) let root_dir = vimwiki#vars#get_wikilocal('path', link_infos.index)
endif endif
@ -265,6 +273,7 @@ function! vimwiki#base#resolve_link(link_text, ...) abort
endif endif
let link_infos.filename = vimwiki#path#normalize(link_infos.filename) let link_infos.filename = vimwiki#path#normalize(link_infos.filename)
return link_infos return link_infos
endfunction endfunction

View File

@ -1019,6 +1019,14 @@ 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 the file /path/to/your/wiki/index.wiki, no matter in which subdirectory you
are currently in. 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 Links to subdirectories inside the wiki directory are also supported. They
end with a "/": > end with a "/": >
[[a subdirectory/|Other files]] [[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. https://github.com/vimwiki-backup/vimwiki/issues.
New:~ New:~
* PR #811: Feature: Added handling of absolute path to vimwiki (with //)
* PR #919: Fix duplicate helptag * PR #919: Fix duplicate helptag
* PR #907: Cycle bullets * PR #907: Cycle bullets
* PR #900: conceallevel is now setted locally for vimwiki buffers * PR #900: conceallevel is now setted locally for vimwiki buffers

View File

@ -1,8 +1,68 @@
Include: vader_includes/vader_setup.vader 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):
\<Cr>
:AssertEqual expand('%'), $HOME.'/in_home1.md'\<Cr>
Do (Check in_home2):
j\<Cr>
:AssertEqual expand('%'), $HOME.'/in_home2.md'\<Cr>
Do (Check in_tmp):
jj\<Cr>
:AssertEqual expand('%'), '/tmp/in_tmp.md'\<Cr>
# 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):
\<Cr>
:AssertEqual expand('%'), $HOME.'/in_home1.wiki'\<Cr>
Do (Check in_home2):
j\<Cr>
:AssertEqual expand('%'), $HOME.'/in_home2.wiki'\<Cr>
Do (Check in_tmp):
jj\<Cr>
:AssertEqual expand('%'), '/tmp/in_tmp.wiki'\<Cr>
# Link with dot {{{1 # Link with dot {{{1
#################### ####################
Execute (Log):
Log 'Link with dot'
Given vimwiki (filnames with dots): Given vimwiki (filnames with dots):
part1.part2.part3 part1.part2.part3
part1.part2.part3.md part1.part2.part3.md
@ -39,6 +99,9 @@ Do (j<Cr><Cr>):
# Rest {{{1 # Rest {{{1
########################## ##########################
Execute (Log):
Log 'And more'
Given vimwiki (Text that is not a wikilink): Given vimwiki (Text that is not a wikilink):
test test
www.google.com www.google.com