From 22d9d012bae4bcfc64776ebef7111c6e51646d2c Mon Sep 17 00:00:00 2001 From: Tinmarino Date: Wed, 12 Aug 2020 21:39:13 -0400 Subject: [PATCH] Fix: Broken TOC links format when using default syntax (Issue #981) - Not normalising for default - SetExt header missed a `^` in the regex so mateched the trailing === of === head3 === --- .travis.yml | 2 +- autoload/vimwiki/base.vim | 33 +++++++++----- autoload/vimwiki/u.vim | 1 + doc/vimwiki.txt | 7 ++- test/link_anchor.vader | 6 +++ test/link_toc.vader | 95 ++++++++++++++++++++++++++++++--------- test/list_margin.vader | 4 ++ 7 files changed, 111 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index 50b252e..be3e489 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ env: # Define jobs <- vim version <- hard copied from Dockerfile # First to be launched - VIM_VERSION=vint - - VIM_VERSION=vim_7.3.429 + - VIM_VERSION=vim_7.3.429 - VIM_VERSION=nvim_0.3.8 - VIM_VERSION=vim_8.1.0519 # More diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 5a713d4..da48b76 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -716,18 +716,21 @@ endfunction " Return: anchor => link in TOC " Called: vimwiki#base#table_of_contents function! vimwiki#base#normalize_anchor(anchor, ...) abort - " Note: See unormalize - " 0 Get in - let anchor = a:anchor + " A Trim space + let anchor = vimwiki#u#trim(a:anchor) + + " Guard: work only for markdown + if vimwiki#vars#get_wikilocal('syntax') !=# 'markdown' + return anchor + endif + + " Keep previous anchors cache: See unormalize if a:0 let previous_anchors = a:1 else let previous_anchors = {} endif - " A Trim space - let anchor = vimwiki#u#trim(anchor) - " 1 Downcase the string let anchor = tolower(anchor) @@ -766,7 +769,14 @@ function! vimwiki#base#unnormalize_anchor(anchor) abort " Link: Inspired from https://gist.github.com/asabaylus/3071099 " Issue: #664 => Points to all others - let anchor = a:anchor + " A Trim space + let anchor = vimwiki#u#trim(a:anchor) + + " Guard: work only for markdown + if vimwiki#vars#get_wikilocal('syntax') !=# 'markdown' + return [anchor, 1, ''] + endif + let punctuation_rx = s:get_punctuaction_regex() " Permit url part of link: '](www.i.did.it.my.way.cl)' let link_rx = '\%(\]([^)]*)\)' @@ -2261,7 +2271,7 @@ function! s:collect_headers() abort " Check SetExt Header " TODO mutualise SetExt line (for consistency) " TODO replace regex with =\+ or -\+ - if line_content =~# '\s\{0,3}[=-][=-]\+$' + if line_content =~# '^\s\{0,3}[=-][=-]\+\s*$' let header_level = stridx(line_content, '=') != -1 ? 1 : 2 let header_text = getline(lnum-1) " Maybe ATX header @@ -2275,7 +2285,7 @@ function! s:collect_headers() abort \ && stridx(line_content, vimwiki#vars#get_syntaxlocal('rxH')) > 0 continue endif - " Get header level && text + " Get header level let header_level = vimwiki#u#count_first_sym(line_content) let header_text = matchstr(line_content, rxHeader) endif @@ -2452,7 +2462,7 @@ function! vimwiki#base#table_of_contents(create) abort let headers_levels[h_level-1] = [h_text, headers_levels[h_level-1][1]+1] for idx in range(h_level, 5) | let headers_levels[idx] = ['', 0] | endfor - " Add description to the link if toc_link_format == 1 => extended + " Add parents header to format if toc_link_format == 0 => extended let h_complete_id = '' if vimwiki#vars#get_wikilocal('toc_link_format') == 1 for l in range(h_level-1) @@ -2476,10 +2486,13 @@ function! vimwiki#base#table_of_contents(create) abort " Keep previous anchor => if redundant => add suffix -2 let previous_anchors = {} for [lvl, anchor, desc] in complete_header_infos + " [DESC](URL) if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink2Template') + " [[URL]] elseif vimwiki#vars#get_wikilocal('toc_link_format') == 1 let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2') + " [[URL|DESC]] else let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1') endif diff --git a/autoload/vimwiki/u.vim b/autoload/vimwiki/u.vim index a32f64f..3180241 100644 --- a/autoload/vimwiki/u.vim +++ b/autoload/vimwiki/u.vim @@ -170,6 +170,7 @@ function! vimwiki#u#count_first_sym(line) abort endfunction +" Escape string for literal magic regex match function! vimwiki#u#escape(string) abort return escape(a:string, '~.*[]\^$') endfunction diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt index eda6482..0e1c85e 100644 --- a/doc/vimwiki.txt +++ b/doc/vimwiki.txt @@ -2790,10 +2790,10 @@ The format of the links in the Table of Contents (see |vimwiki-toc|). Value Description~ -0 Brief: The link contains only the URL. URL references only - the immediate level. -1 Extended: The link contains the description and URL. URL +0 Extended: The link contains the description and URL. URL references all levels. +1 Brief: The link contains only the URL. URL references only + the immediate level. Default: 0 @@ -3798,7 +3798,6 @@ New:~ Changed:~ * VimwikiCheckLinks work on current wiki or on range - * |g:vimwiki_toc_link_format| == 0 (default) means old behavior: short links Removed:~ diff --git a/test/link_anchor.vader b/test/link_anchor.vader index 2ff0784..d2e9267 100644 --- a/test/link_anchor.vader +++ b/test/link_anchor.vader @@ -7,6 +7,12 @@ # Otherwise the bash script is freezing +### Wiki {{{1 +############### + + +### Markdown {{{1 +############### Given vimwiki (a): diff --git a/test/link_toc.vader b/test/link_toc.vader index e6c4108..226b043 100644 --- a/test/link_toc.vader +++ b/test/link_toc.vader @@ -6,29 +6,80 @@ # TODO (10min) test if g:vimwiki_to_header well readen # TODO (10min) test vimviki_toc_link_format # TODO (1h) test if really wiki dependant (for 2 diffrent wikis) -# -# Doc: from #664: -# -- 1. It downcases the string => OK: from previous big collection -# -- 2. remove anything that is not a letter, number, space or hyphen (see the source for how Unicode is handled) => from 'bad characters' -# -- 3. changes any space to a hyphen => OK: from previous big -# -- 4. If that is not unique, add "-1", "-2", "-3",... to make it unique => TODO not implemented -# -# # TODO if link in heading -#Given vimwiki (Two same heading {{{1): -# # Pre [link](anything no parenthesis) Post -# -#Execute (Set syntax markdown && Set sw=8): -# call SetSyntax('markdown') -# set sw=8 -# VimwikiTOC -# -#Expect (Suffix -1 and -2): -# -# -# -# -# Start {{{1 + +Given vimwiki (Wiki with spaces {{{1): + = h1 h2 h3 h4 = + +Execute (Toc and enter (alpha)): + call SetSyntax('default') + call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr')) + AssertEqual -1, vimwiki#vars#get_wikilocal('list_margin') + VimwikiTOC + +Expect (Toc alpha): + = Contents = + - [[#h1 h2 h3 h4]] + + = h1 h2 h3 h4 = + +Given vimwiki (Wiki with bad characters {{{1): + = h!@$%^&() = + +Execute (Toc and beta): + call SetSyntax('default') + file wiki.wiki + VimwikiTOC + +Expect (Toc and jumpes well): + = Contents = + - [[#h!@$%^&()]] + + = h!@$%^&() = + + +Given vimwiki (One word headings (#981) {{{1): + = head1 = + == head2 == + === head3 === + +Execute (Wiki: toc_link_format=1 (to restore) VimwikiTOC x 1): + set sw=2 + let vimwiki_toc_link_format = 1 + call ReloadVimwiki() + call SetSyntax('default') + VimwikiTOC + + +Expect(Headinds TOC one word (1)): + = Contents = + - [[#head1|head1]] + - [[#head1#head2|head2]] + - [[#head1#head2#head3|head3]] + + = head1 = + == head2 == + === head3 === + +Execute (Wiki: toc_link_format=0 (restoring default) VimwikiTOC x 1): + let vimwiki_toc_link_format = 0 + call ReloadVimwiki() + call SetSyntax('default') + VimwikiTOC + +Expect(Headinds TOC one word (0)): + = Contents = + - [[#head1]] + - [[#head2]] + - [[#head3]] + + = head1 = + == head2 == + === head3 === + + +Execute (Clean wiki TOC): + Given vimwiki (One heading: May delete last line (#910) {{{1): # Basic-title diff --git a/test/list_margin.vader b/test/list_margin.vader index 39d12d2..5cb72b6 100644 --- a/test/list_margin.vader +++ b/test/list_margin.vader @@ -37,6 +37,7 @@ Expect (Links with default margin): Execute (Set list margin == 2): call vimwiki#vars#set_wikilocal('list_margin', 2, vimwiki#vars#get_bufferlocal('wiki_nr')) VimwikiGenerateLinks + call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr')) Expect (Links with margin == 2): @@ -52,6 +53,7 @@ Execute (Set syntax media): Execute (Generate Links): VimwikiGenerateLinks + call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr')) Expect (Links with default margin): @@ -64,6 +66,7 @@ Expect (Links with default margin): Execute (Set list margin == 1): call vimwiki#vars#set_wikilocal('list_margin', 1, vimwiki#vars#get_bufferlocal('wiki_nr')) VimwikiGenerateLinks + call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr')) Expect (Links with margin == 1): @@ -92,6 +95,7 @@ Expect (Links with default margin): Execute (Set list margin == 5): call vimwiki#vars#set_wikilocal('list_margin', 5, vimwiki#vars#get_bufferlocal('wiki_nr')) VimwikiGenerateLinks + call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr')) Expect (Links with margin == 5):