From: Hiroshi Shirosaki Date: Mon, 18 Jan 2016 05:39:04 +0000 (+0900) Subject: Fix YAML frontmatter recognised as level-2 heading X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/8b591b760f4c2024197356c6832aeeaf6ab3a44a?ds=inline Fix YAML frontmatter recognised as level-2 heading Fixed front matter heading bugs of folding and `:Toc`. Fix #160 --- diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim index 777ddac..f16d875 100644 --- a/after/ftplugin/markdown.vim +++ b/after/ftplugin/markdown.vim @@ -21,9 +21,20 @@ if get(g:, "vim_markdown_folding_style_pythonic", 0) elseif b:fenced_block == 1 let b:fenced_block = 0 endif + elseif g:vim_markdown_frontmatter == 1 + if b:front_matter == 1 && a:lnum > 2 + let l0 = getline(a:lnum-1) + if l0 == '---' + let b:front_matter = 0 + endif + elseif a:lnum == 1 + if l1 == '---' + let b:front_matter = 1 + endif + endif endif - if b:fenced_block == 1 + if b:fenced_block == 1 || b:front_matter == 1 if a:lnum == 1 " fold any 'preamble' return '>1' @@ -80,9 +91,19 @@ else elseif b:fenced_block == 1 let b:fenced_block = 0 endif + elseif g:vim_markdown_frontmatter == 1 + if b:front_matter == 1 + if l0 == '---' + let b:front_matter = 0 + endif + elseif a:lnum == 2 + if l0 == '---' + let b:front_matter = 1 + endif + endif endif - if b:fenced_block == 1 + if b:fenced_block == 1 || b:front_matter == 1 " keep previous foldlevel return '=' endif @@ -124,6 +145,7 @@ endif let b:fenced_block = 0 +let b:front_matter = 0 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1) if !get(g:, "vim_markdown_folding_disabled", 0) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 752aee4..9ae9967 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -305,9 +305,11 @@ function! s:Toc(...) let b:bufnr = bufnr('%') let b:fenced_block = 0 + let l:front_matter = 0 let b:header_list = [] let l:header_max_len = 0 let g:vim_markdown_toc_autofit = get(g:, "vim_markdown_toc_autofit", 0) + let l:vim_markdown_frontmatter = get(g:, "vim_markdown_frontmatter", 0) for i in range(1, line('$')) let l:lineraw = getline(i) let l:l1 = getline(i+1) @@ -318,13 +320,23 @@ function! s:Toc(...) elseif b:fenced_block == 1 let b:fenced_block = 0 endif + elseif l:vim_markdown_frontmatter == 1 + if l:front_matter == 1 + if l:line == '---' + let l:front_matter = 0 + endif + elseif i == 1 + if l:line == '---' + let l:front_matter = 1 + endif + endif endif if l:line =~ '^#\+' || (l:l1 =~ '^=\+\s*$' || l:l1 =~ '^-\+\s*$') && l:line =~ '^\S' let b:is_header = 1 else let b:is_header = 0 endif - if b:is_header == 1 && b:fenced_block == 0 + if b:is_header == 1 && b:fenced_block == 0 && l:front_matter == 0 " append line to location list let b:item = {'lnum': i, 'text': l:line, 'valid': 1, 'bufnr': b:bufnr, 'col': 1} let b:header_list = b:header_list + [b:item] diff --git a/test/folding-toc.vader b/test/folding-toc.vader index e743e63..79c5400 100644 --- a/test/folding-toc.vader +++ b/test/folding-toc.vader @@ -142,3 +142,37 @@ Execute (check TOC): let elem = res[7] AssertEqual elem.lnum, 50 AssertEqual elem.text, 'chap 4.1' + +Given markdown; +--- +layout: article +title: A test of the heading folding when there is YAML frontmatter +tags: markdown yaml vim-markdown +--- +body + +heading +------- + +Execute (fold level of yaml front matter): + let g:vim_markdown_frontmatter = 1 + source ../after/ftplugin/markdown.vim + AssertEqual foldlevel(1), 0, '---' + AssertEqual foldlevel(2), 0, 'layout: article' + AssertEqual foldlevel(4), 0, 'tags: markdown yaml vim-markdown' + AssertEqual foldlevel(5), 0, '---' + AssertEqual foldlevel(6), 0, 'body' + AssertEqual foldlevel(8), 2, 'heading' + AssertEqual foldlevel(9), 2, '-------' + unlet g:vim_markdown_frontmatter + +Execute (check Toc of yaml front matter): + let g:vim_markdown_frontmatter = 1 + :Toc + :lclose + let res = getloclist(0) + AssertEqual len(res), 1 + let elem = res[0] + AssertEqual elem.lnum, 8 + AssertEqual elem.text, 'heading' + unlet g:vim_markdown_frontmatter diff --git a/test/python-folding.vader b/test/python-folding.vader index ca40c62..91fc9e2 100644 --- a/test/python-folding.vader +++ b/test/python-folding.vader @@ -60,3 +60,26 @@ Execute (fold any preamble): AssertEqual foldlevel(1), 1, 'Headline' AssertEqual foldlevel(3), 1, 'foobar' AssertEqual foldlevel(5), 0, '# Title' + +Given markdown; +--- +layout: article +title: A test of the heading folding when there is YAML frontmatter +tags: markdown yaml vim-markdown +--- +body + +heading +------- + +Execute (fold level of yaml front matter): + let g:vim_markdown_frontmatter = 1 + source ../after/ftplugin/markdown.vim + AssertEqual foldlevel(1), 1, '---' + AssertEqual foldlevel(2), 1, 'layout: article' + AssertEqual foldlevel(4), 1, 'tags: markdown yaml vim-markdown' + AssertEqual foldlevel(5), 1, '---' + AssertEqual foldlevel(6), 1, 'body' + AssertEqual foldlevel(8), 1, 'heading' + AssertEqual foldlevel(9), 1, '-------' + unlet g:vim_markdown_frontmatter