Skip to content

Commit 63c934b

Browse files
committed
Add support for GitLab, ref #36
1 parent f42c2fd commit 63c934b

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ function! s:GetHeadingLinkGFM(headingName)
139139
return l:headingLink
140140
endfunction
141141

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

@@ -169,6 +197,8 @@ function! s:GetHeadingLink(headingName, markdownStyle)
169197
return <SID>GetHeadingLinkGFM(a:headingName)
170198
elseif a:markdownStyle ==# "Redcarpet"
171199
return <SID>GetHeadingLinkRedcarpet(a:headingName)
200+
elseif a:markdownStyle ==# "GitLab"
201+
return <SID>GetHeadingLinkGitLab(a:headingName)
172202
endif
173203
endfunction
174204

@@ -330,6 +360,7 @@ function! s:DeleteExistingToc()
330360
endfunction
331361

332362
command! GenTocGFM :call <SID>GenToc("GFM")
363+
command! GenTocGitLab :call <SID>GenToc("GitLab")
333364
command! GenTocRedcarpet :call <SID>GenToc("Redcarpet")
334365
command! UpdateToc :call <SID>UpdateToc()
335366
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)