Skip to content

Commit 0eb0845

Browse files
committed
Support assign markdown style in modeline, close #37
1 parent 1d79af5 commit 0eb0845

File tree

1 file changed

+68
-21
lines changed

1 file changed

+68
-21
lines changed

ftplugin/markdown.vim

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ endif
3232

3333
let g:GFMHeadingIds = {}
3434

35+
let s:supportMarkdownStyles = ['GFM', 'Redcarpet', 'GitLab']
36+
37+
let s:GFM_STYLE_INDEX = 0
38+
let s:REDCARPET_STYLE_INDEX = 1
39+
let s:GITLAB_STYLE_INDEX = 2
40+
3541
function! s:HeadingLineRegex()
3642
return '\v(^.+$\n^\=+$|^.+$\n^\-+$|^#{1,6})'
3743
endfunction
@@ -205,11 +211,11 @@ function! s:GetHeadingName(headingLine)
205211
endfunction
206212

207213
function! s:GetHeadingLink(headingName, markdownStyle)
208-
if a:markdownStyle ==# "GFM"
214+
if a:markdownStyle ==# s:supportMarkdownStyles[s:GFM_STYLE_INDEX]
209215
return <SID>GetHeadingLinkGFM(a:headingName)
210-
elseif a:markdownStyle ==# "Redcarpet"
216+
elseif a:markdownStyle ==# s:supportMarkdownStyles[s:REDCARPET_STYLE_INDEX]
211217
return <SID>GetHeadingLinkRedcarpet(a:headingName)
212-
elseif a:markdownStyle ==# "GitLab"
218+
elseif a:markdownStyle ==# s:supportMarkdownStyles[s:GITLAB_STYLE_INDEX]
213219
return <SID>GetHeadingLinkGitLab(a:headingName)
214220
endif
215221
endfunction
@@ -220,6 +226,15 @@ function! GetHeadingLinkTest(headingLine, markdownStyle)
220226
endfunction
221227

222228
function! s:GenToc(markdownStyle)
229+
call <SID>GenTocInner(a:markdownStyle, 0)
230+
endfunction
231+
232+
function! s:GenTocInner(markdownStyle, isModeline)
233+
if index(s:supportMarkdownStyles, a:markdownStyle) == -1
234+
echom "Unsupport markdown style: " . a:markdownStyle
235+
return
236+
endif
237+
223238
let l:headingLines = <SID>GetHeadingLines()
224239
let l:levels = []
225240
let l:listItemChars = [g:vmt_list_item_char]
@@ -233,7 +248,7 @@ function! s:GenToc(markdownStyle)
233248
let l:minLevel = min(l:levels)
234249

235250
if g:vmt_dont_insert_fence == 0
236-
put =<SID>GetBeginFence(a:markdownStyle)
251+
put =<SID>GetBeginFence(a:markdownStyle, a:isModeline)
237252
endif
238253

239254
if g:vmt_cycle_list_item_markers == 1
@@ -280,28 +295,46 @@ function! s:GetIndentText()
280295
endif
281296
endfunction
282297

283-
function! s:GetBeginFence(markdownStyle)
284-
return "<!-- ". g:vmt_fence_text . " " . a:markdownStyle . " -->"
298+
function! s:GetBeginFence(markdownStyle, isModeline)
299+
if a:isModeline != 0
300+
return <SID>GetEndFence()
301+
else
302+
return "<!-- ". g:vmt_fence_text . " " . a:markdownStyle . " -->"
303+
endif
285304
endfunction
286305

287306
function! s:GetEndFence()
288307
return "<!-- " . g:vmt_fence_text . " -->"
289308
endfunction
290309

291-
function! s:GetBeginFencePattern()
292-
return "<!-- " . g:vmt_fence_text . " \\([[:alpha:]]\\+\\) -->"
310+
function! s:GetBeginFencePattern(isModeline)
311+
if a:isModeline != 0
312+
return <SID>GetEndFencePattern()
313+
else
314+
return "<!-- " . g:vmt_fence_text . " \\([[:alpha:]]\\+\\) -->"
315+
endif
293316
endfunction
294317

295318
function! s:GetEndFencePattern()
296319
return <SID>GetEndFence()
297320
endfunction
298321

322+
function! s:GetMarkdownStyleInModeline()
323+
let l:myFileType = &filetype
324+
let l:lst = split(l:myFileType, "\\.")
325+
if len(l:lst) == 2 && l:lst[1] ==# "markdown" && index(s:supportMarkdownStyles, l:lst[0]) != -1
326+
return l:lst[0]
327+
else
328+
return ""
329+
endif
330+
endfunction
331+
299332
function! s:UpdateToc()
300333
let l:winview = winsaveview()
301334

302335
let l:totalLineNum = line("$")
303336

304-
let [l:markdownStyle, l:beginLineNumber, l:endLineNumber] = <SID>DeleteExistingToc()
337+
let [l:markdownStyle, l:beginLineNumber, l:endLineNumber, l:isModeline] = <SID>DeleteExistingToc()
305338

306339
if l:markdownStyle ==# ""
307340
echom "Cannot find existing toc"
@@ -319,7 +352,7 @@ function! s:UpdateToc()
319352
endif
320353

321354
call cursor(l:beginLineNumber, 1)
322-
call <SID>GenToc(l:markdownStyle)
355+
call <SID>GenTocInner(l:markdownStyle, l:isModeline)
323356

324357
if l:isFirstLine != 0
325358
call cursor(l:beginLineNumber, 1)
@@ -342,41 +375,55 @@ function! s:DeleteExistingToc()
342375

343376
normal! gg
344377

345-
let l:tocBeginPattern = <SID>GetBeginFencePattern()
378+
let l:markdownStyle = <SID>GetMarkdownStyleInModeline()
379+
380+
let l:isModeline = 0
381+
382+
if index(s:supportMarkdownStyles, l:markdownStyle) != -1
383+
let l:isModeline = 1
384+
endif
385+
386+
let l:tocBeginPattern = <SID>GetBeginFencePattern(l:isModeline)
346387
let l:tocEndPattern = <SID>GetEndFencePattern()
347388

348-
let l:markdownStyle = ""
349389
let l:beginLineNumber = -1
350390
let l:endLineNumber= -1
351391

352-
let l:flags = "Wc"
353-
if search(l:tocBeginPattern, l:flags) != 0
392+
if search(l:tocBeginPattern, "Wc") != 0
354393
let l:beginLine = getline(".")
355394
let l:beginLineNumber = line(".")
356395

357-
if search(l:tocEndPattern, l:flags) != 0
358-
let l:markdownStyle = matchlist(l:beginLine, l:tocBeginPattern)[1]
359-
if l:markdownStyle != "GFM" && l:markdownStyle != "Redcarpet" && l:markdownStyle != "GitLab"
396+
if search(l:tocEndPattern, "W") != 0
397+
if l:isModeline == 0
398+
let l:markdownStyle = matchlist(l:beginLine, l:tocBeginPattern)[1]
399+
endif
400+
401+
if index(s:supportMarkdownStyles, l:markdownStyle) == -1
360402
let l:markdownStyle = "Unknown"
361403
else
362404
let l:endLineNumber = line(".")
405+
echo l:beginLineNumber
406+
echo l:endLineNumber
363407
execute l:beginLineNumber. "," . l:endLineNumber. "delete_"
364408
end
365409
else
410+
let l:markdownStyle = ""
366411
echom "Cannot find toc end fence"
367412
endif
368413
else
414+
let l:markdownStyle = ""
369415
echom "Cannot find toc begin fence"
370416
endif
371417

372418
call winrestview(l:winview)
373419

374-
return [l:markdownStyle, l:beginLineNumber, l:endLineNumber]
420+
return [l:markdownStyle, l:beginLineNumber, l:endLineNumber, l:isModeline]
375421
endfunction
376422

377-
command! GenTocGFM :call <SID>GenToc("GFM")
378-
command! GenTocGitLab :call <SID>GenToc("GitLab")
379-
command! GenTocRedcarpet :call <SID>GenToc("Redcarpet")
423+
command! GenTocGFM :call <SID>GenToc(s:supportMarkdownStyles[s:GFM_STYLE_INDEX])
424+
command! GenTocGitLab :call <SID>GenToc(s:supportMarkdownStyles[s:GITLAB_STYLE_INDEX])
425+
command! GenTocRedcarpet :call <SID>GenToc(s:supportMarkdownStyles[s:REDCARPET_STYLE_INDEX])
426+
command! GenTocModeline :call <SID>GenTocInner(<SID>GetMarkdownStyleInModeline(), 1)
380427
command! UpdateToc :call <SID>UpdateToc()
381428
command! RemoveToc :call <SID>DeleteExistingToc()
382429

0 commit comments

Comments
 (0)