Permalink
Browse files

Fix #11, compatible with Setext Style headings

  • Loading branch information...
1 parent 6fbf75f commit dc84a093b0f565c3d2b4d74511a1466465011221 @mzlogin committed Nov 6, 2016
Showing with 51 additions and 30 deletions.
  1. +2 −2 README.md
  2. +12 −3 ftplugin/markdown.vim
  3. +0 −25 test/GFM-demo.md
  4. +37 −0 test/GFM.md
View
@@ -10,8 +10,8 @@ A vim plugin to generate table of contents for Markdown files.
* [Features](#features)
* [Installation](#installation)
* [Usage](#usage)
- * [Generate table of contents](#generate-table-of-contents)
- * [Update existing table of contents](#update-existing-table-of-contents)
+ * [Generate table of contents](#generate-table-of-contents)
+ * [Update existing table of contents](#update-existing-table-of-contents)
* [Options](#options)
* [Screenshots](#screenshots)
* [References](#references)
View
@@ -14,7 +14,7 @@ endif
let g:GFMHeadingIds = {}
function! s:HeadingLineRegex()
- return "^[#]\\{1,6}"
+ return '\v(^.+$\n\=+|^.+$\n\-+|^#{1,6})'
endfunction
function! s:GetSections(beginRegex, endRegex)
@@ -54,8 +54,8 @@ endfunction
function! s:IsLineInCodeSections(codeSections, lineNum)
for beginLine in keys(a:codeSections)
- if a:lineNum > str2nr(beginLine)
- if a:lineNum < a:codeSections[beginLine]
+ if a:lineNum >= str2nr(beginLine)
+ if a:lineNum <= a:codeSections[beginLine]
return 1
endif
endif
@@ -76,6 +76,15 @@ function! s:GetHeadingLines()
let l:line = getline(".")
let l:lineNum = line(".")
if <SID>IsLineInCodeSections(l:codeSections, l:lineNum) == 0
+ " === compatible with Setext Style headings
+ let l:nextLine = getline(l:lineNum + 1)
+ if matchstr(l:nextLine, '\v^\=+$') != ""
+ let l:line = "# " . l:line
+ elseif matchstr(l:nextLine, '\v^\-+$') != ""
+ let l:line = "## " . l:line
+ endif
+ " ===
+
call add(l:headingLines, l:line)
endif
endwhile
View
@@ -1,25 +0,0 @@
-
-<!-- vim-markdown-toc GFM -->
-* [chapter 1](#chapter-1)
-* [chapter two](#chapter-------two)
-* [第三章!](#第三章)
- * [heading without space behind hashes](#heading-without-space-behind-hashes)
- * [heading with trailing hashes](#heading-with-trailing-hashes)
- * [heading with trailing hashes nested with spaces #](#heading-with-trailing-hashes-nested-with-spaces-)
- * [heading with trailing hashes nested with spaces # #](#heading-with-trailing-hashes-nested-with-spaces--)
-
-<!-- vim-markdown-toc -->
-
-# chapter 1
-
-# chapter two
-
-# 第三章!
-
-##heading without space behind hashes
-
-## heading with trailing hashes ##
-
-## heading with trailing hashes nested with spaces # #
-
-## heading with trailing hashes nested with spaces # #
View
@@ -0,0 +1,37 @@
+
+<!-- vim-markdown-toc GFM -->
+* [heading1](#heading1)
+ * [heading2](#heading2)
+* [chapter 1](#chapter-1)
+* [chapter two](#chapter-------two)
+* [第三章!](#第三章)
+ * [heading without space behind hashes](#heading-without-space-behind-hashes)
+ * [heading with trailing hashes](#heading-with-trailing-hashes)
+ * [heading with trailing hashes nested with spaces #](#heading-with-trailing-hashes-nested-with-spaces-)
+ * [heading with trailing hashes nested with spaces # #](#heading-with-trailing-hashes-nested-with-spaces--)
+
+<!-- vim-markdown-toc -->
+
+heading1
+===
+
+heading2
+--
+
+===
+
+---
+
+# chapter 1
+
+# chapter two
+
+# 第三章!
+
+##heading without space behind hashes
+
+## heading with trailing hashes ##
+
+## heading with trailing hashes nested with spaces # #
+
+## heading with trailing hashes nested with spaces # #

0 comments on commit dc84a09

Please sign in to comment.