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

Broken indentation inside fenced code blocks within list items

Open okdana opened this issue 9 years ago • 2 comments

MarkdownExtra doesn't seem to track the indentation level correctly when you use a fenced code block within a list item. Here's a Gist that illustrates the problem: https://gist.github.com/okdana/75a3bf7f2218b7d67adeed03f81b53fd

The output from the above is:

<pre><code>  Two spaces indented.
</code></pre>

<ul>
<li><p>Un-ordered list item.</p>

<pre><code>Two spaces indented.
</code></pre></li>
</ul>

<ol>
<li><p>Ordered list item.</p>

<pre><code> Two spaces indented.
</code></pre></li>
</ol>

The 'bare' fenced code block has the correct indentation — two spaces.

The fenced code block in the un-ordered list item has no indentation at all.

The fenced code block in the ordered list item has one space (one fewer than expected).

The indentation is similarly broken if you use tabs within the code block (in other words, two or three spaces to indent to the list-item text, then a tab to indent the code): Tab-indented code blocks are rendered with no indentation in both ordered and un-ordered lists. (A 'bare' code block that uses tabs for indentation is correctly rendered with four spaces.)

okdana avatar Sep 28 '16 08:09 okdana

For comparison, the CommonMark spec seems to align with my expectation of how it should work. Their tester thing renders it as such:

http://spec.commonmark.org/dingus/?text=*%20Un-ordered%20list%20item.%0A%0A%20%20%60%60%60%0A%20%20%20%20Two%20spaces%20indented.%0A%20%20%60%60%60%0A%0A1.%20Ordered%20list%20item.%0A%0A%20%20%20%60%60%60%0A%20%20%20%20%20Two%20spaces%20indented.%0A%20%20%20%60%60%60%0A

Their implementation preserves tabs (which i understand that MarkdownExtra doesn't do for parser simplicity reasons), so the tab problem doesn't exist there either. League\CommonMark, unsurprisingly, works the same way as the CommonMark test thing.

The only other implementation i've actually tested is GitHub's, which doesn't seem to get it quite right either unfortunately: https://gist.github.com/okdana/79d05d836faf2da8ee8db2d266c24b26

okdana avatar Sep 28 '16 08:09 okdana

Note that this applies to more than fenced code blocks. The original Markdown spec says that content of list items must be indented by four spaces, but it was never implemented exactly way in the reference implementation Markdown.pl which is less strict. Except for code blocks (the indented kind in original Markdown) where you would have to indent by four spaces for the list item and four more spaces for the code block. If you look at this example, you'll see that the results are all over the place.

The same thing applies to fenced code blocks. It's a relic of the way the four-space rule is half implemented as said in the spec. It could be changed with some work, but I fear it might break a lot of documents already out there. So I'm not too sure what to do about this.

michelf avatar Sep 28 '16 10:09 michelf