Repository navigation
fix(Markdown): pair emphasis and strong markers by CommonMark's rules - #7051
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewNo new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression16 of 424 shot(s) changed. A change here is a question, not a failure: check whether the after is the
Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
There was a problem hiding this comment.
Requesting one change: emphasis can now nest thousands deep, and Markdown cannot render that.
A crafted message, *a **a repeated 2,000 times, then b, then a** a* repeated 2,000 times (28 KB), fails to render in Chrome with "Maximum call stack size exceeded", so the reader sees an error instead of the message. The previous parser rendered it flat in a few milliseconds. Pairing is also quadratic in the nesting: in Node, 56 KB of this input takes 3 seconds and 112 KB takes 13 seconds before the render fails, and 48 KB of *a runs followed by b* runs takes 4 seconds. Each closer walks back over every run already used, and each pair marks every index between its runs. Markdown renders chat and model output, so one hostile or broken message can freeze and blank a reader's view. Two changes would close this: cap emphasis nesting at a fixed depth and leave deeper runs as text, and unlink used runs from the delimiter stack, as CommonMark's reference implementation does, so each search skips them. A test with CommonMark's nested strong emphasis input at 10,000 levels would guard both.
The rest holds. All 132 CommonMark emphasis examples produce CommonMark's output except three where *** keeps strong outside emphasis on purpose, and five that need inline HTML or angle-bracket autolinks, which Markdown does not parse. Against commonmark.js on 28,000 random strings, the only other differences come from emoji: commonmark.js reads half of a surrogate pair beside a run, while this change reads the whole character as the spec defines it. Markdown and RichText now agree on all 17 nested forms I tried, including a plugin node inside nested marks (7 before). The 118 stories that render Markdown render it the same, while streaming shows raw markers on fewer partial lines (16 instead of 35 in my sample) and closes before trailing spaces as described. The FR23 benchmark stays within budget over three isolated rounds on each side, and ordinary documents parse as fast as before. 30 of the 46 new tests fail on the previous parser, and 759 core tests, 346 RichText tests, the build, tsc and the Changeset check pass, with all 59 Chrome tests.
On ***both***: keeping strong outside emphasis is stated in the Changeset and the code. It differs from CommonMark only in which tag is outermost, renders the same bold italic, and keeps the tree shape Markdown has always produced.
[Automated review]
There was a problem hiding this comment.
Approving. The pairing pass is now linear and nesting stops at 100 levels, so the inputs from my last review parse in milliseconds and render.
- Timing alone: CommonMark's nested strong emphasis input parses in 35 ms at 109 KB, where it took 13 seconds before, and in 181 ms at 438 KB. Time doubles as the input doubles from 27 KB to 438 KB, and the other pathological inputs scale the same way, so the tests' 5-second budgets sit far above it.
- In Chrome, the same inputs from 14 KB to 140 KB render with no stack error and no React error, nesting 100 levels deep and leaving the rest as text. The 137 KB, 10,000-level input parses in 68 ms.
- Nothing else moved. The 132 CommonMark emphasis examples give the same results as the last head, the 28,000-string comparison with commonmark.js gives identical counts, Markdown and RichText still agree on all 17 nested forms, and streaming shows raw markers on the same 16 partial lines.
- 767 core tests, 346 RichText tests, the builds, tsc, lint and the Changeset check pass. The FR23 benchmark stays within budget in isolation, and ordinary documents parse as fast as on main. The 59 Chrome tests pass apart from the known flaky plugin-node test, which flakes on main too. The docs now state the strong-around-emphasis order and the depth limit, and the change merges cleanly with main, including the link destination fix.
[Automated review]
<!-- template: bug-fix.md -->
## User impact
An app that builds Astryx from source with the documented Next.js setup, but sets no browserslist or targets a browser without Unicode property escapes (such as Safari 11), fails `next build --webpack` with `Unknown property: L` (also `Zs`, `Letter`). The errors point at `Avatar.tsx`, `Markdown/parser.ts` and `Markdown/plugins/headingLinks.ts`, package source the app cannot change, and come from both the PostCSS pass and the JavaScript compile. Any import from the package root reaches them.
## Problem and solution fit
- Why it fails: with no targets, or old ones, `next/babel`'s preset-env rewrites every `\p{…}` regex literal with transform-unicode-property-regex. Next's bundled copy of that transform loads `regenerate-unicode-properties` at runtime, which Next does not ship, so it throws (Next 16.3.6 and 16.4.2 alike).
- Why it harms the task: the throw stops the whole build.
- Why that matters: the app cannot fix it in its own code, and the error names a Unicode property, not the missing target.
## Expected behavior and authority
This is a regression of a settled case. #5450 hit this failure in `NumberInput/numberParser.ts` and builds those expressions with `new RegExp(...)`; its comment records why, and notes that property escapes need no transform in any browser this package supports. #6698, #6911 and #7051 later added four new literals and reintroduced it.
## Smallest restoration
- The four literals become `new RegExp(...)` with the same pattern and flags, built once at module load as before.
- Each site's comment names the cause and when a literal is safe again: once Next's bundled Babel ships the Unicode data.
- A source-build test fails when a Core module writes a property escape as a regex literal.
## Evidence
- Reproduction before the change: a clean registry install of main's documented Next.js example (`apps/example-nextjs-source`, hoisted, browserslist removed). `next build --webpack` fails in the PostCSS pass (`Avatar.tsx: Unknown property: L`) and in the JavaScript compile (`Unknown property: L`, `Zs`, `Letter` from the three files).
- Result after the change: the same install, with this change applied to Core's source, compiles and prerenders, and all 226 rendered class names have a rule in the emitted CSS.
- Representative unchanged path: the Avatar and Markdown suites pass unchanged.
- Regression test or other mutation-sensitive proof: `unicodePropertyEscapes.source-build.test.mjs` fails on main listing the four literals, and passes here.
## Scope
- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.
## Testing
- `pnpm exec vitest run --project ui packages/core/src/Avatar packages/core/src/Markdown packages/core/src/unicodePropertyEscapes.source-build.test.mjs`: 50 files, 1,158 tests pass (the new test alone fails on main).
- `next build --webpack` on the clean install, no browserslist: fails on main in both passes; passes with this change (226 of 226 rendered classes have a rule).
- `pnpm -F @astryxdesign/core typecheck`, ESLint and Prettier on the touched files, `pnpm check:repo`, changeset coverage: pass.
User impact
A reader of
*see **bold** more*— emphasis with bold inside, which RichText writes whenever a person bolds a word inside italic text — saw inMarkdownthree separate italic pieces and no bold;*see **bold***lost the bold and showed a stray**. The same document reads correctly in RichText, so the two surfaces disagreed, and every RichText export of this form rendered wrong inMarkdown.Problem and solution fit
Markdown's inline parser paired each*,**,_, or__with the next identical marker in the text, so the first**of a nested strong closed the surrounding emphasis.spec:AST-061FR7 (strong, emphasis, and the rest expose the same semantics on every surface) andcomponent:MarkdownFR23.Runs of
*and_are now paired as CommonMark 0.31 §6.2 specifies: each run is left- or right-flanking by the characters around it (an underscore run only outside a word), each closer takes the nearest compatible opener, two delimiters make strong and one makes emphasis, and the rule of three stops runs that can both open and close from pairing across a multiple of three. Unpaired runs stay text. Strikethrough, code, links, escapes, and everything else are unchanged.***text***still renders as strong around emphasis (<strong><em>), where CommonMark's reference output puts emphasis outside. It is the orderMarkdownhas always rendered and RichText draws, both elements are present and announced either way, and changing it would change every released document that uses it; the consumer docs now say so.Robustness
Markdownrenders chat and model output, so no input may hang or crash it:Streaming: an unfinished
**boldnow closes before its trailing space, so a partial line keeps its formatting under the new flanking rules.Expected behavior and authority
spec:AST-061FR7 andcomponent:MarkdownFR23 (current), using CommonMark 0.31 §6.2 for the pairing rules.Smallest restoration
The emphasis branch of the inline parser and one pairing pass after it (
parser.ts); one line in the streaming closer. No public API, node, or renderer change.Evidence
*a\*b*is<em>a*b</em>.*see **bold***,*see **bold** more*,**bold *both***,_see __bold__ more_, and*see **bold** and **more***nest as written;Markdownrenders the<strong>inside the<em>with no stray markers.***both***andfoo***bar***bazkeep strong outside emphasis.Markdownrenders the 10,000-level input (50<em>, 50<strong>) without exhausting the stack. Alone, the 10,000-level input parses in about 0.1 s (about 90 ms at 112 KB). The test budget is 5 s per parse, because the UI suite runs in parallel and its timings vary tenfold under load; the previous pass took 50–200 s on the nested inputs.Scope
Testing
vitest run --project ui packages/core/src/Markdown packages/core/src/Outline(perf run separately): 767 passed, three runs in a row;packages/richtext: 346 passed.Markdown.fr23.perf.test.tsalone: pass.pnpm -F @astryxdesign/core build;tsc;check:use-client;check:changesets: pass.pnpm storybook:build, then the RichText and core Markdown Playwright specs in Chrome: 59 passed.[fix].