Skip to content

Adds new option to create anchors on headings #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ if !exists("g:vmt_include_headings_before")
let g:vmt_include_headings_before = 0
endif

if !exists("g:vmt_insert_anchors")
let g:vmt_insert_anchors = 0
endif

let g:GFMHeadingIds = {}

let s:supportMarkdownStyles = ['GFM', 'Redcarpet', 'GitLab', 'Marked']
Expand Down Expand Up @@ -129,7 +133,7 @@ function! s:GetHeadingLines()
endif
" ===

call add(l:headingLines, l:line)
call add(l:headingLines, [l:line, l:lineNum])
endif
let l:flags = "W"
endwhile
Expand Down Expand Up @@ -255,6 +259,20 @@ function! GetHeadingLinkTest(headingLine, markdownStyle)
return <SID>GetHeadingLink(l:headingName, a:markdownStyle)
endfunction

function! s:GenerateAnchor(headingLine, line, markdownStyle, isModeline)
let l:anchorText = []
if g:vmt_dont_insert_fence == 0
call add(l:anchorText, <SID>GetBeginFence(a:markdownStyle, a:isModeline))
call add(l:anchorText, "") "Add newline
endif
call add(l:anchorText, "<a name='". <SID>GetHeadingLink(a:headingLine, a:markdownStyle) ."'></a>")
call add(l:anchorText, "") "Add newline
if g:vmt_dont_insert_fence == 0
call add(l:anchorText, <SID>GetEndFence())
endif
call append(a:line, l:anchorText)
endfunction

function! s:GenToc(markdownStyle)
call <SID>GenTocInner(a:markdownStyle, 0)
endfunction
Expand All @@ -265,12 +283,15 @@ function! s:GenTocInner(markdownStyle, isModeline)
return
endif

let l:headingLines = <SID>GetHeadingLines()
let l:fullHeadingLines = <SID>GetHeadingLines()
let l:headingLines = deepcopy(l:fullHeadingLines)
call map(l:headingLines, {_, val -> val[0]})
let l:levels = []
let l:listItemChars = [g:vmt_list_item_char]
let l:currentOffset = line('.')

let g:GFMHeadingIds = {}

for headingLine in l:headingLines
call add(l:levels, <SID>GetHeadingLevel(headingLine))
endfor
Expand Down Expand Up @@ -312,6 +333,16 @@ function! s:GenTocInner(markdownStyle, isModeline)
if g:vmt_dont_insert_fence == 0
silent put =<SID>GetEndFence()
endif

let l:currentOffset = line('.') - l:currentOffset - 1

if g:vmt_insert_anchors == 1
call reverse(l:fullHeadingLines)
for headingLine in l:fullHeadingLines
let l:offset = headingLine[1] + l:currentOffset
call <SID>GenerateAnchor(headingLine[0], l:offset, a:markdownStyle, a:isModeline)
endfor
endif
endfunction

function! s:GetIndentText()
Expand Down Expand Up @@ -406,7 +437,7 @@ function! s:DeleteExistingToc()
keepjumps normal! gg0

let l:markdownStyle = <SID>GetMarkdownStyleInModeline()

let l:isModeline = 0

if index(s:supportMarkdownStyles, l:markdownStyle) != -1
Expand Down Expand Up @@ -454,6 +485,17 @@ function! s:DeleteExistingToc()
echom "Cannot find toc begin fence"
endif

"Delete anchors
while search(l:tocBeginPattern, "Wc") != 0
let l:anchorBeginLineNumber = line(".")
if search(l:tocEndPattern, "W") != 0
let l:anchorEndLineNumber = line(".")
silent execute l:anchorBeginLineNumber. "," . l:anchorEndLineNumber. "delete_"
else
echom "Cannot find toc end fence"
endif
endwhile

call winrestview(l:winview)

return [l:markdownStyle, l:beginLineNumber, l:endLineNumber, l:isModeline]
Expand Down