From 540380cb71ca98142628a43ec3de69d1dadc9833 Mon Sep 17 00:00:00 2001 From: Dia Pacifica Date: Tue, 1 Oct 2024 19:14:47 -0700 Subject: [PATCH] initial commit --- README.md | 40 ++++++++++++++++++++++++++++++++++ after/ftplugin/markdown.vim | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 README.md create mode 100644 after/ftplugin/markdown.vim diff --git a/README.md b/README.md new file mode 100644 index 0000000..33f2491 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# vim-markdown-navigation + +Enable navigation of in-document links in Vim editor for Markdown documents. + +This plugin is intended to work in conjunction with [vim-markdown](https://github.com/plasticboy/vim-markdown) but will work standalone as well. + + +## Installation + +Copy markdown.vim to ~/.vim/after/ftplugin/ + + +## Testing + +A useful document to test this with is [README.md with TOC](https://github.com/ekalinin/envirius/blob/24ea3be0d3cc03f4235fa4879bb33dc122d0ae29/README.md) by ekalinin + + +## Usage + +Simply move the cursor over a link and press *K* (shift-k). + + +## Recommended + +For convenient generation of tables of contents for Markdown documents the following are recommended: + +[vim-markdown-toc](https://github.com/mzlogin/vim-markdown-toc) - Vim plugin capable of generating tables of contents in GitHub, GitLab, and Redcarpet flavored markdown. + +[github-markdown-toc](https://github.com/ekalinin/github-markdown-toc.go) - Standalone program with many features that generates tables of contents. + + +## Authors + +User *novum* on freenode IRC channel #vim + + +## Credits + +All of the helpful people on the freenode #vim channel. + diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim new file mode 100644 index 0000000..5e30792 --- /dev/null +++ b/after/ftplugin/markdown.vim @@ -0,0 +1,43 @@ +" this script (markdown.vim) must be placed in ~/.vim/after/ftplugin +" requires https://github.com/plasticboy/vim-markdown + +function! s:markdownGotoHeader() + let pos = getpos('.') + call search('(', 'bcW', line('.')) + if !search('#', 'cW', line('.')) + echohl ErrorMsg + echo 'No link found on rest of line' + echohl NONE + call setpos('.', pos) + return + endif + let asav = @a + normal! "ayib + let targ = @a + let @a = asav + let targ = substitute(targ, '^#\+', '', '') + let targ = substitute(targ, '^\s\+\|\s\+$', '', 'g') + if empty(targ) + echohl ErrorMsg + echo 'target is empty' + echohl NONE + call setpos('.', pos) + return + endif + let targ = split(targ)[0] + let targ = '\[[:punct:][:space:]]\*'.join(map(split(targ, '-'), { _,str -> join(map(split(str, '\zs'), { _,chr -> chr == '\' ? '\\' : chr }), '\[[:punct:]]\*') }), '\[[:punct:][:space:]]\+').'\[[:punct:][:space:]]\*' + let pat = '\V\c\^\%(\s\*'.targ.'\n\[-=]\|#\+\s\*'.targ.'\)' + let lnr = search(pat, 'cnw') + if lnr == 0 + echohl ErrorMsg + echo 'target /'.targ.'/ not found' + echohl NONE + call setpos('.', pos) + return + endif + call setpos("''", pos) + call setpos('.', [bufnr(), lnr, 1, 1]) +endfunction + +nnoremap K :call markdownGotoHeader() +