masukomi / vim-markdown-folding

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

Lines inside fenced code blocks that start with an octothorpe are interpreted as headers and break folding #35

Open rpdelaney opened 5 years ago

rpdelaney commented 5 years ago

This looks very much along the lines of #1. Possibly related to #6 also (but I kinda doubt it).

Using vim-markdown-folding with vim-plug, on 3f35acfb753cc9ea22182400b075c5b6e896ad71. I'm observing a problem when editing this file I'm working on.

Looks like this:

#     Pre-OS prep                             [17 lines]-----------------------------------------------------------------------------------
##    Installation media                      [13 lines]-----------------------------------------------------------------------------------
##    Create partitions                       [4 lines]------------------------------------------------------------------------------------
#     gdisk /dev/nvme0n1                      [7 lines]------------------------------------------------------------------------------------
##    Create LUKS container                   [4 lines]------------------------------------------------------------------------------------
#     cryptsetup luksFormat --type luks1 --use-random --hash whirlpool --iter-time 5000 /dev/nvme0n1p2 [4 lines]---------------------------
##    O/S Filesystem                          [7 lines]------------------------------------------------------------------------------------
###   Create and mount LVM volumes            [6 lines]------------------------------------------------------------------------------------
# vgcreate volgroup0 /dev/mapper/cryptlvm
# lvcreate volgroup0 -L 32G -n lvswap
#     lvcreate volgroup0 -l 100%FREE -n lvroot [5 lines]-----------------------------------------------------------------------------------
###   Create btrfs filesystem                 [4 lines]------------------------------------------------------------------------------------
# mkfs.btrfs -L root /dev/volgroup0/lvroot
# mkswap -L swap /dev/volgroup0/lvswap
#     swapon -L swap                          [56 lines]-----------------------------------------------------------------------------------
##    O/S                                     [4 lines]------------------------------------------------------------------------------------

I would expect something more like this (maybe with different counts of folded lines):

#     Pre-OS prep                             [17 lines]-----------------------------------------------------------------------------------
##    Installation media                      [13 lines]-----------------------------------------------------------------------------------
##    Create partitions                       [4 lines]------------------------------------------------------------------------------------
##    Create LUKS container                   [4 lines]------------------------------------------------------------------------------------
##    O/S Filesystem                          [7 lines]------------------------------------------------------------------------------------
###   Create and mount LVM volumes            [6 lines]------------------------------------------------------------------------------------
###   Create btrfs filesystem                 [4 lines]------------------------------------------------------------------------------------
##    O/S                                     [4 lines]------------------------------------------------------------------------------------
masukomi commented 4 years ago

My apologies for the delay.

Thanks for the nice writeup and use-case @rpdelaney

I'll try and set aside some time to look at this. If you come up with a PR that addresses it I'll happily merge it.

masukomi commented 4 years ago

quick note for future me:

As the title suggests, the problem is essentially that it's not ignoring headings contained in code fences.

eg

```
# cryptsetup luksFormat --type luks1 --use-random --hash whirlpool --iter-time 5000 /dev/nvme0n1p2
```
wesrer commented 4 years ago

@masukomi Any progress on this?

noperator commented 4 years ago

Also experiencing this issue and would love a fix! Thanks for a great plugin @masukomi 🙂

masukomi commented 4 years ago

@wesrer just haven't gotten around to working on this. Will happily accept PRs (cc @noperator ) ;)

noperator commented 4 years ago

Strangely, I'm encountering this issue a bit inconsistently. I tried to reproduce @rpdelaney's example above by downloading the file as is, and opening it in vim.

wget https://raw.githubusercontent.com/rpdelaney/iris-setup/a73f04ab1d0d0cc587d40893af2bf48c7eb81ba7/PRE_OS.md
vim PRE_OS.md

Here's what I'm seeing:

#     Pre-OS prep                             [17 lines]-----------------------
##    Installation media                      [13 lines]-----------------------
##    Create partitions                       [12 lines]-----------------------
##    Create LUKS container                   [9 lines]------------------------
##    O/S Filesystem                          [7 lines]------------------------
###   Create and mount LVM volumes            [14 lines]-----------------------
###   Create btrfs filesystem                 [63 lines]-----------------------
##    O/S                                     [4 lines]------------------------

I'll report back here if I can reproduce an octothorpe within fenced code blocks that breaks folding.

noperator commented 4 years ago

I noticed that wherever leading octothorpes inside fenced code blocks were interpreted by this plugin as a header, those lines were also highlighted as headers—i.e., for some reason, Vim's included syntax highlighting wasn't recognizing those lines as being inside of a code block. That's likely the root issue here since this plugin's folding.vim, in turn, relies on the aforementioned syntax highlighting to determine whether a given line is inside of a code block or not: https://github.com/masukomi/vim-markdown-folding/blob/3f35acfb753cc9ea22182400b075c5b6e896ad71/after/ftplugin/markdown/folding.vim#L67-L76

I think I solved this issue (in my case, at least) by following some guidance from tpope/vim-markdown (Vim's included syntax highlighting):

Syntax highlight is synchronized in 50 lines. It may cause collapsed highlighting at large fenced code block. In the case, please set larger value in your vimrc:

let g:markdown_minlines = 100

Note that setting too large value may cause bad performance on highlighting.

I kept increasing g:markdown_minlines until this problem went away (i.e., until the folded headers looked right), settling at 1000 with no recognizable performance issues. Here's my .vimrc for your reference; masukomi/vim-markdown-folding is the only plugin I'm using, installed with Vim's native package loading.