Skip to content

Commit dc84a09

Browse files
committed
Fix #11, compatible with Setext Style headings
1 parent 6fbf75f commit dc84a09

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ A vim plugin to generate table of contents for Markdown files.
1010
* [Features](#features)
1111
* [Installation](#installation)
1212
* [Usage](#usage)
13-
* [Generate table of contents](#generate-table-of-contents)
14-
* [Update existing table of contents](#update-existing-table-of-contents)
13+
* [Generate table of contents](#generate-table-of-contents)
14+
* [Update existing table of contents](#update-existing-table-of-contents)
1515
* [Options](#options)
1616
* [Screenshots](#screenshots)
1717
* [References](#references)

ftplugin/markdown.vim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endif
1414
let g:GFMHeadingIds = {}
1515

1616
function! s:HeadingLineRegex()
17-
return "^[#]\\{1,6}"
17+
return '\v(^.+$\n\=+|^.+$\n\-+|^#{1,6})'
1818
endfunction
1919

2020
function! s:GetSections(beginRegex, endRegex)
@@ -54,8 +54,8 @@ endfunction
5454

5555
function! s:IsLineInCodeSections(codeSections, lineNum)
5656
for beginLine in keys(a:codeSections)
57-
if a:lineNum > str2nr(beginLine)
58-
if a:lineNum < a:codeSections[beginLine]
57+
if a:lineNum >= str2nr(beginLine)
58+
if a:lineNum <= a:codeSections[beginLine]
5959
return 1
6060
endif
6161
endif
@@ -76,6 +76,15 @@ function! s:GetHeadingLines()
7676
let l:line = getline(".")
7777
let l:lineNum = line(".")
7878
if <SID>IsLineInCodeSections(l:codeSections, l:lineNum) == 0
79+
" === compatible with Setext Style headings
80+
let l:nextLine = getline(l:lineNum + 1)
81+
if matchstr(l:nextLine, '\v^\=+$') != ""
82+
let l:line = "# " . l:line
83+
elseif matchstr(l:nextLine, '\v^\-+$') != ""
84+
let l:line = "## " . l:line
85+
endif
86+
" ===
87+
7988
call add(l:headingLines, l:line)
8089
endif
8190
endwhile

test/GFM-demo.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/GFM.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
<!-- vim-markdown-toc GFM -->
3+
* [heading1](#heading1)
4+
* [heading2](#heading2)
5+
* [chapter 1](#chapter-1)
6+
* [chapter two](#chapter-------two)
7+
* [第三章!](#第三章)
8+
* [heading without space behind hashes](#heading-without-space-behind-hashes)
9+
* [heading with trailing hashes](#heading-with-trailing-hashes)
10+
* [heading with trailing hashes nested with spaces #](#heading-with-trailing-hashes-nested-with-spaces-)
11+
* [heading with trailing hashes nested with spaces # #](#heading-with-trailing-hashes-nested-with-spaces--)
12+
13+
<!-- vim-markdown-toc -->
14+
15+
heading1
16+
===
17+
18+
heading2
19+
--
20+
21+
===
22+
23+
---
24+
25+
# chapter 1
26+
27+
# chapter two
28+
29+
# 第三章!
30+
31+
##heading without space behind hashes
32+
33+
## heading with trailing hashes ##
34+
35+
## heading with trailing hashes nested with spaces # #
36+
37+
## heading with trailing hashes nested with spaces # #

0 commit comments

Comments
 (0)