Strong ending in nested emphasis (`**bold *italic***`) closes early and leaves a literal `*` #32
Summary#
When a strong span ends with a nested emphasis span, so that the closing run is ***, @tanstack/markdown closes the <strong> early and leaves a literal * behind. The reverse case (*outer **inner***) already works.
This is common in real content. MDXEditor's Markdown serializer, for example, writes bold text ending in an italic title exactly as **25 years of *Title***, so content produced by that editor renders with stray asterisks.
Environment#
@tanstack/markdown1.0.0,renderHtmlfrom@tanstack/markdown/html. The React adapter shows the same output.- Node 24
- Reference:
micromark4.0.2 (CommonMark)
Reproduction#
import { renderHtml } from '@tanstack/markdown/html'
renderHtml('**bold *italic***')
renderHtml('**25 years of *Title***, the debut album.')
renderHtml('***Iceman*, *Maid of Honour* and *Habibti***')
| Input | @tanstack/markdown 1.0.0 | micromark 4.0.2 (expected) |
|---|---|---|
**bold *italic*** | <p><strong>bold *italic</strong>*</p> | <p><strong>bold <em>italic</em></strong></p> |
**25 years of *Title***, the debut album. | <p><strong>25 years of *Title</strong>*, the debut album.</p> | <p><strong>25 years of <em>Title</em></strong>, the debut album.</p> |
***Iceman*, *Maid of Honour* and *Habibti*** | <p><em><strong>Iceman<em>, </em>Maid of Honour<em> and </em>Habibti</strong></em></p> | <p><strong><em>Iceman</em>, <em>Maid of Honour</em> and <em>Habibti</em></strong></p> |
*outer **inner*** (works) | <p><em>outer <strong>inner</strong></em></p> | same |
Related flanking cases (in addition to #28)#
#28 covers a * with whitespace on both sides. These two variants also open or close where CommonMark does not:
| Input | @tanstack/markdown 1.0.0 | micromark 4.0.2 (expected) |
|---|---|---|
**VIP pass **: entry included | <p><strong>VIP pass </strong>: entry included</p> | <p>**VIP pass **: entry included</p> |
Seats CSI5* and CSI1*, both sold | <p>Seats CSI5<em> and CSI1</em>, both sold</p> | <p>Seats CSI5* and CSI1*, both sold</p> |
- First row: the closing
**is preceded by whitespace, so it is not right-flanking. - Second row: the first
*is followed by whitespace, so it is not left-flanking.
Context#
We compared @tanstack/markdown against a corpus of about 4,000 real Markdown documents. These emphasis cases account for most of the visible differences. They are the only thing blocking us from adopting it.
Quick follow-up: this repository currently restricts pull request creation to collaborators, so I cannot open the PR myself. The fix is complete and pushed on my fork, ready to be turned into a PR from this compare link: https://github.com/TanStack/markdown/compare/main...anhnt256:markdown:fix/nested-emphasis-closing-run (3 files: src/inline.ts, tests/issue-regressions.test.tsx, tests/bundle-size.test.ts; full suite 356/356 passing locally). Happy to adjust anything if a maintainer opens it or if the restriction changes.
Sign in to join this conversation.
Hi! I'd like to pick this up. I have a fix ready: when a strong span closes at a longer delimiter run (), the first * of the run is lent to the inner content so the nested emphasis closes first, and the strong span closes on the remaining . It is covered by regression tests and the full suite passes locally (356/356). One scope note, also in the PR: the third example in this issue (the ** opener branch) is a different code path and is not covered by this fix. Opening a PR now.