michelf / php-markdown

Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.
http://michelf.ca/projects/php-markdown/
Other
3.42k stars 530 forks source link

Nested lists are not always nested #389

Closed AntoineFr closed 1 year ago

AntoineFr commented 1 year ago

When I have a multi-level list, some levels aren't nested enough. It seams that is only affects odd levels.

Code

<?php

require __DIR__ . '/vendor/Michelf/MarkdownInterface.php';
require __DIR__ . '/vendor/Michelf/Markdown.php';
require __DIR__ . '/vendor/Michelf/MarkdownExtra.php';

$text = <<<MARKDOWN
* List level 1
  * List level 2
    * List level 3
      * List level 4
        * List level 5
          * List level 6
MARKDOWN;

$markdown = new Michelf\MarkdownExtra();

echo $markdown->transform($text);

Result

<ul>
<li>List level 1

<ul>
<li>List level 2</li>
<li>List level 3

<ul>
<li>List level 4</li>
<li>List level 5

<ul>
<li>List level 6</li>
</ul></li>
</ul></li>
</ul></li>
</ul>

Preview image

Expected result

Krinkle commented 1 year ago

I'm having the same issue, through use in https://github.com/tighten/jigsaw.

michelf commented 1 year ago

The Markdown syntax says that nested lists need to be indented by 4 spaces or one tab. The way it actually works with this parser (which mimics the original parser's behavior) is that 1 to 4 spaces count as one indent. So your level 3 item need to be indented by 5 to 8 spaces to count as two levels of indentation, and so on.

AntoineFr commented 1 year ago

The Markdown syntax says that nested lists need to be indented by 4 spaces or one tab. The way it actually works with this parser (which mimics the original parser's behavior) is that 1 to 4 spaces count as one indent. So your level 3 item need to be indented by 5 to 8 spaces to count as two levels of indentation, and so on.

Thank you for your help, it works when using 5 spaces or 2 tabs. I was confused because on GitHub (for example) you can create nested lists with only 2 spaces!