Python-Markdown / markdown

A Python implementation of John Gruber’s Markdown with Extension support.
https://python-markdown.github.io/
BSD 3-Clause "New" or "Revised" License
3.8k stars 862 forks source link

Listing Issues with Python Markdown #1482

Closed Skrillx13 closed 1 month ago

Skrillx13 commented 1 month ago

Context

Had an issue, and reported it to Material for MKDocs. Was told it was a Python Markdown issue so I am creating an issue here.

I was using MKDocs, and making a number list. Unsure if this is a Markdown issue or Material MKDocs issue though, but it seems to be on the MKDocs side.

Bug description

When making said number list, I inserted a code block in between the list, so it looks something like this:

  1. Step 1
  2. Step 2
    Code Block I inserted
  3. Step 3

However, instead of rendering like the step above, it rendered like this:

  1. Step 1
  2. Step 2
    Code Block I inserted
  3. Step 3

As you can see, the order breaks, and it defaults back to 1 when a code block is inserted through it.

Related links

Reporting a bug GitHub Repo Reproduction 9.5.38-zip.zip

Steps to reproduce

  1. Created a GitHub Repo and cloned it using a SSH Key
  2. Set up a Python Virtual Enviroment
    python -m venv venv
    source venv/bin/activate
  3. Installed Material for MKDocs, and opened it up in VSC
    pip install mkdocs-material
    code .
  4. Ran mkdocs new ., and mkdocs serve, before adding all my configurations. Created a number list as mentioned in the steps above.
facelessuser commented 1 month ago

This is not an uncommon way to process these lists, though many CommonMark parsers (which this parser is not) will often restart the list with the specified number.

Basically, due to the lack of indentation on the code block, the code block is not part of the previous list item, so a new list is started after the code block. The default list handler doesn't care about specific numbers but starts a new list always with 1.

So, you have two options here.

  1. Indent your code block by 4 spaces. If you do, I would use SuperFences instead as Python Markdown's fenced code extension as Python Markdown's default one only processes fenced code at the root level of documentation, not nested under other constructs.
  2. Or use the Sane Lists extension that is included already in Python Markdwon. This extension pays attention to the specified number and will start/restart a list using the starting number of the list.
waylan commented 1 month ago

I agree with @facelessuser. Whenever a non-list item element exists between two list items, that element ends the first list and a new list is started after it. If the element is intended to be part of the list, then it needs to be indented as a child element, which avoids starting a new list. This is user error, the parser is working as intended. I am closing this.