Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supporting relative path for EditUrlUnderCursor #533

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions test/map.vader
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand All @@ -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;
Expand Down