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 ===
This commit is contained in:
Tinmarino 2020-08-12 21:39:13 -04:00
parent 212cd8e09b
commit 22d9d012ba
7 changed files with 111 additions and 37 deletions

View File

@ -716,18 +716,21 @@ endfunction
" Return: anchor <string> => link in TOC " Return: anchor <string> => link in TOC
" Called: vimwiki#base#table_of_contents " Called: vimwiki#base#table_of_contents
function! vimwiki#base#normalize_anchor(anchor, ...) abort function! vimwiki#base#normalize_anchor(anchor, ...) abort
" Note: See unormalize " A Trim space
" 0 Get in let anchor = vimwiki#u#trim(a:anchor)
let anchor = 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 if a:0
let previous_anchors = a:1 let previous_anchors = a:1
else else
let previous_anchors = {} let previous_anchors = {}
endif endif
" A Trim space
let anchor = vimwiki#u#trim(anchor)
" 1 Downcase the string " 1 Downcase the string
let anchor = tolower(anchor) let anchor = tolower(anchor)
@ -766,7 +769,14 @@ function! vimwiki#base#unnormalize_anchor(anchor) abort
" Link: Inspired from https://gist.github.com/asabaylus/3071099 " Link: Inspired from https://gist.github.com/asabaylus/3071099
" Issue: #664 => Points to all others " 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() let punctuation_rx = s:get_punctuaction_regex()
" Permit url part of link: '](www.i.did.it.my.way.cl)' " Permit url part of link: '](www.i.did.it.my.way.cl)'
let link_rx = '\%(\]([^)]*)\)' let link_rx = '\%(\]([^)]*)\)'
@ -2261,7 +2271,7 @@ function! s:collect_headers() abort
" Check SetExt Header " Check SetExt Header
" TODO mutualise SetExt line (for consistency) " TODO mutualise SetExt line (for consistency)
" TODO replace regex with =\+ or -\+ " 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_level = stridx(line_content, '=') != -1 ? 1 : 2
let header_text = getline(lnum-1) let header_text = getline(lnum-1)
" Maybe ATX header " Maybe ATX header
@ -2275,7 +2285,7 @@ function! s:collect_headers() abort
\ && stridx(line_content, vimwiki#vars#get_syntaxlocal('rxH')) > 0 \ && stridx(line_content, vimwiki#vars#get_syntaxlocal('rxH')) > 0
continue continue
endif endif
" Get header level && text " Get header level
let header_level = vimwiki#u#count_first_sym(line_content) let header_level = vimwiki#u#count_first_sym(line_content)
let header_text = matchstr(line_content, rxHeader) let header_text = matchstr(line_content, rxHeader)
endif 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] 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 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 = '' let h_complete_id = ''
if vimwiki#vars#get_wikilocal('toc_link_format') == 1 if vimwiki#vars#get_wikilocal('toc_link_format') == 1
for l in range(h_level-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 " Keep previous anchor => if redundant => add suffix -2
let previous_anchors = {} let previous_anchors = {}
for [lvl, anchor, desc] in complete_header_infos for [lvl, anchor, desc] in complete_header_infos
" [DESC](URL)
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink2Template') let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink2Template')
" [[URL]]
elseif vimwiki#vars#get_wikilocal('toc_link_format') == 1 elseif vimwiki#vars#get_wikilocal('toc_link_format') == 1
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2') let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2')
" [[URL|DESC]]
else else
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1') let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
endif endif

View File

@ -170,6 +170,7 @@ function! vimwiki#u#count_first_sym(line) abort
endfunction endfunction
" Escape string for literal magic regex match
function! vimwiki#u#escape(string) abort function! vimwiki#u#escape(string) abort
return escape(a:string, '~.*[]\^$') return escape(a:string, '~.*[]\^$')
endfunction endfunction

View File

@ -2790,10 +2790,10 @@ The format of the links in the Table of Contents (see |vimwiki-toc|).
Value Description~ Value Description~
0 Brief: The link contains only the URL. URL references only 0 Extended: The link contains the description and URL. URL
the immediate level.
1 Extended: The link contains the description and URL. URL
references all levels. references all levels.
1 Brief: The link contains only the URL. URL references only
the immediate level.
Default: 0 Default: 0
@ -3798,7 +3798,6 @@ New:~
Changed:~ Changed:~
* VimwikiCheckLinks work on current wiki or on range * VimwikiCheckLinks work on current wiki or on range
* |g:vimwiki_toc_link_format| == 0 (default) means old behavior: short links
Removed:~ Removed:~

View File

@ -7,6 +7,12 @@
# Otherwise the bash script is freezing # Otherwise the bash script is freezing
### Wiki {{{1
###############
### Markdown {{{1
###############
Given vimwiki (a): Given vimwiki (a):

View File

@ -6,29 +6,80 @@
# TODO (10min) test if g:vimwiki_to_header well readen # TODO (10min) test if g:vimwiki_to_header well readen
# TODO (10min) test vimviki_toc_link_format # TODO (10min) test vimviki_toc_link_format
# TODO (1h) test if really wiki dependant (for 2 diffrent wikis) # 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 # TODO if link in heading
#Given vimwiki (Two same heading {{{1):
# # Pre [link](anything no parenthesis) Post Given vimwiki (Wiki with spaces {{{1):
# = h1 h2 h3 h4 =
#Execute (Set syntax markdown && Set sw=8):
# call SetSyntax('markdown') Execute (Toc and enter (alpha)):
# set sw=8 call SetSyntax('default')
# VimwikiTOC call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr'))
# AssertEqual -1, vimwiki#vars#get_wikilocal('list_margin')
#Expect (Suffix -1 and -2): VimwikiTOC
#
# Expect (Toc alpha):
# = Contents =
# - [[#h1 h2 h3 h4]]
# Start {{{1
= 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): Given vimwiki (One heading: May delete last line (#910) {{{1):
# Basic-title # Basic-title

View File

@ -37,6 +37,7 @@ Expect (Links with default margin):
Execute (Set list margin == 2): Execute (Set list margin == 2):
call vimwiki#vars#set_wikilocal('list_margin', 2, vimwiki#vars#get_bufferlocal('wiki_nr')) call vimwiki#vars#set_wikilocal('list_margin', 2, vimwiki#vars#get_bufferlocal('wiki_nr'))
VimwikiGenerateLinks VimwikiGenerateLinks
call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr'))
Expect (Links with margin == 2): Expect (Links with margin == 2):
@ -52,6 +53,7 @@ Execute (Set syntax media):
Execute (Generate Links): Execute (Generate Links):
VimwikiGenerateLinks VimwikiGenerateLinks
call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr'))
Expect (Links with default margin): Expect (Links with default margin):
@ -64,6 +66,7 @@ Expect (Links with default margin):
Execute (Set list margin == 1): Execute (Set list margin == 1):
call vimwiki#vars#set_wikilocal('list_margin', 1, vimwiki#vars#get_bufferlocal('wiki_nr')) call vimwiki#vars#set_wikilocal('list_margin', 1, vimwiki#vars#get_bufferlocal('wiki_nr'))
VimwikiGenerateLinks VimwikiGenerateLinks
call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr'))
Expect (Links with margin == 1): Expect (Links with margin == 1):
@ -92,6 +95,7 @@ Expect (Links with default margin):
Execute (Set list margin == 5): Execute (Set list margin == 5):
call vimwiki#vars#set_wikilocal('list_margin', 5, vimwiki#vars#get_bufferlocal('wiki_nr')) call vimwiki#vars#set_wikilocal('list_margin', 5, vimwiki#vars#get_bufferlocal('wiki_nr'))
VimwikiGenerateLinks VimwikiGenerateLinks
call vimwiki#vars#set_wikilocal('list_margin', -1, vimwiki#vars#get_bufferlocal('wiki_nr'))
Expect (Links with margin == 5): Expect (Links with margin == 5):