dotfiles

personal configuration files and scripts
git clone https://tongong.net/git/dotfiles.git
Log | Files | Refs | README

markdown.vim (1588B)


      1 " https://habamax.github.io/2019/03/07/vim-markdown-frontmatter.html
      2 function! MarkdownFold()
      3   let line = getline(v:lnum)
      4 
      5   " Regular headers
      6   let depth = match(line, '\(^#\+\)\@<=\( .*$\)\@=')
      7   if depth > 0
      8     return ">" . depth
      9   endif
     10 
     11   " Setext style headings
     12   " let prevline = getline(v:lnum - 1)
     13   " let nextline = getline(v:lnum + 1)
     14   " if (line =~ '^.\+$') && (nextline =~ '^=\+$') && (prevline =~ '^\s*$')
     15   "   return ">1"
     16   " endif
     17 
     18   " if (line =~ '^.\+$') && (nextline =~ '^-\+$') && (prevline =~ '^\s*$')
     19   "   return ">2"
     20   " endif
     21 
     22   " frontmatter
     23   if (v:lnum == 1) && (line =~ '^----*$')
     24 	  return ">1"
     25   endif
     26 
     27   return "="
     28 endfunction
     29 
     30 setlocal foldmethod=expr
     31 setlocal foldexpr=MarkdownFold()
     32 
     33 " fancy foldtext
     34 function! MyFoldText()
     35 	let line = getline(v:foldstart)
     36 
     37 	" markdown frontmatter -- just take the next line hoping it would be
     38 	" title: Your title
     39 	if line =~ '^----*$'
     40 		let line = getline(v:foldstart+1)
     41 	endif
     42 
     43 	let indent = max([indent(v:foldstart)-v:foldlevel, 1])
     44 	let lines = (v:foldend - v:foldstart + 1)
     45 	let strip_line = substitute(line, '^//\|=\+\|["#]\|/\*\|\*/\|{{{\d\=\|title:\s*', '', 'g')
     46 	let strip_line = substitute(strip_line, '^[[:space:]]*\|[[:space:]]*$', '', 'g')
     47 	let text = strpart(strip_line, 0, winwidth(0) - v:foldlevel - indent - 6 - strlen(lines))
     48 	if strlen(strip_line) > strlen(text)
     49 		let text = text.'…'
     50 	endif
     51 	return repeat('#', v:foldlevel) . repeat(' ', indent) . text .' ('. lines .')'
     52 endfunction
     53 set foldtext=MyFoldText()
     54 
     55 " for markdown I prefer 2-wide tabs
     56 setlocal tabstop=2 softtabstop=2 shiftwidth=2