Skip to content

Keep emphasis nested directly in emphasis when re-rendering Markdown - #312

Merged
frostming merged 1 commit into
frostming:masterfrom
RavSinghChandan:emphasis-nested-delimiter
Nov 11, 2026
Merged

frostming merged 1 commit into
frostming:masterfrom
RavSinghChandan:emphasis-nested-delimiter

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

MarkdownRenderer writes every emphasis with *, so when emphasis is the only content of another emphasis, the delimiters of both join into one run and the nesting changes. The common bold italic **_foo_** is one case:

>>> md = marko.Markdown(renderer=MarkdownRenderer)
>>> md("**_foo_**\n")
'***foo***\n'
<!-- before -->
<p><strong><em>foo</em></strong></p>
<!-- after re-rendering -->
<p><em><strong>foo</strong></em></p>

The same happens with __*foo*__, and *_foo_* (spec example 460) and _*foo*_ (462) come back as **foo**, which is strong emphasis.

When the only child of an emphasis or strong emphasis is an emphasis, that inner emphasis is now written with _ (or * if the outer one uses _), so **_foo_** stays **_foo_**. Its _ always has the outer * next to it, so the rules for _ inside words don't apply. Other nesting is unchanged: ***foo***, emphasis around strong emphasis, already gives the same HTML, and so does foo***bar***baz.

Added test_markdown_renderer_nested_emphasis; 5 of its 7 cases fail on master, and the last two check that ***foo*** and foo***bar***baz are unchanged. Across the CommonMark spec examples, the number whose re-rendered Markdown gives different HTML goes from 17 to 15. pytest tests, black, isort and flake8 pass, and mypy reports the same 5 errors as on master.

Used Claude (Opus) to help find and patch this; I reviewed and tested the change

@human-attestation

Copy link
Copy Markdown

🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge.

Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements.

@frostming
frostming merged commit 890a8ff into frostming:master Nov 11, 2026
19 of 20 checks passed
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.

2 participants