From b4e0f738a32e0dbfdad6c74e22721d1872932ef8 Mon Sep 17 00:00:00 2001 From: Rane Brown Date: Mon, 30 Dec 2019 10:08:28 -0700 Subject: [PATCH] Allow markdown syntax to have tags at top of file. Previously tags that did not fall under a header caused errors because the generation code expected an anchor. Update tags test case. Closes #790 --- autoload/vimwiki/tags.vim | 18 +++--- test/command_generate_tags.vader | 95 +++++++++++++++++++------------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/autoload/vimwiki/tags.vim b/autoload/vimwiki/tags.vim index 240ec9c..c9a1471 100644 --- a/autoload/vimwiki/tags.vim +++ b/autoload/vimwiki/tags.vim @@ -354,16 +354,18 @@ function! vimwiki#tags#generate_tags(create, ...) abort let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink3Template') let link_infos = vimwiki#base#resolve_link(taglink) if empty(link_infos.anchor) - echom 'Vimwiki Error: Tags must appear after a header.' - return [] + let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template') + let entry = s:safesubstitute(link_tpl, '__LinkUrl__', taglink, '') + let entry = s:safesubstitute(entry, '__LinkDescription__', taglink, '') + else + let link_caption = split(link_infos.anchor, '#', 0)[-1] + let link_text = split(taglink, '#', 1)[0] + let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link_text, '') + let entry = s:safesubstitute(entry, '__LinkAnchor__', link_infos.anchor, '') + let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '') endif - let link_caption = split(link_infos.anchor, '#', 0)[-1] - let link_text = split(taglink, '#', 1)[0] - let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link_text, '') - let entry = s:safesubstitute(entry, '__LinkAnchor__', link_infos.anchor, '') - let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '') - call add(lines, bullet.entry) + call add(lines, bullet . entry) else let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1') call add(lines, bullet . substitute(link_tpl, '__LinkUrl__', taglink, '')) diff --git a/test/command_generate_tags.vader b/test/command_generate_tags.vader index 5d7459a..18e8ae7 100644 --- a/test/command_generate_tags.vader +++ b/test/command_generate_tags.vader @@ -1,67 +1,90 @@ Include: vader_includes/vader_setup.vader - Execute (Copy Wiki's Resources): Log "Start: Copy Resources" call CopyResources() - -# 1 VimwikiRebuildTags -###################### - -Execute (New Command): - Log "1 Testing VimwikiRebuildTags" +Execute (Setup): set sw=4 AssertEqual 4, &sw - Execute (Edit Test-Tag.md): edit $HOME/testmarkdown/Test-Tag.md AssertEqual $HOME . '/testmarkdown/Test-Tag.md', expand('%') AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax') AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr') -Do (Add tag <- with `Do` trick to save file): +Do (Create File Content): :edit $HOME/testmarkdown/Test-Tag.md\ - :call append(0, ":test-tag:")\ - :set bt=\ - :write %\ + I + :top-tag:\ + \ + # A header\ + \ + :test-tag:\ + \ + # Another header\ + \ + Words here.\ + If tag isn't within 2 lines of header then it has a direct link instead of\ + a link to the header.\ + \ + :second-tag: + \ + :write\ :VimwikiRebuildTags\ -Execute (Edit .vimwiki_tags): +Execute (Edit tags file): edit $HOME/testmarkdown/.vimwiki_tags AssertEqual $HOME . '/testmarkdown/.vimwiki_tags', expand('%') AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax') AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr') -# Carefull, following 2 lines have tabs: `this is the way` -Expect (Tag file with test-tag): - !_TAG_FILE_SORTED 1 - test-tag Test-Tag.md 1;" vimwiki:Test-Tag\tTest-Tag +# Note: tags file uses tabs +Expect (Correctly formatted tags file): + !_TAG_FILE_FORMAT 2 + !_TAG_FILE_SORTED 1 + !_TAG_OUTPUT_MODE vimwiki-tags + !_TAG_PROGRAM_AUTHOR Vimwiki + !_TAG_PROGRAM_NAME Vimwiki Tags + !_TAG_PROGRAM_URL https://github.com/vimwiki/vimwiki + !_TAG_PROGRAM_VERSION 2.4.1 + second-tag Test-Tag.md 13;" vimwiki:Test-Tag\tTest-Tag#second-tag + test-tag Test-Tag.md 5;" vimwiki:Test-Tag\tTest-Tag#A header + top-tag Test-Tag.md 1;" vimwiki:Test-Tag\tTest-Tag +Execute (Generate tags): + edit $HOME/testmarkdown/Test-Tag.md + VimwikiGenerateTags -# 2 VimwikiGenerateTags -######################### +Expect (Correctly generated tags section): + :top-tag: -Execute (New Command): - Log "2 Testing VimwikiGenerateTags TODO" - set sw=4 - AssertEqual 4, &sw + # A header -Given (Void): + :test-tag: -Do (Edit Test-Tag && GenerateTags): - :edit $HOME/testmarkdown/Test-Tag.md\ - :call append(0, ':Tag:')\ - :call append(0, '')\ - :call append(0, 'Test Here')\ - :call append(0, '')\ - :call append(0, '# A header')\ - :call WriteMe()\ - :VimwikiRebuildTags\ - :VimwikiGenerateTags\ - :Log "TODO give the expect block when VimwikigenerateTags is working"\ + # Another header + Words here. + If tag isn't within 2 lines of header then it has a direct link instead of + a link to the header. + + :second-tag: + + # Generated Tags + + ## second-tag + + - [second-tag](Test-Tag#second-tag) + + ## test-tag + + - [A header](Test-Tag#A header) + + ## top-tag + + - [Test-Tag](Test-Tag) Execute (Clean Test-Tag and .vimwiki_tags): Log "End: Clean" @@ -70,6 +93,4 @@ Execute (Clean Test-Tag and .vimwiki_tags): call system("rm $HOME/testmarkdown/Test-Tag.md") call DeleteHiddenBuffers() - Include: vader_includes/vader_teardown.vader -# vim: sw=2 foldmethod=indent foldlevel=30 foldignore=