Skip to content

Instantly share code, notes, and snippets.

@gimKondo
Created June 7, 2014 13:59
Show Gist options
  • Save gimKondo/19509aa64a621f08e2be to your computer and use it in GitHub Desktop.
Save gimKondo/19509aa64a621f08e2be to your computer and use it in GitHub Desktop.
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFold()
function! MarkdownFold()
let head = s:head(v:lnum)
if head
return head
" end fold of this level when nexe and nexe line is title and nexe line is empty
elseif v:lnum + 1 < line('$') && getline(v:lnum + 1) == "" && getline(v:lnum + 2) =~ '^#'
return '<' . s:head(v:lnum + 2)
" ignore empty line
elseif getline(v:lnum) == ""
return '='
" end fold of this level when next line is title
elseif v:lnum != line('$') && getline(v:lnum + 1) =~ '^#'
return '<' . s:head(v:lnum + 1)
endif
return '='
endfunction
function! s:head(lnum)
return strlen(matchstr(getline(a:lnum), '^#*'))
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment