Skip to content

Fix HTML blocks nested in indented blocks - #1666

Open
inchang-ing wants to merge 4 commits into
Python-Markdown:masterfrom
inchang-ing:fix/html-block-nested-indent
Open

inchang-ing wants to merge 4 commits into
Python-Markdown:masterfrom
inchang-ing:fix/html-block-nested-indent

Conversation

@inchang-ing

@inchang-ing inchang-ing commented Oct 20, 2026 •

Copy link
Copy Markdown

Raw HTML blocks which start on a line indented by more than three spaces were not extracted by the HTML block preprocessor, as it could not distinguish such a start tag nested within a containing block (a list item, admonition, etc.) from an indented code block. The block parser then treated the HTML as inline content and split it into separate paragraphs at any blank lines within the raw block.

The preprocessor now extracts such indented raw blocks, removes the indentation of the start tag from the stashed content, and preserves that indentation on the line which holds the placeholder. The block parser can then make the decision based on the resulting block structure: within a containing block the placeholder is parsed as nested content, whereas an indented code block keeps the placeholder in its content. In the latter case, a RawHtmlInCodeTreeprocessor (running before the codehilite treeprocessor) restores the stashed HTML as escaped text, so indented code blocks are rendered exactly as before, whether or not codehilite is enabled.

Fixes #1096

Description

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

    Specifically, Claude (Anthropic's LLM, via the WorkBuddy agent) was used to draft the code, tests, documentation, changelog entry, commit messages and this description. All output was reviewed, tested and verified by a human before submission.

    • Other: ______________

Checklist

@waylan

waylan commented Oct 20, 2026

Copy link
Copy Markdown
Member

Please edit your initial comment and check the appropriate boxes (add a x in the brackets like this [x]).

@waylan waylan added the more-info-needed More information needs to be provided. label Oct 20, 2026
@facelessuser

Copy link
Copy Markdown
Collaborator

I would like to see this tested with md_in_html as well. The testing seems generally sparse for such a potentially big change. I'd like to spend some time more thoroughly vetting this and seeing how it will impact 3rd party plugins as well.

Raw HTML blocks which start on a line indented by more than three
spaces were not extracted by the HTML block preprocessor, as it could
not distinguish such a start tag nested within a containing block
(a list item, admonition, etc.) from an indented code block. The
block parser then treated the HTML as inline content and split it
into separate paragraphs at any blank lines within the raw block.

The preprocessor now extracts such indented raw blocks, removes the
indentation of the start tag from the stashed content, and preserves
that indentation on the line which holds the placeholder. The block
parser can then make the decision based on the resulting block
structure: within a containing block the placeholder is parsed as
nested content, whereas an indented code block keeps the placeholder
in its content. In the latter case, the RawHtmlPostprocessor escapes
the stashed HTML, so indented code blocks are rendered exactly as
before.

Fixes Python-Markdown#1096
@inchang-ing
inchang-ing force-pushed the fix/html-block-nested-indent branch from 6b8d1e8 to d433147 Compare October 22, 2026 06:54
Add TestIndentedHtmlBlockswMdInHTML mirroring the two new regression
tests with the md_in_html extension enabled, per maintainer feedback on
Python-Markdown#1666.

Co-Authored-By: Claude <noreply@anthropic.com>
@inchang-ing

Copy link
Copy Markdown
Author

@facelessuser Good point — I've added TestIndentedHtmlBlockswMdInHTML to tests/test_syntax/extensions/test_md_in_html.py, which mirrors the two new regression cases with md_in_html enabled:

  • test_indented_code_block_with_blank_lines — the indented block with blank lines still renders as a code block with the extension on (stashed HTML escaped in <pre><code>), identical to the default behavior.
  • test_raw_indented_in_list_item — the raw block nested in the list item stays a single <div> inside the <li> rather than being split at blank lines.

Full suite: 1138 tests OK (111 skipped). Also checked the boxes in the PR template per @waylan's request.

@waylan

waylan commented Oct 22, 2026

Copy link
Copy Markdown
Member

You checked AI tools were used and I have disclosed which ones but I do not see where you have disclosed which ones.

@waylan

waylan commented Oct 22, 2026

Copy link
Copy Markdown
Member

I see you force pushed a change. As the Contributing Guide states:

It is generally best not to squash multiple commits and force-push your changes to a pull request. Instead, the maintainers would like to be able to follow the series of commits along with the discussion about those changes as they progress over time. If your pull request is accepted, it will be squashed at that time if deemed appropriate.

There have been multiple failures to follow our contributing guidelines. It is disrespectful for the reviewer's time. Please review the guidelines carefully and follow them fully going forward or this PR may be closed.

An indented raw HTML block which the block parser placed in an
indented code block was restored by RawHtmlPostprocessor after
serialization, so the codehilite extension never saw the content and
its non-Pygments escaping was bypassed (testFormatterLangStrGuessLang
failed because quotes were not escaped).

Restore such placeholders in a new RawHtmlInCodeTreeprocessor which
runs before the hilite treeprocessor (priority 35 vs 30), setting the
code text as escaped AtomicString content. The codehilite extension
then unescapes and re-escapes the content itself, matching its
established output, while documents without codehilite keep the
code_escape semantics of ordinary code blocks.

Co-Authored-By: Claude <noreply@anthropic.com>
@inchang-ing

Copy link
Copy Markdown
Author

Sorry about that — both fair points.

The AI disclosure is now filled in: Claude was used to draft the code, tests, docs, changelog entry, commit messages and the description; I reviewed and verified everything before submitting.

On the force push: understood, it won't happen again. The two fixes pushed since are regular incremental commits, and everything further will be as well.

@waylan waylan added needs-review Needs to be reviewed and/or approved. and removed more-info-needed More information needs to be provided. labels Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review Needs to be reviewed and/or approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants