markdown-mode icon indicating copy to clipboard operation
markdown-mode copied to clipboard

Code blocks don't parse properly when in lists

Open MyriaCore opened this issue 3 years ago • 3 comments

When the beginning backticks of a codeblock are on the first line of a new list element (numbered, ordered, whatever), markdown-mode fails to parse the codeblock properly, and interprets the ending backticks as starting backticks.

As a result, whenever the user starts a list with a codeblock, then tries to edit it with C-', they'll get an error Not inside a GFM or tilde fenced code block. Also, if the codeblock happens to be a language that has a mode for it in emacs, the fontification fails - the contents of the codeblock are highlighted the default color.

This error is likely due to the parsing rule that requires the starting backticks of a codeblock to have whitespace before it. A correct rule would be whitespace preceding the backticks, or "list bullets" of any type preceding the backticks.

Steps to Reproduce

  1. Fill a markdown buffer with the following content:
    1. ```
       this is
       a codeblock
       ```
    2. This is normal text
    
  2. Press C-' in the codeblock to edit it
  3. See Bug
  4. Replace the buffer contents with the following:
    1. ```
       this is
       a codeblock
       ```
    2. This is normal text
    3. ```
       the user thinks this
       is a new codeblock
       I think differently c;
       ```
    
  5. Press C-' in the second codeblock
  6. See Bug

MyriaCore avatar Dec 05 '20 19:12 MyriaCore

I found this bug quite annoying when viewing/editing @logseq's Markdown notes in Emacs. Because all note entries are basically bullets and code fences will break syntax highlighting.

syu-id avatar Oct 04 '22 05:10 syu-id

BTW Is this valid markdown syntax ? I couldn't find such examples in commonmark spec. Code block can be indented by spaces but other characters other than spaces can not be put before triple quotes.

syohex avatar Oct 04 '22 14:10 syohex

As shown in the issue post, GitHub can render those code fences without a problem. So at least it is supported by GFM (I believe VS Code also supports this).

  1. this is
    a codeblock
    
  2. This is normal text
  3. the user thinks this
    is a new codeblock
    I think differently c;
    

But in the Emacs Markdown mode, they will look like this screenshot:

syu-id avatar Oct 04 '22 16:10 syu-id