masukomi / vim-markdown-folding

Fold markdown documents by section.
248 stars 43 forks source link

Want to just fold the fenced code #21

Closed tfer closed 5 years ago

tfer commented 8 years ago

I'm looking for a way to just fold up the fenced code blocks, i.e. without folding any of the heading levels. The use case for this is for adding emphasis for various strings in paragraphs, but not when they occur in fenced code. I known that there is a way to limit searches to text outside of folds, so being able to fold just the fenced code should let me do what I need.

None of the markdown folding available seems to do this, and most are way too complex to try to hack into, yours looks simple enough that I might have a chance to add this in.

Enjoyed your class by the way.

sappo commented 7 years ago

@tfer

Here's a quick and dirty way to only fold fenced code blocks. Just copy it to ~/.vim/after/ftplugin/markdown/folding.vim

let b:isopen = 0
let b:closenext = 0
function! MarkdownFolds()
  if b:closenext == 1
    let b:closenext = 0
    return "0"
  endif
  let thisline = getline(v:lnum)
  if match(thisline, '^```') >= 0
    if b:isopen == 0
      let b:isopen = 1
      return ">1"
    else
      let b:isopen = 0
      let b:closenext = 1
    endif
  endif
  return "="
endfunction
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFolds()

function! MarkdownFoldText()
  let foldsize = (v:foldend - v:foldstart)
  let firstline = getline(v:foldstart)
  let type = substitute(firstline, '^```\+', '', '')
  return 'Code Block -> '.type.' ('.foldsize.' lines)'
endfunction
setlocal foldtext=MarkdownFoldText()
masukomi commented 5 years ago

@tfer The plugin now supports folding fenced code blocks in the same way you'd fold headings. Is that sufficient for your request or were you asking for something a little different? Please reopen this Issue with details if you were hoping for something different.