From 3bf03d9300618156f70a8cd5d4b9f222e8e98ed5 Mon Sep 17 00:00:00 2001 From: Nick Wynja Date: Mon, 19 Apr 2021 13:00:09 -0400 Subject: [PATCH] feat: add graph search to check for broken links --- autoload/wiki/buffer.vim | 3 +++ autoload/wiki/graph.vim | 34 ++++++++++++++++++++++++++++++++++ doc/wiki.txt | 5 +++++ 3 files changed, 42 insertions(+) diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 89d0e2b3..0be9929a 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -39,6 +39,7 @@ endfunction function! s:init_buffer_commands() abort " {{{1 command! -buffer WikiCodeRun call wiki#u#run_code_snippet() command! -buffer WikiGraphFindBacklinks call wiki#graph#find_backlinks() + command! -buffer WikiGraphCheckLinks call wiki#graph#check_links() command! -buffer -count=99 WikiGraphIn call wiki#graph#in() command! -buffer -count=99 WikiGraphOut call wiki#graph#out() command! -buffer WikiJournalIndex call wiki#journal#make_index() @@ -81,6 +82,7 @@ endfunction function! s:init_buffer_mappings() abort " {{{1 nnoremap (wiki-code-run) :WikiCodeRun nnoremap (wiki-graph-find-backlinks) :WikiGraphFindBacklinks + nnoremap (wiki-graph-check-links) :WikiGraphCheckLinks nnoremap (wiki-graph-in) :WikiGraphIn nnoremap (wiki-graph-out) :WikiGraphOut nnoremap (wiki-journal-index) :WikiJournalIndex @@ -141,6 +143,7 @@ function! s:init_buffer_mappings() abort " {{{1 let l:mappings = { \ '(wiki-code-run)' : 'wc', \ '(wiki-graph-find-backlinks)' : 'wb', + \ '(wiki-graph-check-links)' : 'wlc', \ '(wiki-graph-in)' : 'wg', \ '(wiki-graph-out)' : 'wG', \ '(wiki-link-next)' : '', diff --git a/autoload/wiki/graph.vim b/autoload/wiki/graph.vim index 6eaae3ff..7e6ec1fc 100644 --- a/autoload/wiki/graph.vim +++ b/autoload/wiki/graph.vim @@ -27,6 +27,40 @@ endfunction "}}}1 +function! wiki#graph#check_links() abort "{{{1 + redraw + call wiki#log#info('Scanning wiki for broken links ... ') + sleep 25m + + let l:broken_links = [] + + for l:file in globpath(b:wiki.root, '**/*.' . b:wiki.extension, 0, 1) + for l:link in filter(wiki#link#get_all(l:file), + \ 'get(v:val, ''scheme'', '''') ==# ''wiki''') + if !filereadable(l:link.path) + call add(l:broken_links, { + \ 'filename' : l:file, + \ 'text' : get(l:link, 'full'), + \ 'anchor' : l:link.anchor, + \ 'lnum' : l:link.lnum, + \ 'col' : l:link.c1 + \}) + endif + endfor + endfor + + call wiki#log#info('Done!') + + if empty(l:broken_links) + call wiki#log#info('No broken links exist') + else + call setloclist(0, l:broken_links, 'r') + lopen + endif +endfunction + +"}}}1 + function! wiki#graph#out(...) abort " {{{1 let l:max_level = a:0 > 0 ? a:1 : -1 diff --git a/doc/wiki.txt b/doc/wiki.txt index 17928caa..396e99bf 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -575,6 +575,10 @@ the commands are also available as mappings of the form `(wiki-[name])`. *WikiGraphFindBacklinks* Find backlinks to current page. +*(wiki-graph-check-links)* +*WikiGraphCheckLinks* + Check for broken links. + *(wiki-graph-in)* *(wiki-graph-out)* *WikiGraphIn* @@ -829,6 +833,7 @@ information about the different modes. `n` wx |(wiki-reload)| [GLOBAL] `n` wc |(wiki-code-run)| `n` wb |(wiki-graph-find-backlinks)| + `n` wlc |(wiki-graph-check-links)| `n` wg |(wiki-graph-in)| `n` wG |(wiki-graph-out)| `n` wf |(wiki-link-toggle)|