Skip to content

Split list items off an indented block of the previous item - #1670

Open
gabesgh wants to merge 2 commits into
Python-Markdown:masterfrom
gabesgh:fix-list-item-after-indented-block
Open

gabesgh wants to merge 2 commits into
Python-Markdown:masterfrom
gabesgh:fix-list-item-after-indented-block

Conversation

@gabesgh

@gabesgh gabesgh commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

Fixes #845.

When a list item comes right after an indented paragraph or nested list of the previous item, with no blank line in between, it was swallowed into that paragraph as a lazy continuation line.

ListIndentProcessor now ends the indented block at the first line that is indented less than the block and starts another item of the list at that level, so it works at any level of nesting. Those items continue their list as tight items, because no blank line comes before them (the same as Markdown.pl).

Unchanged:

  • Lazy continuation lines, and the First line of a paragraph / - second line example from the issue.
  • With sane_lists, a marker of the other list type is still treated as text.

One behaviour change: a horizontal rule or header right after those items now ends the list, the same as it already does for a list at the top level. Before, it ended up inside the previous item.

Not covered: the equivalent case with def_list, where a : definition comes right after an indented paragraph of the previous definition. I'm happy to look at that separately.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: Tests deterministically created and verified using Claude Code.

Checklist

A list item which directly followed an indented paragraph or nested list
of the previous item, without a blank line between them, was treated as
a lazy continuation line of that block (Python-Markdown#845).

`ListIndentProcessor` now splits the block before the first unindented
line which starts another item of the list. The registered list
processors decide what counts as an item, so `sane_lists` still treats a
marker of the other list type as text. Other unindented lines remain
lazy continuations.

As no blank line comes before them, the split off items continue the
list as tight items: a `listcontinue` parser state tells
`OListProcessor` not to make the first of them loose. They are parsed
like any list block at that level, so a horizontal rule or header after
them now ends the list, as it already does at the top level.

Fixes Python-Markdown#845.

@waylan waylan left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that the new behavior matches the reference implementation.

Comment thread tests/test_syntax/blocks/test_ul.py Resolved
@waylan

waylan commented Oct 22, 2026

Copy link
Copy Markdown
Member

Not covered:

  • The same pattern one level deeper, where a nested item comes after a nested item's indented paragraph.

Just saw that. It should definitely be covered at the same time.

@waylan waylan added the requires-changes Awaiting updates after a review. label Oct 22, 2026
@facelessuser

Copy link
Copy Markdown
Collaborator

Not sure how this issue was not found before now. In any event, I confirmed that the new behavior matches the reference implementation.

I believe this issue was generally known; at least I was aware of it. Due to the fact that I rarely run into it, I think it was never a priority for me to look into 🤷🏻.

An item of a nested list which directly follows an indented paragraph
of the previous nested item, without a blank line, was still merged
into that paragraph. Split an indented block at the first line which
is indented less than the block and starts an item of the list at that
level of indentation, not only at unindented lines.

When the rest is still indented, it is split again in the same way,
and its items continue the nested list as tight items, as at the top
level.

Also move the sane_lists test to a test file for the extension.

Refs Python-Markdown#845.
@gabesgh
gabesgh force-pushed the fix-list-item-after-indented-block branch from 5842d7c to 1826557 Compare October 22, 2026 19:58
@gabesgh

gabesgh commented Oct 23, 2026

Copy link
Copy Markdown
Author

Thanks for the review
I moved the sane_lists test to tests/test_syntax/extensions/test_sane_lists.py and extended the fix to nested lists at any depth, with a test.
The nested output matches PHP Markdown on Babelmark. The def_list case is still open and I'm happy to do it in a separate PR.

@gabesgh
gabesgh requested a review from waylan October 23, 2026 01:46
@waylan waylan added needs-review Needs to be reviewed and/or approved. and removed requires-changes Awaiting updates after a review. labels Oct 23, 2026

@waylan waylan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good to me. Although I would like a second opinion from @facelessuser and/or @mitya57.

(parent[-1].tag in self.LIST_TYPES)))

def run(self, parent: etree.Element, blocks: list[str]) -> None:
# Whether this block starts with items split off an indented block (see the end of this method).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor thing, but this comment strikes me as odd. Starting with the word "whether" is confusing and the phrase "see the end of this method" is vague. I don't have any concrete suggestions for improvment.

@facelessuser

Copy link
Copy Markdown
Collaborator

I'll try to review over the weekend. I'm just getting overrun with AI PRs and such in some of my other projects. Real bugs, etc., but it has been quite taxing.

@facelessuser

Copy link
Copy Markdown
Collaborator

Thanks for working on this. I reviewed the change, looking especially for behavior it changes beyond what it sets out to fix. Overall, it works well: lists, blockquotes, footnotes, and lists nested in definitions all render better, including several cases that were badly wrong before (deeply nested items collapsed into one paragraph, etc.

A few things to address:

1. A nested split makes the outer item loose

Python-Markdown decides whether an item is tight or loose from the blank lines at that item's own level. An item becomes loose when a blank line separates its own blocks or separates it from a sibling. A blank line inside a nested item doesn't make the parent loose:

-   A list.
    -   Nested.

        More content.

Here A list. is tight, both before and after this PR. Adding a nested item right after, with no blank line, makes it loose with this PR:

-   A list.
    -   Nested.

        More content.
    -   Break.

A list. becomes <p>A list.</p>, while Break. is tight. No blank line touches the outer item, so it should stay tight.

The split-off - Break. is routed to the outer item through the ListIndentProcessor branch meant for a later block after a blank line. That branch moves the item's text into a <p> before anything else, and continued is only used there to keep the new items tight. Skipping the wrap for continued items fixes it. In markdown/blockprocessors.py, line 207 (permalink):

if sibling[-1].text and not continued:

With that change the outer item stays tight in the nested cases, and nothing else in my test documents changed. test_nested_item_after_indented_paragraph_no_blank_line currently expects <p>item one</p>, so it would need updating to <li>item one<ul>…. Unless loose was intended there, I'd treat the current expectation as recording the side effect.

2. Behavior changes worth stating in the description

  • Horizontal rules. A * * * or - - - line right after an indented paragraph used to end up inside the item. It now ends the list, since the line also matches a list marker. That's what already happens when a blank line separates them, so I think it's correct, but it should be mentioned.
  • Lazy lines that look like list markers now start items. This includes prose such as - an aside, + c and * b *, and numbered lines such as 1986. A good year. As elsewhere in Python-Markdown, they join the existing list whatever the marker, unless sane_lists is enabled. So in a bullet list the 1986. becomes an item and the number is dropped. This matches how tight lists already behave.
  • Other improvements: a list item after a table is no longer absorbed as a table row, and an attr_list {: ... } on a lazy line now applies to the paragraph.

As an aside, if we are to merge this, we should absolutely apply this to definition lists as well for consistent behavior.

@waylan waylan added requires-changes Awaiting updates after a review. and removed needs-review Needs to be reviewed and/or approved. labels Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-changes Awaiting updates after a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested block level items break list without blank line after

3 participants