Skip to content

Commit 958b204

Browse files
committed
2 parents f7a62cc + 7553b05 commit 958b204

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A vim 7.4+ plugin to generate table of contents for Markdown files.
77
## Table of Contents
88

99
<!-- vim-markdown-toc GFM -->
10+
1011
* [Features](#features)
1112
* [Installation](#installation)
1213
* [Usage](#usage)
@@ -26,6 +27,7 @@ A vim 7.4+ plugin to generate table of contents for Markdown files.
2627
Supported Markdown parsers:
2728

2829
- [x] GFM (GitHub Flavored Markdown)
30+
- [x] GitLab
2931
- [x] Redcarpet
3032

3133
* Update existing table of contents.
@@ -66,6 +68,12 @@ Move the cursor to the line you want to append table of contents, then type a co
6668
6769
This command is suitable for Jekyll or anywhere else use Redcarpet as its Markdown parser.
6870
71+
3. `:GenTocGitLab`
72+
73+
Generate table of contents in [GitLab][9] link style.
74+
75+
This command is suitable for GitLab repository and wiki.
76+
6977
You can view [here][1] to know differences between *GFM* and *Redcarpet* style toc links.
7078
7179
### Update existing table of contents
@@ -160,3 +168,4 @@ The `:UpdateToc` command, which is designed to update toc manually, can only wor
160168
[6]: https://github.com/mzlogin/awesome-adb
161169
[7]: http://mazhuang.org/2015/12/19/vim-markdown-toc/
162170
[8]: https://github.com/junegunn/vim-plug
171+
[9]: https://docs.gitlab.com/ee/user/markdown.html

ftplugin/markdown.vim

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ function! s:GetHeadingLinkGFM(headingName)
147147
return l:headingLink
148148
endfunction
149149

150+
" suppport for GitLab, fork of GetHeadingLinkGFM
151+
" it's dirty to copy & paste code but more clear for maintain
152+
function! s:GetHeadingLinkGitLab(headingName)
153+
let l:headingLink = tolower(a:headingName)
154+
155+
let l:headingLink = substitute(l:headingLink, "\\_^_\\+\\|_\\+\\_$", "", "g")
156+
let l:headingLink = substitute(l:headingLink, "\\%#=0[^[:alnum:]\u00C0-\u00FF\u4e00-\u9fbf _-]", "", "g")
157+
let l:headingLink = substitute(l:headingLink, " ", "-", "g")
158+
let l:headingLink = substitute(l:headingLink, "-\\{2,}", "-", "g")
159+
160+
if l:headingLink ==# ""
161+
let l:nullKey = "<null>"
162+
if has_key(g:GFMHeadingIds, l:nullKey)
163+
let g:GFMHeadingIds[l:nullKey] += 1
164+
let l:headingLink = l:headingLink . "-" . g:GFMHeadingIds[l:nullKey]
165+
else
166+
let g:GFMHeadingIds[l:nullKey] = 0
167+
endif
168+
elseif has_key(g:GFMHeadingIds, l:headingLink)
169+
let g:GFMHeadingIds[l:headingLink] += 1
170+
let l:headingLink = l:headingLink . "-" . g:GFMHeadingIds[l:headingLink]
171+
else
172+
let g:GFMHeadingIds[l:headingLink] = 0
173+
endif
174+
175+
return l:headingLink
176+
endfunction
177+
150178
function! s:GetHeadingLinkRedcarpet(headingName)
151179
let l:headingLink = tolower(a:headingName)
152180

@@ -177,6 +205,8 @@ function! s:GetHeadingLink(headingName, markdownStyle)
177205
return <SID>GetHeadingLinkGFM(a:headingName)
178206
elseif a:markdownStyle ==# "Redcarpet"
179207
return <SID>GetHeadingLinkRedcarpet(a:headingName)
208+
elseif a:markdownStyle ==# "GitLab"
209+
return <SID>GetHeadingLinkGitLab(a:headingName)
180210
endif
181211
endfunction
182212

@@ -322,7 +352,7 @@ function! s:DeleteExistingToc()
322352

323353
if search(l:tocEndPattern, l:flags) != 0
324354
let l:markdownStyle = matchlist(l:beginLine, l:tocBeginPattern)[1]
325-
if l:markdownStyle != "GFM" && l:markdownStyle != "Redcarpet"
355+
if l:markdownStyle != "GFM" && l:markdownStyle != "Redcarpet" && l:markdownStyle != "GitLab"
326356
let l:markdownStyle = "Unknown"
327357
else
328358
let l:endLineNumber = line(".")
@@ -341,6 +371,7 @@ function! s:DeleteExistingToc()
341371
endfunction
342372

343373
command! GenTocGFM :call <SID>GenToc("GFM")
374+
command! GenTocGitLab :call <SID>GenToc("GitLab")
344375
command! GenTocRedcarpet :call <SID>GenToc("Redcarpet")
345376
command! UpdateToc :call <SID>UpdateToc()
346377
command! RemoveToc :call <SID>DeleteExistingToc()

test/test.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ call ASSERT(GetHeadingLinkTest("### ![](/path/to/a/png)", "GFM") ==# "-2")
4040
call ASSERT(GetHeadingLinkTest("### 1.1", "GFM") ==# "11")
4141
call ASSERT(GetHeadingLinkTest("### heading with some \"special\" \(yes, special\) chars: les caractères unicodes", "GFM") ==# "heading-with-some-special-yes-special-chars-les-caractères-unicodes")
4242

43+
" GitLab Test Cases
44+
45+
let g:GFMHeadingIds = {}
46+
47+
call ASSERT(GetHeadingLinkTest("# 你好!", "GitLab") ==# "你好")
48+
call ASSERT(GetHeadingLinkTest("## Hello World", "GitLab") ==# "hello-world")
49+
call ASSERT(GetHeadingLinkTest("### Hello World", "GitLab") ==# "hello-world-1")
50+
call ASSERT(GetHeadingLinkTest("#### `Hello World`", "GitLab") ==# "hello-world-2")
51+
call ASSERT(GetHeadingLinkTest("##### _Hello_World_", "GitLab") ==# "hello_world")
52+
call ASSERT(GetHeadingLinkTest("###### ,", "GitLab") ==# "")
53+
call ASSERT(GetHeadingLinkTest("# ,", "GitLab") ==# "-1")
54+
call ASSERT(GetHeadingLinkTest("## No additional spaces before / after punctuation in fullwidth form", "GitLab") ==# "no-additional-spaces-before-after-punctuation-in-fullwidth-form")
55+
call ASSERT(GetHeadingLinkTest("### No additional spaces before/after punctuation in fullwidth form", "GitLab") ==# "no-additional-spaces-beforeafter-punctuation-in-fullwidth-form")
56+
call ASSERT(GetHeadingLinkTest("#### Hello Markdown ", "GitLab") ==# "hello-markdown")
57+
call ASSERT(GetHeadingLinkTest("####Heading without a space after the hashes", "GitLab") ==# "heading-without-a-space-after-the-hashes")
58+
call ASSERT(GetHeadingLinkTest("### heading with trailing hashes ###", "GitLab") ==# "heading-with-trailing-hashes")
59+
call ASSERT(GetHeadingLinkTest("### heading with trailing hashes###", "GitLab") ==# "heading-with-trailing-hashes-1")
60+
call ASSERT(GetHeadingLinkTest("### heading with trailing hashes ends with spaces ### ", "GitLab") ==# "heading-with-trailing-hashes-ends-with-spaces-")
61+
call ASSERT(GetHeadingLinkTest("### heading with trailing hashes nested with spaces # # # ", "GitLab") ==# "heading-with-trailing-hashes-nested-with-spaces-")
62+
call ASSERT(GetHeadingLinkTest("### [vim-markdown-toc](https://github.com/mzlogin/vim-markdown-toc)", "GitLab") ==# "vim-markdown-toc")
63+
call ASSERT(GetHeadingLinkTest("### [vim-markdown-toc-again][1]", "GitLab") ==# "vim-markdown-toc-again")
64+
call ASSERT(GetHeadingLinkTest("### ![vim-markdown-toc-img](/path/to/a/png)", "GitLab") ==# "vim-markdown-toc-img")
65+
call ASSERT(GetHeadingLinkTest("### ![](/path/to/a/png)", "GitLab") ==# "-2")
66+
call ASSERT(GetHeadingLinkTest("### 1.1", "GitLab") ==# "11")
67+
call ASSERT(GetHeadingLinkTest("### heading with some \"special\" \(yes, special\) chars: les caractères unicodes", "GitLab") ==# "heading-with-some-special-yes-special-chars-les-caractères-unicodes")
68+
4369
" Redcarpet Test Cases
4470
call ASSERT(GetHeadingLinkTest("# -Hello-World-", "Redcarpet") ==# "hello-world")
4571
call ASSERT(GetHeadingLinkTest("## _Hello_World_", "Redcarpet") ==# "hello_world")

0 commit comments

Comments
 (0)