Markdown typeface: match closer to GFM specification, also add tests

This commit is contained in:
Tinmarino
2023-03-17 16:08:27 -03:00
parent f5399ffdfa
commit 8640988c5c
3 changed files with 102 additions and 30 deletions
+7 -7
View File
@@ -408,6 +408,13 @@ function! vimwiki#u#hi_typeface(dic) abort
let nested = vimwiki#u#get_syntax_dic().nested
" Bold Italic
if has_key(a:dic, 'bold_italic')
for bi in a:dic['bold_italic']
call vimwiki#u#hi_tag(bi[0], bi[1], 'VimwikiBoldItalic', nested . ',VimwikiBoldItalicUnderline')
endfor
endif
" Italic
for i in a:dic['italic']
" -- Italic 1
@@ -436,13 +443,6 @@ function! vimwiki#u#hi_typeface(dic) abort
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiUnderlineItalicBold', nested, 2)
endfor
" Bold Italic
if has_key(a:dic, 'bold_italic')
for bi in a:dic['bold_italic']
call vimwiki#u#hi_tag(bi[0], bi[1], 'VimwikiBoldItalic', nested . ',VimwikiBoldItalicUnderline')
endfor
endif
" Underline
for u in a:dic['underline']
" -- Underline 1
+36 -8
View File
@@ -753,19 +753,19 @@ function! s:get_markdown_syntaxlocal() abort
\ 'symH': {'type': type(0), 'default': 0},
\ 'typeface': {'type': type({}), 'default': {
\ 'bold': vimwiki#u#hi_expand_regex([
\ ['\%(\\\@<!_\)\@<!__\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!__\%(\\\@<!_\)\@!'],
\ ['\%(\\\@<!\*\)\@<!\*\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\*\%(\\\@<!\*\)\@!'],
\ [s:expand_delimiter('__', 1), s:expand_delimiter('__', 1)],
\ [s:expand_delimiter('\*\*', 1), s:expand_delimiter('\*\*', 1)],
\ ]),
\ 'italic': vimwiki#u#hi_expand_regex([
\ ['\%(\\\@<!\*\)\@<!\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\%(\\\@<!\*\)\@!'],
\ ['\%(\\\@<!_\)\@<!_\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!_\%(\\\@<!_\)\@!'],
\ [s:expand_delimiter('_', 0), s:expand_delimiter('_', 0)],
\ [s:expand_delimiter('\*', 0), s:expand_delimiter('\*', 0)],
\ [s:expand_delimiter('\*_', 1), s:expand_delimiter('_\*', 1)],
\ [s:expand_delimiter('_\*', 1), s:expand_delimiter('\*_', 1)],
\ ]),
\ 'underline': vimwiki#u#hi_expand_regex([]),
\ 'bold_italic': vimwiki#u#hi_expand_regex([
\ ['\*_', '_\*'],
\ ['_\*', '\*_'],
\ ['\%(\\\@<!\*\)\@<!\*\*\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\*\*\%(\\\@<!\*\)\@!'],
\ ['\%(\\\@<!_\)\@<!___\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!___\%(\\\@<!_\)\@!']
\ [s:expand_delimiter('\*\*\*', 1), s:expand_delimiter('\*\*\*', 1)],
\ [s:expand_delimiter('___', 1), s:expand_delimiter('___', 1)],
\ ]),
\ 'code': [
\ ['\%(^\|[^`\\]\)\@<=`\%($\|[^`]\)\@=',
@@ -1344,6 +1344,34 @@ function! s:normalize_syntax_settings(syntax) abort
endfunction
function! s:expand_delimiter(delim, b_can_mult) abort
" Helper: From a delimiter to the lookhead defensive version
" See: also vimwiki#u#hi_expand_regex
" TODO: get some cache to avoid recrafting the same prefix always
" Clause: if nothing, return nothing
if len(a:delim) == 0
return '\%(\)'
endif
" let c_start = a:delim[0] ==# '\' ? a:delim[0:1] : a:delim[0]
" let c_end = (len(a:delim) > 1 && a:delim[-2:-2] ==# '\') ? a:delim[-2:-1] : a:delim[-1:]
" Hardcode for markdown
let c_start = '[_*]'
let c_end = '[_*]'
let rx_mult = a:b_can_mult ? '\+' : ''
let rx_start = '\%(^\|\%(\\\@<!' . c_start . '\)\@<!\)'
let rx_middle = '\%(\%(' . a:delim . '\)' . rx_mult . '\)'
let rx_end = '\%($\|\%(\\\@<!' . c_end . '\)\@!\)'
let res = '\%(' . rx_start . rx_middle . rx_end . '\)'
echom res
return res
endfunction
" ----------------------------------------------------------
" 4. Command (exported) {{{1
" ----------------------------------------------------------
+59 -15
View File
@@ -768,11 +768,9 @@ Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'TODO Change that, currently ItalicBold and shoul dbe only Italic'
AssertEqual 0, 0
# CommentLine 'Double italic (2)'
# AssertEqual 'VimwikiItalic', SyntaxAt(1, 4)
# AssertEqual '', SyntaxAt(2, 1)
CommentLine 'Double italic (2)'
AssertEqual 'VimwikiItalic', SyntaxAt(1, 4)
AssertEqual '', SyntaxAt(2, 4)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-473):
@@ -783,11 +781,9 @@ Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'TODO, my lookaheads in typeface vars are too strict'
AssertEqual 0, 0
# CommentLine 'Double bold'
# AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# AssertEqual '', SyntaxAt(2, 1)
CommentLine 'Double bold with ****'
AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-474):
@@ -798,17 +794,53 @@ Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'TODO, my lookaheads in typeface vars are too strict'
AssertEqual 0, 0
# CommentLine 'Double bold (2)'
# AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# AssertEqual '', SyntaxAt(2, 1)
CommentLine 'Double bold with ____'
AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-475):
******foo******
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Triple bold with ******'
AssertEqual 'VimwikiBold', SyntaxAt(1, 8)
AssertEqual '', SyntaxAt(2, 1)
# Rule 14 {{{1
# An interpretation <em><strong>...</strong></em> is always preferred to <strong><em>...</em></strong>.
Given vimwiki (Typeface: https://github.github.com/gfm/#example-476):
***foo***
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Bold Italic normal, with 3 *'
AssertEqual 'VimwikiBoldItalic', SyntaxAt(1, 5)
AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-477):
_____foo_____
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Bold Italic with 5 _'
CommentLine 'TODO Passed'
# AssertEqual 'VimwikiBoldItalic', SyntaxAt(1, 7)
# AssertEqual '', SyntaxAt(2, 1)
# Rule 15 {{{1
# When two potential emphasis or strong emphasis spans overlap, so that the second begins before the first ends and ends after the first ends, the first takes precedence. Thus, for example, *foo _bar* baz_ is parsed as <em>foo _bar</em> baz_ rather than *foo <em>bar* baz</em>.
@@ -835,5 +867,17 @@ Execute (Assert Syntax):
# Rule 17 {{{1
# Inline code spans, links, images, and HTML tags group more tightly than emphasis. So, when there is a choice between an interpretation that contains one of these elements and one that does not, the former always wins. Thus, for example, *[foo*](bar) is parsed as *<a href="bar">foo*</a> rather than as <em>[foo</em>](bar).
Given vimwiki (Typeface: https://github.github.com/gfm/#example-485):
*a `*` b*
*a `a a*a a` b*
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'TODO with a * as unique code, a nice trap'
AssertEqual 0, 0
# vim: foldmethod=marker foldlevel=30 sw=2