Make normalized links in diary pages use relative path.

A previous PR added this feature to the default syntax. This add the
same functionality to markdown syntax. Fixes #729
This commit is contained in:
Rane Brown 2019-07-10 21:06:33 -06:00
parent b997e687c3
commit c58268df3f
2 changed files with 38 additions and 20 deletions

View File

@ -386,7 +386,7 @@ function! vimwiki#base#generate_links(create)
let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' ' let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' '
for link in links for link in links
let link_infos = vimwiki#base#resolve_link(link) let link_infos = vimwiki#base#resolve_link(link)
if !s:is_diary_file(link_infos.filename) if !vimwiki#base#is_diary_file(link_infos.filename)
if vimwiki#vars#get_wikilocal('syntax') == 'markdown' if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template') let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template')
else else
@ -2080,7 +2080,7 @@ function! s:clean_url(url)
endfunction endfunction
function! s:is_diary_file(filename) function! vimwiki#base#is_diary_file(filename)
let file_path = vimwiki#path#path_norm(a:filename) let file_path = vimwiki#path#path_norm(a:filename)
let rel_path = vimwiki#vars#get_wikilocal('diary_rel_path') let rel_path = vimwiki#vars#get_wikilocal('diary_rel_path')
let diary_path = vimwiki#path#path_norm(vimwiki#vars#get_wikilocal('path') . rel_path) let diary_path = vimwiki#path#path_norm(vimwiki#vars#get_wikilocal('path') . rel_path)
@ -2107,26 +2107,31 @@ function! vimwiki#base#normalize_imagelink_helper(str, rxUrl, rxDesc, rxStyle, t
return lnk return lnk
endfunction endfunction
function! vimwiki#base#normalize_link_in_diary(lnk)
function! s:normalize_link_in_diary(lnk) let sc = vimwiki#vars#get_wikilocal('links_space_char')
let link = a:lnk . vimwiki#vars#get_wikilocal('ext') let link = a:lnk . vimwiki#vars#get_wikilocal('ext')
let link_wiki = vimwiki#vars#get_wikilocal('path') . '/' . link let link_wiki = substitute(vimwiki#vars#get_wikilocal('path') . '/' . link, '\s', sc, 'g')
let link_diary = vimwiki#vars#get_wikilocal('path') . '/' let link_diary = substitute(vimwiki#vars#get_wikilocal('path') . '/'
\ . vimwiki#vars#get_wikilocal('diary_rel_path') . '/' . link \ . vimwiki#vars#get_wikilocal('diary_rel_path') . '/' . link, '\s', sc, 'g')
let link_exists_in_diary = filereadable(link_diary) let link_exists_in_diary = filereadable(link_diary)
let link_exists_in_wiki = filereadable(link_wiki) let link_exists_in_wiki = filereadable(link_wiki)
let link_is_date = a:lnk =~# '\d\d\d\d-\d\d-\d\d' let link_is_date = a:lnk =~# '\d\d\d\d-\d\d-\d\d'
if link_exists_in_diary || link_is_date if link_is_date
let str = a:lnk
let rxUrl = vimwiki#vars#get_global('rxWord')
let rxDesc = '\d\d\d\d-\d\d-\d\d'
let template = vimwiki#vars#get_global('WikiLinkTemplate1')
elseif link_exists_in_diary
let str = a:lnk let str = a:lnk
let rxUrl = vimwiki#vars#get_global('rxWord') let rxUrl = vimwiki#vars#get_global('rxWord')
let rxDesc = '' let rxDesc = ''
let template = vimwiki#vars#get_global('WikiLinkTemplate1') let template = vimwiki#vars#get_global('WikiLinkTemplate1')
elseif link_exists_in_wiki elseif link_exists_in_wiki
let depth = len(split(vimwiki#vars#get_wikilocal('diary_rel_path'), '/')) let depth = len(split(vimwiki#vars#get_wikilocal('diary_rel_path'), '/'))
let str = repeat('../', depth) . a:lnk . '|' . a:lnk let str = repeat('../', depth) . a:lnk
let rxUrl = '^.*\ze|' let rxUrl = '.*'
let rxDesc = '|\zs.*$' let rxDesc = '[^/]*$'
let template = vimwiki#vars#get_global('WikiLinkTemplate2') let template = vimwiki#vars#get_global('WikiLinkTemplate2')
else else
let str = a:lnk let str = a:lnk
@ -2135,6 +2140,10 @@ function! s:normalize_link_in_diary(lnk)
let template = vimwiki#vars#get_global('WikiLinkTemplate1') let template = vimwiki#vars#get_global('WikiLinkTemplate1')
endif endif
if vimwiki#vars#get_wikilocal('syntax') ==? 'markdown'
let template = vimwiki#vars#get_syntaxlocal('Weblink1Template')
endif
return vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) return vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template)
endfunction endfunction
@ -2173,8 +2182,8 @@ function! s:normalize_link_syntax_n()
" normalize_link_syntax_v " normalize_link_syntax_v
let lnk = vimwiki#base#matchstr_at_cursor(vimwiki#vars#get_global('rxWord')) let lnk = vimwiki#base#matchstr_at_cursor(vimwiki#vars#get_global('rxWord'))
if !empty(lnk) if !empty(lnk)
if s:is_diary_file(expand("%:p")) if vimwiki#base#is_diary_file(expand("%:p"))
let sub = s:normalize_link_in_diary(lnk) let sub = vimwiki#base#normalize_link_in_diary(lnk)
else else
let sub = s:safesubstitute( let sub = s:safesubstitute(
\ vimwiki#vars#get_global('WikiLinkTemplate1'), '__LinkUrl__', lnk, '') \ vimwiki#vars#get_global('WikiLinkTemplate1'), '__LinkUrl__', lnk, '')
@ -2197,8 +2206,8 @@ function! s:normalize_link_syntax_v()
normal! gv""y normal! gv""y
" Set substitution " Set substitution
if s:is_diary_file(expand("%:p")) if vimwiki#base#is_diary_file(expand("%:p"))
let sub = s:normalize_link_in_diary(@") let sub = vimwiki#base#normalize_link_in_diary(@")
else else
let sub = s:safesubstitute(vimwiki#vars#get_global('WikiLinkTemplate1'), let sub = s:safesubstitute(vimwiki#vars#get_global('WikiLinkTemplate1'),
\ '__LinkUrl__', @", '') \ '__LinkUrl__', @", '')

View File

@ -97,9 +97,13 @@ function! s:normalize_link_syntax_n()
" normalize_link_syntax_v " normalize_link_syntax_v
let lnk = vimwiki#base#matchstr_at_cursor(vimwiki#vars#get_global('rxWord')) let lnk = vimwiki#base#matchstr_at_cursor(vimwiki#vars#get_global('rxWord'))
if !empty(lnk) if !empty(lnk)
let sub = vimwiki#base#normalize_link_helper(lnk, if vimwiki#base#is_diary_file(expand("%:p"))
\ vimwiki#vars#get_global('rxWord'), '', let sub = vimwiki#base#normalize_link_in_diary(lnk)
\ vimwiki#vars#get_syntaxlocal('Weblink1Template')) else
let sub = vimwiki#base#normalize_link_helper(lnk,
\ vimwiki#vars#get_global('rxWord'), '',
\ vimwiki#vars#get_syntaxlocal('Weblink1Template'))
endif
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub) call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
return return
endif endif
@ -118,8 +122,13 @@ function! s:normalize_link_syntax_v()
try try
norm! gvy norm! gvy
let visual_selection = @" let visual_selection = @"
let link = s:safesubstitute(vimwiki#vars#get_syntaxlocal('Weblink1Template'),
\ '__LinkUrl__', visual_selection, '') if vimwiki#base#is_diary_file(expand('%:p'))
let link = vimwiki#base#normalize_link_in_diary(visual_selection)
else
let link = s:safesubstitute(vimwiki#vars#get_syntaxlocal('Weblink1Template'),
\ '__LinkUrl__', visual_selection, '')
endif
" replace spaces with new character if option is set " replace spaces with new character if option is set
let link = substitute(link, '\s', vimwiki#vars#get_wikilocal('links_space_char'), 'g') let link = substitute(link, '\s', vimwiki#vars#get_wikilocal('links_space_char'), 'g')