Feature: Markdown: Support SetExt Heading (Issue #209)

Like these
==========

See: https://spec.commonmark.org/0.29/#setext-headings

Note: work for follow_link and VimwikiTOC
This commit is contained in:
Tinmarino
2020-08-04 00:44:01 -04:00
parent 40f02293bf
commit fc056cfeca
8 changed files with 202 additions and 26 deletions

View File

@ -727,7 +727,7 @@ function! s:normalize_anchor(anchor, ...) abort
let anchor = substitute(anchor, punctuation_rx, '', 'g')
" 3 Change any space to a hyphen
let anchor = substitute(anchor, ' ', '-', 'g')
let anchor = substitute(anchor, ' \+', '-', 'g')
" 4 Append '-1', '-2', '-3',... to make it unique <= If that not unique
if has_key(previous_anchors, anchor)
@ -777,7 +777,7 @@ function! s:unnormalize_anchor(anchor) abort
for char in split(anchor, '\zs')
" 3 Change any space to a hyphen
if char ==# '-'
let anchor_loop .= '[ \-]'
let anchor_loop .= '[ \-]\+'
" 2 Remove anything that is not a letter, number, CJK character, hyphen or space
" -- So add puncutation regex at each char
else
@ -816,13 +816,13 @@ function! s:jump_to_anchor(anchor) abort
let anchor_header = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('header_match'),
\ '__Header__', segment_re, '')
\ '__Header__', segment_re, 'g')
let anchor_bold = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('bold_match'),
\ '__Text__', segment_re, '')
\ '__Text__', segment_re, 'g')
let anchor_tag = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('tag_match'),
\ '__Tag__', segment_re, '')
\ '__Tag__', segment_re, 'g')
" Go: Move cursor: maybe more than onces (see markdown suffix)
let success_nb = 0
@ -840,7 +840,10 @@ function! s:jump_to_anchor(anchor) abort
if success_nb < segment_nb-1 | let pos += 1 | endif
call cursor(pos, 1)
let success_nb += 1
else
" Do not move
" But maybe suffix -2 is not the segment number but the real header suffix
" TODO make this more robust
elseif i == 0
" Next segment (default syntax)
call setpos('.', oldpos)
let fail = 1
@ -1048,6 +1051,8 @@ function! vimwiki#base#edit_file(command, filename, anchor, ...) abort
call vimwiki#u#ft_set()
endif
endif
" Goto anchor
if a:anchor !=? ''
call s:jump_to_anchor(a:anchor)
endif
@ -2154,8 +2159,12 @@ endfunction
" Returns: all the headers in the current buffer as a list of the form
" [[line_number, header_level, header_text], [...], [...], ...]
function! s:collect_headers() abort
" Init loop variables
let is_inside_pre_or_math = 0 " 1: inside pre, 2: inside math, 0: outside
let headers = []
let rxHeader = vimwiki#vars#get_syntaxlocal('rxHeader')
" For all lines in file
for lnum in range(1, line('$'))
let line_content = getline(lnum)
if (is_inside_pre_or_math == 1 && line_content =~# vimwiki#vars#get_syntaxlocal('rxPreEnd')) ||
@ -2174,17 +2183,31 @@ function! s:collect_headers() abort
let is_inside_pre_or_math = 2
continue
endif
if line_content !~# vimwiki#vars#get_syntaxlocal('rxHeader')
continue
endif
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
if stridx(line_content, vimwiki#vars#get_syntaxlocal('rxH')) > 0
continue " markdown headers must start in the first column
" Check SetExt Header
" TODO mutualise SetExt line (for consistency)
" TODO replace regex with =\+ or -\+
if line_content =~# '\s\{0,3}[=-][=-]\+$'
let header_level = stridx(line_content, '=') != -1 ? 1 : 2
let header_text = getline(lnum-1)
" Maybe ATX header
else
" Clause: Must match rxHeader
if line_content !~# rxHeader
continue
endif
" Clause: markdown headers must start in the first column
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
\ && stridx(line_content, vimwiki#vars#get_syntaxlocal('rxH')) > 0
continue
endif
" Get header level && text
let header_level = vimwiki#u#count_first_sym(line_content)
let header_text = matchstr(line_content, rxHeader)
endif
let header_level = vimwiki#u#count_first_sym(line_content)
let header_text =
\ vimwiki#u#trim(matchstr(line_content, vimwiki#vars#get_syntaxlocal('rxHeader')))
" Clean && Append to res
let header_text = vimwiki#u#trim(header_text)
call add(headers, [lnum, header_level, header_text])
endfor
@ -2294,6 +2317,7 @@ endfunction
" a:create == 1: creates or updates TOC in current file
" a:create == 0: update if TOC exists
function! vimwiki#base#table_of_contents(create) abort
" Collect headers
let headers = s:collect_headers()
let toc_header_text = vimwiki#vars#get_wikilocal('toc_header')

View File

@ -638,6 +638,7 @@ function! vimwiki#vars#populate_syntax_vars(syntax) abort
\ '^\s*\('.header_symbol.'\{1,6}\)\zs[^'.header_symbol.'].*[^'.header_symbol.']\ze\1\s*$'
else
" asymmetric
" Note: For markdown rxH=# and asymetric
for i in range(1,6)
let syntax_dic['rxH'.i.'_Template'] =
\ repeat(header_symbol, i).' __Header__'
@ -650,8 +651,11 @@ function! vimwiki#vars#populate_syntax_vars(syntax) abort
let syntax_dic['rxH'.i.'_End'] =
\ '^\s*'.header_symbol.'\{1,'.i.'}[^'.header_symbol.'].*$'
endfor
let syntax_dic.rxHeader =
\ '^\s*\('.header_symbol.'\{1,6}\)\zs[^'.header_symbol.'].*\ze$'
" Define header regex
" -- ATX heading := preceed by #*
let atx_heading = '^\s*\%('.header_symbol.'\{1,6}\)'
let atx_heading .= '\zs[^'.header_symbol.'].*\ze$'
let syntax_dic.rxHeader = atx_heading
endif
let syntax_dic.rxPreStart =