]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix YAML frontmatter recognised as level-2 heading
authorHiroshi Shirosaki <h.shirosaki@gmail.com>
Mon, 18 Jan 2016 05:39:04 +0000 (14:39 +0900)
committerHiroshi Shirosaki <h.shirosaki@gmail.com>
Mon, 18 Jan 2016 05:50:27 +0000 (14:50 +0900)
Fixed front matter heading bugs of folding and `:Toc`.
Fix #160

after/ftplugin/markdown.vim
ftplugin/markdown.vim
test/folding-toc.vader
test/python-folding.vader

index 777ddace57db29e70dde6ce2f9fec97209c72e7b..f16d875f4127f596abf75dc4aa1ddf2a061ca8f8 100644 (file)
@@ -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 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
 
         endif
 
-        if b:fenced_block == 1
+        if b:fenced_block == 1 || b:front_matter == 1
             if a:lnum == 1
                 " fold any 'preamble'
                 return '>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 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
 
         endif
 
-        if b:fenced_block == 1
+        if b:fenced_block == 1 || b:front_matter == 1
             " keep previous foldlevel
             return '='
         endif
             " keep previous foldlevel
             return '='
         endif
@@ -124,6 +145,7 @@ endif
 
 
 let b:fenced_block = 0
 
 
 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)
 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
 
 if !get(g:, "vim_markdown_folding_disabled", 0)
index 752aee40b558066e3d3d5febbeabf3742a4c1829..9ae99676d44a5b595dee203c3ec1711237c99b0c 100644 (file)
@@ -305,9 +305,11 @@ function! s:Toc(...)
 
     let b:bufnr = bufnr('%')
     let b:fenced_block = 0
 
     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 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)
     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 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
         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]
             " 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]
index e743e63f2aac4d6c1b1da0f728d1181d56be5031..79c5400185d0b710a64fa8107a599a2916a63285 100644 (file)
@@ -142,3 +142,37 @@ Execute (check TOC):
   let elem = res[7]
   AssertEqual elem.lnum, 50
   AssertEqual elem.text, 'chap 4.1'
   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
index ca40c6277487ff47eece5b516b9630a680c9d2f9..91fc9e270eabf6d12d88ebdc97e1440eaf0330f9 100644 (file)
@@ -60,3 +60,26 @@ Execute (fold any preamble):
   AssertEqual foldlevel(1), 1, 'Headline'
   AssertEqual foldlevel(3), 1, 'foobar'
   AssertEqual foldlevel(5), 0, '# Title'
   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