diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index d6e51c21..8074043b 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -660,6 +660,24 @@ function! s:OpenUrlUnderCursor() endif endfunction +" More general way of creating file path +function! s:CreateFilePath(filepath, extension) + let l:filepath = substitute(a:filepath, '^./', '', '') + let l:current_path_list = split(expand('%:p:h'), '/', 1) + let l:file_path_list = split(l:filepath, '/', 1) + let l:backs_count = 0 + "whole for block can be replaced with matchfuzzy if it is supported + for i in l:file_path_list + if i == '..' + let l:backs_count += 1 + endif + endfor + "let l:backs = len(matchfuzzy(l:file_path_list, '..')) + let l:new_path = l:current_path_list[:-l:backs_count-1] + let l:trail = '/'.join(l:file_path_list[l:backs_count:], '/').a:extension + return fnameescape(join(l:new_path, '/').l:trail) +endfunction + " We need a definition guard because we invoke 'edit' which will reload this " script while this function is running. We must not replace it. if !exists('*s:EditUrlUnderCursor') @@ -692,7 +710,7 @@ if !exists('*s:EditUrlUnderCursor') let l:ext = '.md' endif endif - let l:url = fnameescape(fnamemodify(expand('%:h').'/'.l:url.l:ext, ':.')) + let l:url = s:CreateFilePath(l:url, l:ext) let l:editmethod = '' " determine how to open the linked file (split, tab, etc) if exists('g:vim_markdown_edit_url_in') diff --git a/test/map.vader b/test/map.vader index 7ecdfad1..15c7ec87 100644 --- a/test/map.vader +++ b/test/map.vader @@ -55,7 +55,7 @@ Given markdown; Execute (ge opens file): normal ge - AssertEqual @%, 'ge_test.md' + AssertEqual @%, expand('%:p:h').'/ge_test.md' AssertEqual getline(1), 'ge test' Given markdown; @@ -64,7 +64,7 @@ Given markdown; Execute (ge opens file without .md extensions): let g:vim_markdown_no_extensions_in_markdown = 1 normal ge - AssertEqual @%, 'ge_test.md' + AssertEqual @%, expand('%:p:h').'/ge_test.md' AssertEqual getline(1), 'ge test' unlet g:vim_markdown_no_extensions_in_markdown @@ -75,7 +75,7 @@ Execute (ge does not write before opening file): normal ia normal l normal ge - AssertEqual @%, 'ge_test.md' + AssertEqual @%, expand('%:p:h').'/ge_test.md' AssertEqual getline(1), 'ge test' Given markdown;