Permalink
Browse files

fix #1, enhance GFM style links, add screenshots

  • Loading branch information...
1 parent 82ffae9 commit 6dd2a06f2df4071c6b5e4fe745c9d5e2adaeabab @mzlogin committed Dec 17, 2015
Showing with 85 additions and 24 deletions.
  1. +2 −0 .gitignore
  2. +18 −0 README.md
  3. +65 −24 ftdetect/markdown.vim
  4. BIN screenshots/chinese.gif
  5. BIN screenshots/english.gif
View
@@ -0,0 +1,2 @@
+*.swp
+.DS_Store
View
@@ -6,14 +6,20 @@ A vim plugin to generate table of contents for a markdown file.
There are *GFM* and *Redcarpet* two styles TOC links, see [here][1] to view their difference.
+Move the cursor to the place you want to insert TOC, then type a command below suit you. The command will generate **headings after the cursor** into TOC.
+
1. `:GenTocGFM`
Generate table of contents in [GFM][2] link style.
+ Suit markdown files in **GitHub repository**, like `README.md`.
+
2. `:GenTocRedcarpet`
Generate table of contents in [Redcarpet][3] link style.
+ Suit **Jekyll/gh-pages** use Redcarpet as it's markdown parser.
+
## Installation
Suggest to manage your vim plugins via [Vundle][4] so you can install it simply three steps:
@@ -28,6 +34,16 @@ Suggest to manage your vim plugins via [Vundle][4] so you can install it simply
3. `:PluginInstall`
+## Screenshots
+
+* [online demo in English][5]
+
+![](./screenshots/english.gif)
+
+* [online demo in Chinese][6]
+
+![](./screenshots/chinese.gif)
+
## References
* <https://github.com/ajorgensen/vim-markdown-toc>
@@ -36,3 +52,5 @@ Suggest to manage your vim plugins via [Vundle][4] so you can install it simply
[2]: https://help.github.com/articles/github-flavored-markdown/
[3]: https://github.com/vmg/redcarpet
[4]: http://github.com/VundleVim/Vundle.Vim
+[5]: https://github.com/mzlogin/chinese-copywriting-guidelines/blob/Simplified/README.en.md
+[6]: http://mazhuang.org/wiki/chinese-copywriting-guidelines/
View
@@ -1,7 +1,7 @@
if exists("g:loaded_MarkdownTocPlugin")
finish
endif
-"let g:loaded_MarkdownTocPlugin = 1
+let g:loaded_MarkdownTocPlugin = 1
function! s:HeadingLineRegex()
return "^[#]\\{1,6} "
@@ -11,14 +11,11 @@ function! s:GetHeadingLines()
let l:winview = winsaveview()
let l:headingLines = []
let l:headingLineRegex = <SID>HeadingLineRegex()
- let l:flags = "Wc"
- normal! gg
+ let l:flags = "W"
while search(l:headingLineRegex, l:flags) != 0
let l:line = getline(".")
call add(l:headingLines, l:line)
-
- let l:flags = "W"
endwhile
call winrestview(l:winview)
@@ -31,38 +28,82 @@ function! s:GetHeadingLevel(headingLine)
return len(l:sharps)
endfunction
+function! s:SpecSubstitiute(lowerHeading, src, dst)
+ let l:headingLink = a:lowerHeading
+
+ if l:headingLink[0] ==# a:src
+ let l:quote = a:dst
+ let l:headingLink = l:headingLink[1:-1]
+ if len(l:headingLink) > 0
+ let l:quote = l:quote . "-"
+ endif
+ let l:headingLink = l:quote . l:headingLink
+ endif
+
+ if l:headingLink[-1] ==# a:src
+ let l:quote = a:dst
+ let l:headingLink = l:headingLink[0:-2]
+ if len(l:headingLink) > 0
+ let l:quote = "-" . l:quote
+ endif
+ let l:headingLink = l:headingLink . l:quote
+ endif
+
+ let l:headingLink = substitute(l:headingLink, a:src, "-" . a:dst . "-", "g")
+
+ return l:headingLink
+endfunction
+
function! s:GetHeadingLink(headingName, markdownStyle)
let l:headingLink = tolower(a:headingName)
+ let l:headingLink = substitute(l:headingLink, "`", "", "g")
+
if a:markdownStyle ==# "GFM"
let l:headingLink = substitute(l:headingLink, "/", "", "g")
let l:headingLink = substitute(l:headingLink, "\"", "", "g")
+ let l:headingLink = substitute(l:headingLink, "@", "", "g")
+ let l:headingLink = substitute(l:headingLink, "#", "", "g")
+ let l:headingLink = substitute(l:headingLink, "\\$", "", "g")
+ let l:headingLink = substitute(l:headingLink, "%", "", "g")
+ let l:headingLink = substitute(l:headingLink, "\\^", "", "g")
+ let l:headingLink = substitute(l:headingLink, "+", "", "g")
+ let l:headingLink = substitute(l:headingLink, "&", "", "g")
+ let l:headingLink = substitute(l:headingLink, "*", "", "g")
+ let l:headingLink = substitute(l:headingLink, "'", "", "g")
+ let l:headingLink = substitute(l:headingLink, "\\~", "", "g")
+ let l:headingLink = substitute(l:headingLink, ";", "", "g")
+ let l:headingLink = substitute(l:headingLink, "\\.", "", "g")
+ let l:headingLink = substitute(l:headingLink, ",", "", "g")
+ let l:headingLink = substitute(l:headingLink, "?", "", "g")
+ let l:headingLink = substitute(l:headingLink, ":", "", "g")
+ let l:headingLink = substitute(l:headingLink, "|", "", "g")
+
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "", "", "g")
+ let l:headingLink = substitute(l:headingLink, "——", "", "g")
elseif a:markdownStyle ==# "Redcarpet"
if l:headingLink[0] ==# "-"
let l:headingLink = l:headingLink[1:-1]
endif
let l:headingLink = substitute(l:headingLink, "/", "-", "g")
- if l:headingLink[0] ==# "\""
- let l:quote = "quot"
- let l:headingLink = l:headingLink[1:-1]
- if len(l:headingLink) > 0
- let l:quote = l:quote . "-"
- endif
- let l:headingLink = l:quote . l:headingLink
- endif
-
- if l:headingLink[-1] ==# "\""
- let l:quote = "quot"
- let l:headingLink = l:headingLink[0:-2]
- if len(l:headingLink) > 0
- let l:quote = "-" . l:quote
- endif
- let l:headingLink = l:headingLink . l:quote
- endif
-
- let l:headingLink = substitute(l:headingLink, "\"", "-quot-", "g")
+ let l:headingLink = <SID>SpecSubstitiute(l:headingLink, "\"", "quot")
+ let l:headingLink = <SID>SpecSubstitiute(l:headingLink, "'", "39")
endif
let l:headingLink = substitute(l:headingLink, " ", "-", "g")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6dd2a06

Please sign in to comment.