Skip to content

Commit

Permalink
Fix code fence handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KSR-Yasuda committed Jul 27, 2023
1 parent cc82d88 commit a6a1bed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
17 changes: 13 additions & 4 deletions after/ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ endfunction

if get(g:, 'vim_markdown_folding_style_pythonic', 0)
function! Foldexpr_markdown(lnum)
if (a:lnum == 1)
let b:fence_str = ''
endif

let l1 = getline(a:lnum)
"~~~~~ keep track of fenced code blocks ~~~~~
"If we hit a code block fence
if l1 =~# '````*' || l1 =~# '\~\~\~\~*'
if l1 =~# '\v^[[:space:]>]*\v(`{3,}|\~{3,})\s*(\w+)?\s*$'
" toggle the variable that says if we're in a code block
if b:fenced_block == 0
let b:fenced_block = 1
elseif b:fenced_block == 1
let b:fence_str = matchstr(l1, '\v(`{3,}|\~{3,})')
elseif b:fenced_block == 1 && matchstr(l1, '\v(`{3,}|\~{3,})') ==# b:fence_str
let b:fenced_block = 0
let b:fence_str = ''
endif
" else, if we're caring about front matter
elseif get(g:, 'vim_markdown_frontmatter', 0) == 1
Expand Down Expand Up @@ -100,16 +106,19 @@ else " vim_markdown_folding_style_pythonic == 0
function! Foldexpr_markdown(lnum)
if (a:lnum == 1)
let l0 = ''
let b:fence_str = ''
else
let l0 = getline(a:lnum-1)
endif

" keep track of fenced code blocks
if l0 =~# '````*' || l0 =~# '\~\~\~\~*'
if l0 =~# '\v^[[:space:]>]*\v(`{3,}|\~{3,})\s*(\w+)?\s*$'
if b:fenced_block == 0
let b:fenced_block = 1
elseif b:fenced_block == 1
let b:fence_str = matchstr(l0, '\v(`{3,}|\~{3,})')
elseif b:fenced_block == 1 && matchstr(l0, '\v(`{3,}|\~{3,})') ==# b:fence_str
let b:fenced_block = 0
let b:fence_str = ''
endif
elseif get(g:, 'vim_markdown_frontmatter', 0) == 1
if b:front_matter == 1
Expand Down
7 changes: 5 additions & 2 deletions ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,19 @@ function! s:GetHeaderList()
let l:front_matter = 0
let l:header_list = []
let l:vim_markdown_frontmatter = get(g:, 'vim_markdown_frontmatter', 0)
let l:fence_str = ''
for i in range(1, line('$'))
let l:lineraw = getline(i)
let l:l1 = getline(i+1)
let l:line = substitute(l:lineraw, '#', "\\\#", 'g')
" exclude lines in fenced code blocks
if l:line =~# '````*' || l:line =~# '\~\~\~\~*'
if l:line =~# '\v^[[:space:]>]*(`{3,}|\~{3,})\s*(\w+)?\s*$'
if l:fenced_block == 0
let l:fenced_block = 1
elseif l:fenced_block == 1
let l:fence_str = matchstr(l:line, '\v(`{3,}|\~{3,})')
elseif l:fenced_block == 1 && matchstr(l:line, '\v(`{3,}|\~{3,})') ==# l:fence_str
let l:fenced_block = 0
let l:fence_str = ''
endif
" exclude lines in frontmatters
elseif l:vim_markdown_frontmatter == 1
Expand Down

0 comments on commit a6a1bed

Please sign in to comment.