Skip to content

Fix HTML blocks nested in indented blocks (#1096) - #1688

Open
inchang-ing wants to merge 3 commits into
Python-Markdown:masterfrom
inchang-ing:html-indent-container
Open

inchang-ing wants to merge 3 commits into
Python-Markdown:masterfrom
inchang-ing:html-indent-container

Conversation

@inchang-ing

Copy link
Copy Markdown

Description

Fixes #1096.

A raw HTML block whose start tag is indented by more than three spaces and sits inside a containing block (a list item, an admonition, etc.) was not extracted by the HTML block preprocessor, because at that point the indentation cannot be told apart from an indented code block. The block parser then treated the HTML as inline content and split it into separate paragraphs at every blank line.

This takes the "container" approach prototyped by @facelessuser in facelessuser/markdown (branch html-indent). Once a container has removed its own indentation, a block which starts with a block level tag can only be a raw block, so a new RawHtmlBlockProcessor extracts it there using the same extractor as the preprocessor, or the md_in_html extractor when that extension is enabled. Containers pass their content on a block at a time, so a raw block containing blank lines is collected across calls, keyed by the container, until it is closed, and RawHtmlFlushTreeprocessor finishes anything still open once the document has been parsed. The preprocessor is not modified, so extensions which work on the document text before it is parsed see exactly what they saw before. Content of a blockquote is deliberately left alone to keep its current behavior.

The two implementation commits are @facelessuser's, included unmodified and with their original authorship: Extract raw HTML blocks nested in containers in a block processor and Handle elements under blockquotes and fix comments. The third commit only adds the changelog entry. The approach is considerably simpler than the treeprocessor-based one in #1666, which this PR supersedes.

Verification

python -m unittest discover tests passes: 1310 tests, 111 skipped, 0 failures. flake8 reports nothing on the touched source files.

The behavior was also checked by rendering the same three inputs against the parent commit and against this branch. A raw block with a blank line inside a list item was rendered as <p><div> / <p>second paragraphs before and as a single <div> after; the same input inside a blockquote likewise; a four-space indented code block containing the same markup is unchanged in both, still escaped inside <pre><code>.

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: ______________

    Claude was used to write the PR description, the changelog entry, and the message of the changelog commit. The implementation commits were authored by @facelessuser in his fork; he disclosed in Fix HTML blocks nested in indented blocks #1666 that he used AI to compile the list of cases that should pass while prototyping. I ran the test suite and the before/after renders myself and can explain any part of the change.

Checklist

facelessuser and others added 3 commits November 3, 2026 17:57
The HTML preprocessor can only extract a raw block that starts at the
beginning of a line. Within a container (a list item, an admonition,
etc.) the block is indented, which cannot be told apart from an indented
code block, so it was left as text. The block parser then treated it as
inline HTML and split it at any blank lines.

Once the container has removed its indentation, a block that starts with
a block level tag can only be a raw block. A new `RawHtmlBlockProcessor`
extracts it there, using the same extractor as the preprocessor (or the
`md_in_html` extractor when that extension is enabled). Containers pass
their content on a block at a time, so a raw block with blank lines in it
is collected across calls, keyed by the container, until it is closed.
`RawHtmlFlushTreeprocessor` finishes anything still open when the
document has been parsed.

The preprocessor is not changed, so extensions that work on the text of
the document before it is parsed see exactly what they saw before, and
indented code blocks, paragraph continuation lines and list items render
as they always have.

Content of a blockquote is left alone to keep its current behavior.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@inchang-ing inchang-ing mentioned this pull request Nov 4, 2026
13 of 15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML block handling nested in indented blocks doesn't work properly

2 participants