Repository navigation
[lexical-mdast] Bug Fix: Join touching strikethrough and write the character references cmark-gfm needs in Markdown export - #9983
Conversation
…arkdown
## Description
An export rule that returns a `delete` with `data` (or an inline decorator that returns one) is kept as it is instead of grouped with the text around it, so two `delete` nodes can end up side by side. GFM has no way to write those apart: `~~a~~~~b~~` reads back as one strikethrough of `a~~~~b`, in micromark, cmark-gfm and markdown-it alike. Markdown export now joins touching `delete` nodes into one while the stock strikethrough handler writes them, keeping the first one's fields, at any depth (in bold, italic, links and table cells). Within a joined `delete`, touching inline code is joined too where the stock or table cell code handler writes it, and a plain empty text between two `delete` nodes does not keep them apart where the stock text handler writes it as nothing. The join works on a copy when serializing: `$convertToMdast()` still returns both nodes, and a `delete` handler of your own still gets both.
The README sentence that such middleware "must ensure its output serializes correctly on its own" now says it means each node by itself, and describes the join.
## Changeset
`.changeset/mdast-touching-deletes.md` (`@lexical/mdast` patch).
## Test plan
### Before
`TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts` with the source of `main`:
```
× joins touching deletes an export rule returns with the same data
× joins touching deletes an export rule returns with data on one of them
× joins touching deletes an export rule returns with the same data, three of them
...
AssertionError: expected '~~a~~~~**b**~~' to be '~~a**b**~~' // Object.is equality
Tests 29 failed | 223 passed (252)
```
### After
```
"~~a**b**~~" -> [["a",4],["b",5]]
```
```
$ pnpm exec vitest run --project unit packages/lexical-mdast
Test Files 18 passed (18)
Tests 669 passed (669)
$ pnpm run test-unit
Test Files 1032 passed | 6 skipped (1038)
Tests 40594 passed | 423 expected fail | 174 skipped (41191)
```
`pnpm run tsc`, `tsc-test`, `flow`, `prettier`, `eslint packages/lexical-mdast/src`, `check-changesets` and `build` pass. No e2e or browser tests: the playground does not use `@lexical/mdast`.
…ds beside emphasis and strikethrough in a strict export
## Description
cmark-gfm 0.29.0.gfm.14, which GitHub renders with, reads `*`/`_` runs differently from micromark and markdown-it in two places. It skips a `~` run and classifies the character beyond it, so ``~~a~~*`b`*`` keeps its asterisks. It reads a symbol such as `©` as a letter, as CommonMark did before 0.31, so `©_a_` keeps its underscores. A strict export (`exportPunctuation: 'strict-commonmark'`, the default) now re-reads the written Markdown with micromark and writes the character cmark-gfm reads beside such a run as a reference where that fixes the run (``~~a~~*`b`*``, `©_a_`). A reference is only kept if micromark reads every paragraph and the whole document as before, and only where it cannot change how markdown-it reads the text: a backtick that micromark does not read as syntax on a paragraph's lines, or on the lines joined to it, leaves the paragraph as it is, and a `*`, `_` or `~` it does not read as syntax keeps the runs of that marker beside a reference as they read; no reference goes in a word that linkify-it or cmark-gfm's bare-URL scanner may link, in an image or reference label, or in a table cell with brackets other than those of an inline link, with the same decisions when measuring column widths. `compileMdast` keeps the strict micromark extensions for this. `exportPunctuation` and the README list what stays different. A `'cjk-friendly'` export is unchanged.
## Changeset
`.changeset/mdast-cmark-gfm-references.md` (`@lexical/mdast` patch).
## Test plan
### Before
`TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfm.test.ts` with the source of the previous commit:
```
× writes a reference beyond a `~` run: ~~a~~*`b`*
× writes a reference beyond a `~` run: *`b`*~~a~~
× writes a symbol beside a run as a reference: ©_a_
...
AssertionError: expected '~~a~~*`b`*' to be '~~a~~*`b`*' // Object.is equality
AssertionError: expected '©_a_' to be '©_a_' // Object.is equality
Tests 60 failed | 86 passed (146)
```
GitHub's `/markdown` API on those exports:
```
~~a~~*`b`* <p><del>a</del>*<code>b</code>*</p>
©_a_ <p>©_a_</p>
```
### After
```
~~a~~*`b`* <p><del>a</del><em><code>b</code></em></p>
©_a_ <p>©<em>a</em></p>
```
```
$ pnpm exec vitest run --project unit packages/lexical-mdast
Tests 815 passed (815)
$ pnpm run test-unit
Tests 40740 passed | 423 expected fail | 174 skipped (41337)
```
`pnpm run tsc`, `tsc-test`, `flow`, `prettier`, `eslint`, `check-changesets`, `build` and the tree-shaking tests pass. No e2e: the playground does not use `@lexical/mdast`.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
This looks good: thanks for tracking both problems down to the reader rules behind them. The delete join matches GFM's rule that only runs of one or two tildes pair, so ~~a~~~~b~~ really is one strikethrough of a~~~~b. The reference pass models what cmark-gfm 0.29 does: scan_delims skips ~ (a strikethrough "emphasis" skip char) on both sides of a */_ run, and cmark_utf8proc_is_punctuation counts only P-category characters, so © reads as a letter. Keeping an edit only when micromark reads the whole document unchanged is a good safety net for Lexical's own import.
A few suggestions, all optional and non-blocking:
-
Skip work when nothing fails (
packages/lexical-mdast/src/MdastExport.ts, around line 2561). The gate matches any~run touching a*/_run, so a document holding ordinary strikethrough-bold text (~~**x**~~) gets a full micromark parse on every strict export. When no run fails,readForCmarkGfmstill serializes every event, becausesearcheskeeps each subject that has matched runs. A subject with no failing run never proposes an edit, so filtering onsubject.runs.some(run => isFailing(run, NO_EDITS))should give identical output and return before that work. Export can run on every change:dev-examples/mdast-editorrecomputes$convertToMarkdownString()for each editor state. A small benchmark of a document that only matches the gate would show what that path costs. -
Put the repaired output under the existing reader tests.
MdastCrossReader.test.ts(markdown-it, strict and CJK-friendly) andMdastPunctuationRoundTrip.test.ts(strict export read back by the default CJK-friendly import and by@lexical/markdown) run only theirFIXED_CASESin CI, and none of those reaches the new pass. A fixed case such as[['a', strikethrough], ['.', italic]](exported as~~a~~*.*) in each would keep that coverage in every run. The Test plan could also paste a run of the env-gated random cases (MDAST_ROUND_TRIP_CASES=… MDAST_CROSS_READER_CASES=… pnpm run test-unit) on this head, in the Before/After form AGENTS.md asks for; your differential run did not list markdown-it-cjk-friendly or the CJK-friendly import, and it predates the last change. -
Nit:
MdastImportExport.test.ts"exports a list on its own" pins'ab', an export that drops both list markers, as the expected value. Asserting only that the export does not throw, or saying in the assertion message that the markers are a known gap, would keep the guard without making that output look like a contract.
…t a repaired export under the reader tests
## Description
The cmark-gfm pass kept every subject with a matched run for its search, although one with no failing run never proposes an edit; it now keeps only those with a failing run, which gives the same output. The fixed cases of `MdastCrossReader.test.ts` and `MdastPunctuationRoundTrip.test.ts` never reached the pass, so each now has `[['a', strikethrough], ['.', italic]]`, exported as `~~a~~*.*`, which markdown-it, the CJK-friendly import and `@lexical/markdown` read back. "exports a list on its own" asserts only that the export does not throw, since `'ab'`, which `main` writes too, is not a contract.
## Test plan
### Before
With the pass turned off, the strict exports of every fixed case of both files are the same as with it on.
### After
The strict exports of 2,432,128 generated inputs are the same as with the previous commit. With the pass turned off, only the new case exports differently.
```
$ TZ=UTC pnpm exec vitest run --project unit packages/lexical-mdast
Tests 815 passed (815)
$ MDAST_ROUND_TRIP_CASES=20000 MDAST_CROSS_READER_CASES=20000 TZ=UTC pnpm exec vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCrossReader.test.ts packages/lexical-mdast/src/__tests__/unit/MdastPunctuationRoundTrip.test.ts
Test Files 2 passed (2)
Tests 7 passed (7)
```
There was a problem hiding this comment.
This looks good, thanks. Joining touching strikethrough fixes a real round-trip bug, and the strict-export pass is careful: it only keeps a reference when micromark still reads the whole document as before, so an @lexical/mdast import of the new output gives back the same document. I rendered the base and head outputs your tests assert with cmark-gfm 0.29.0.gfm.13 and markdown-it (CommonMark 0.31.2). 58 of the 77 outputs the pass changes, which cmark-gfm misread on the base, now read the same in both. The rest are single-tilde and bare-URL cases markdown-it does not cover, plus the partial repairs the README lists. The earlier suggestions are all in e3e5664: only subjects with a failing run are searched, MdastCrossReader.test.ts and MdastPunctuationRoundTrip.test.ts have a repaired fixed case, and the list test asserts only that the export does not throw.
Two optional, non-blocking suggestions:
-
Cost of the gate (
packages/lexical-mdast/src/MdastExport.ts:1662).CMARK_GFM_GATEmatches any*or_touching a~, so every strict export with strikethrough bold or italic text (~~**x**~~) parses the whole Markdown at line 2557, even when no run fails. Your own numbers are 1.4 ms without the pass and 13.6 ms with it, on 43 paragraphs. The cheaper check you described, which skips the parse when cmark-gfm reads a space or punctuation beyond each~run and no symbol sits beside a*or_, would cover the commontext ~~**x**~~ textcase. Exporting on every change, as thedev-examples/mdast-editormarkdownsignal does, makes this cost visible. A test that repeats the input of 'splits a proposal that changes the reading in halves' in one long paragraph would also show whether rejected attempts keep the parse count bounded: each round re-parses the document (line 2975). -
A half-repaired pair is pinned (
packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfm.test.ts:732). The expected;→~~*&*~~→_a_©_**keeps the second→for the*and~~closers. That reference also opens the_beforea, whose closer still fails, so cmark-gfm renders→<em>a_©</em>**where micromark and markdown-it read→<em>a</em>©_**. This is no regression, because the base output was misread too. But it contradicts the JSDoc atMdastExport.ts:2542("the opener and closer of a pair are repaired together or not at all"). Rewording that sentence, or adding this case to the exceptions inexportPunctuation, would keep the stated guarantee accurate.
| const CMARK_GFM_GATE = | ||
| /[*_]~|~[*_]|(?=[\x80-\uffff])\p{S}[*_~]|[*_~](?=[\x80-\uffff])\p{S}/u; |
There was a problem hiding this comment.
Optional: this gate matches any * or _ touching a ~, so every strict export with strikethrough bold or italic (text ~~**x**~~ text) parses the whole Markdown at line 2557 even when no run fails. Your 43-paragraph measurement puts that at 1.4 ms to 13.6 ms. The cheaper check you described (a space or punctuation beyond each ~ run as cmark-gfm reads it, and no symbol beside a * or _) would let that common case return before the parse.
| expect(exportRaw(';→~~*&*~~→_a_©_**')).toBe( | ||
| ';→~~*&*~~→_a_©_**', | ||
| ); |
There was a problem hiding this comment.
Nit: cmark-gfm 0.29.0.gfm.13 renders this expected output as ;→<del><em>&</em></del>→<em>a_©</em>**, while micromark and markdown-it read →<em>a</em>©_**. The second →, kept for the * and ~~ closers, also opens the _ before a, whose closer still fails, so cmark-gfm closes it on the literal _ after ©. This is no regression, because the base output was misread too. But it contradicts "the opener and closer of a pair are repaired together or not at all" in the JSDoc at MdastExport.ts:2542. Rewording that line, or listing this case among the exceptions, would keep the guarantee accurate.
|
A heads-up on a performance regression I found in the strict-export pass while following up on the parse-cost suggestion. When proposals keep getting rejected within one long paragraph, each round re-parses the whole document, and the number of rounds grows with the paragraph. A 10 KB paragraph of editor text takes about 40 s to export. The output is still correct, it's just slow. The cheaper pre-parse check I mentioned doesn't work (it would skip repairs like |
…helper ## Description The merge of `main` brought a module-scope `text` helper into `MdastMiddleware.test.ts` (meta#9983). The tests added here reuse `text` as a parameter of the `$block` builders and as a local in the selection test, which ESLint's `no-shadow` rejects. They are now `leaf` and `selected`. ## Test plan ### Before ``` $ pnpm exec eslint packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts 1152:30 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow 1153:23 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow 1157:7 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow 1164:7 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow 1172:7 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow 1265:17 error 'text' is already declared in the upper scope on line 268 column 7 no-shadow ✖ 6 problems (6 errors, 0 warnings) ``` ### After ``` $ pnpm exec eslint packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts $ pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts Tests 103 passed (103) ```
… with micromark syntax of your own ## Description The cmark-gfm pass of a strict export (meta#9983) judges each character reference it proposes by parsing the document again, unless the pairing model (meta#9988) judges it. The model only judged the stock configuration, so with any other `micromarkExtensions` entry, a `disable`, or without GFM strikethrough, a long paragraph whose proposals keep being rejected took quadratic time: a 7.2 KB paragraph exported in about 16 s (production build), 57 s in a development build. This change makes the model judge those configurations too, with the same output: - Without lexical's strikethrough syntax, the model reads `~` as text. - A `disable` of only `autolink`, `codeIndented`, `htmlFlow` or `htmlText` keeps the model on. - Beside other syntax (`isForeignSyntax`: no construct at a marker or at `&`, no attention markers, no span resolver), the first parse records what each of its constructs read (`parseRecordingForeignReads`): the character before an attempt, what it and the attempts inside it consumed, the character after, what it serializes, the content before it where it reads the events, and the check before an attempt. A proposal that none of them read is judged by the model; one that any read, or one in a subject where such a construct with a resolver succeeded, or once such an edit is accepted, by a parse. - A parse budget per export (65,536 characters, and four times the length of the document and of the cells of each table) bounds the parses that remain; past it, a proposal that only a parse judges reads as changing the reading, and the search goes on with the model's verdicts. ## Test plan ### Before On origin/main (0e6fc89), production build (esbuild, `__DEV__` false), one paragraph of `~~_a\~\~ba\~\~b_€a_b\a~~___`!_.`___` repeated 200 times (7.2 KB): ``` math-like text construct at `$` 16201 ms 200 references disable codeIndented, … 16033 ms 200 references ``` ### After ``` math-like text construct at `$` 48 ms 200 references disable codeIndented, … 45 ms 200 references ``` With the budget off, the export equals origin/main's for 26,000 generated documents across 14 configurations (stock GFM, CommonMark only, footnotes, a math-like construct, a table syntax copy, disables, constructs that read the events, the character before them, a parser flag across a line, a tab or a backslash outside the text). ``` $ pnpm vitest run --project unit packages/lexical-mdast Test Files 21 passed (21) Tests 1098 passed (1098) $ pnpm run test-unit Test Files 1039 passed | 6 skipped (1045) Tests 41055 passed | 423 expected fail | 174 skipped (41652) ```
…hout a parse budget ## Description The strict export checks every character reference it writes for cmark-gfm by reading the document again with micromark. Any repair read the whole document twice, so one `x ©_a_` in a 1 MB export took 915 ms on origin/main against 46 ms before meta#9983, and a `~~**x**~~` that the gate catches but needs no repair cost a whole parse too. The parse budget that kept the slowest searches short also left repairs undone when it ran out (0 of 400 in a 14 KB paragraph that syntax of your own reads). The export now records the string each top-level block writes and searches only the blocks the gate passes, together with every block that holds a definition, as a small document, then writes the repairs back. A list or block quote that is more than half the export is cut at the strings its items or children wrote. This takes effect only where every block was written by the default handlers (lexical's table and HTML-block handlers are registered as such) and the serializer configuration is lexical's own; otherwise the whole document is searched as before. With syntax lexical knows, the small document's final check decides. With syntax of your own, which may carry state across blocks, the whole original and the whole output are read once each and compared. When that whole check fails, the edits are kept by halves of the search's accepted attempts (a pair is never split), then searched on from there; this starts without records and makes one recording parse, through the same I5 comparison as before, only once it needs a third parse verdict, so that the per-start learning applies only where it pays. The parse budget (`CMARK_GFM_PARSE_BUDGET`) is removed: every candidate is decided, by the model or by a parse. ## Changeset `.changeset/mdast-cmark-gfm-model.md` (unreleased meta#9983 entry) now states the block-by-block search instead of the budget. ## Test plan ### Before The new tests on 179b52c: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts × searches only the blocks the gate passes 58ms × searches only the blocks the gate passes on each export 16ms × reads the whole original and the whole output once each 28ms × repairs at least what parse verdicts do, in few parses, with peek 1183ms × records once, after its second parse verdict, in the search on 3ms Tests 26 failed | 39 passed (65) ``` ### After ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1226 passed (1226) $ pnpm run test-unit Tests 8 failed | 41176 passed | 423 expected fail | 174 skipped (41781) ``` The 8 failures are `packages/lexical-playground/__tests__/unit/DateTimeComponent.test.tsx`, which fails the same 8 on 179b52c when run on 2026-12-01 (KST); this change does not touch the playground. Production build (esbuild, `__DEV__=false`), 1 MB unless noted, median of 3 alternated rounds, ms: | case | before meta#9983 (60926d0) | origin/main | this | |---|---:|---:|---:| | GFM, one repair, 10 KB / 1 MB | 0.4 / 46 | 7.7 / 915 | 0.6 / 55 | | GFM, gated but no repair | 46 | 571 | 60 | | GFM, list / quote over half the export, one repair | 57 / 30 | 1,519 / 1,435 | 71 / 50 | | syntax of your own (`$`), one repair, 10 KB / 1 MB | 0.3 / 40 | 6.3 / 738 | 6.9 / 786 | | syntax of your own, rejected | 40 | 1,061 | 53 | | syntax of your own reading every edit, 14 KB, 400 repairs | — | 108,730 | 359 | | stateful syntax of your own, model wrong (1.26 MB) | 120 | 2,161 | 2,400 | Outputs: on 18 corpora (generated, literal, bracket-heavy, adversarial, large documents), no input gets fewer references than 179b52c with the budget off, and every output reads as its input in micromark; 31 inputs get more. Known costs: with syntax of your own a repair still reads the whole document twice (as origin/main); stateful syntax where the model is wrong is 1.11–1.27× origin/main; adversarial inputs whose rejected edits spread over many paragraphs are up to 6.9× origin/main (97 ms); a 1 MB block quote cut into 33,000 parts is 20 ms over the time before meta#9983.
…your own, and cut an export that is one list ## Description Two defects in the block-by-block search of the strict export: - The whole original and the whole output were compared only beside syntax of your own that the pairing model can judge (`isForeignSyntax`). A text construct of your own at `&`, `*`, `_` or `~` turns the model off and was taken for syntax lexical knows, so the small document's check alone decided, and a repair that syntax read otherwise across blocks was kept (`%` marking a state that a construct at `©` reads: `x ©_a_` became `x ©_a_`, which micromark reads otherwise). The whole check now runs unless every extension is syntax lexical knows. - An export that is a single list or block quote was never cut, so it took the whole path (1 MB: 1,007 ms on origin/main, the same here, against 16 ms before meta#9983). A single block is now cut like any other list or quote over half the export. The verdict that makes the fallback record its reads is now decided by its parse instead of being judged again with the records; that re-judging saved no parse on any input tried. The changeset sentence left without a verb when the budget was removed is rewritten. ## Test plan ### Before The new tests on 13bb1c5: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts × keeps no such edit beside a text construct of your own at 38 → expected false to be true × searches only the parts the gate passes where a tight list is the export → expected 1260 to be less than 630 Tests 6 failed | 69 passed (75) ``` ### After ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1238 passed (1238) ``` Production build, 1 MB, one repair, median of 3 alternated rounds, ms: | case | before meta#9983 | origin/main | previous commit | this | |---|---:|---:|---:|---:| | a text construct of your own at `&` | 15.7 | 867.7 | 21.1 | 831.1 | | the same, nothing to repair | 15.1 | 16.9 | 13.5 | 18.7 | | the export is one list | 15.7 | 1,006.9 | — | 28.8 | | the export is one block quote | 10.3 | 996.9 | — | 26.2 | With a construct of your own at `&`, a repair now costs the two whole parses that every syntax of your own does.
…al provides ## Description The strict export writes character references where cmark-gfm (GitHub's renderer) reads a `*`, `_` or `~` run otherwise than micromark, checking each by reading the output again. Beside micromark extensions lexical does not know, parser state can cross blocks, so that check needs whole-document parses: a single repair in a 1 MB export cost about 900 ms, and inputs with many repairs reached 10× origin/main. GitHub does not read such syntax itself, so a document that uses it does not read there as it does in lexical anyway. The pass now runs only when every micromark extension is syntax lexical provides (emphasis, strikethrough, task list items, literal autolinks, tables, as lexical marked them) or only disables `autolink`, `codeIndented`, `htmlFlow` or `htmlText`. Any other extension, even one the document never uses and even a GFM extension added directly such as `gfmTable()`, turns the pass off for the whole export, which then writes what it wrote before meta#9983 (`x ©_a_`). The code that served only that case is removed (`foreignReads.ts`, the recording of what constructs read, the fallback that searched the whole document again). A marked extension now also keeps the functions of its constructs as they were when lexical marked it, so one whose construct is changed afterwards is not taken for lexical's. The README paragraph and the changesets say so, and the README no longer mentions a parse budget. ## Test plan ### Before The new tests on b3812d5: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmModel.test.ts × skips the pass, and parses nothing 22ms × skips the pass in the cells of a table 33ms × skips the pass beside GFM tables put in directly 12ms × skips the pass beside a copy of the syntax lexical has for the literals, with its marks 14ms × skips the pass where a construct takes the place of one in the syntax lexical has for the literals 15ms AssertionError: expected '©_a_' to be '©_a_' // Object.is equality ``` (25 failures in all.) ### After ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1183 passed (1183) $ pnpm run test-unit Tests 8 failed | 41132 passed | 423 expected fail | 174 skipped (41737) ``` The 8 failures are `packages/lexical-playground/__tests__/unit/DateTimeComponent.test.tsx`, which fails the same on 179b52c on 2026-12-01; `tsc`, `tsc-test`, `build` and the changeset check pass. Production build, median of 3 alternated rounds, ms: | case | before meta#9983 | origin/main | previous commit | this | |---|---:|---:|---:|---:| | GFM, one repair, 1 MB | 46.4 | 925 | 55.5 | 55.2 | | GFM, rejected, 1 MB | 46.7 | 568 | 60.3 | 60.0 | | `$` syntax of your own, one repair, 1 MB | 40.7 | 726 | 906 | 43.6 | | `$` syntax of your own, rejected, 1 MB | 40.3 | 1,045 | 1,315 | 43.1 | | stateful syntax of your own, model wrong | 125 | 2,121 | 2,365 | 131 | Outputs: with syntax lexical provides, 2,130 inputs export as in the previous commit; with syntax of your own, 8,165 inputs export as before meta#9983.
… with micromark syntax of your own The cmark-gfm pass of a strict export (meta#9983) judges each character reference it proposes by parsing the document again, unless the pairing model (meta#9988) judges it. The model only judged the stock configuration, so with any other `micromarkExtensions` entry, a `disable`, or without GFM strikethrough, a long paragraph whose proposals keep being rejected took quadratic time: a 7.2 KB paragraph exported in about 16 s (production build), 57 s in a development build. This change makes the model judge those configurations too, with the same output: - Without lexical's strikethrough syntax, the model reads `~` as text. - A `disable` of only `autolink`, `codeIndented`, `htmlFlow` or `htmlText` keeps the model on. - Beside other syntax (`isForeignSyntax`: no construct at a marker or at `&`, no attention markers, no span resolver), the first parse records what each of its constructs read (`parseRecordingForeignReads`): the character before an attempt, what it and the attempts inside it consumed, the character after, what it serializes, the content before it where it reads the events, and the check before an attempt. A proposal that none of them read is judged by the model; one that any read, or one in a subject where such a construct with a resolver succeeded, or once such an edit is accepted, by a parse. - A parse budget per export (65,536 characters, and four times the length of the document and of the cells of each table) bounds the parses that remain; past it, a proposal that only a parse judges reads as changing the reading, and the search goes on with the model's verdicts. On origin/main (0e6fc89), production build (esbuild, `__DEV__` false), one paragraph of `~~_a\~\~ba\~\~b_€a_b\a~~___`!_.`___` repeated 200 times (7.2 KB): ``` math-like text construct at `$` 16201 ms 200 references disable codeIndented, … 16033 ms 200 references ``` ``` math-like text construct at `$` 48 ms 200 references disable codeIndented, … 45 ms 200 references ``` With the budget off, the export equals origin/main's for 26,000 generated documents across 14 configurations (stock GFM, CommonMark only, footnotes, a math-like construct, a table syntax copy, disables, constructs that read the events, the character before them, a parser flag across a line, a tab or a backslash outside the text). ``` $ pnpm vitest run --project unit packages/lexical-mdast Test Files 21 passed (21) Tests 1098 passed (1098) $ pnpm run test-unit Test Files 1039 passed | 6 skipped (1045) Tests 41055 passed | 423 expected fail | 174 skipped (41652) ```
…hout a parse budget The strict export checks every character reference it writes for cmark-gfm by reading the document again with micromark. Any repair read the whole document twice, so one `x ©_a_` in a 1 MB export took 915 ms on origin/main against 46 ms before meta#9983, and a `~~**x**~~` that the gate catches but needs no repair cost a whole parse too. The parse budget that kept the slowest searches short also left repairs undone when it ran out (0 of 400 in a 14 KB paragraph that syntax of your own reads). The export now records the string each top-level block writes and searches only the blocks the gate passes, together with every block that holds a definition, as a small document, then writes the repairs back. A list or block quote that is more than half the export is cut at the strings its items or children wrote. This takes effect only where every block was written by the default handlers (lexical's table and HTML-block handlers are registered as such) and the serializer configuration is lexical's own; otherwise the whole document is searched as before. With syntax lexical knows, the small document's final check decides. With syntax of your own, which may carry state across blocks, the whole original and the whole output are read once each and compared. When that whole check fails, the edits are kept by halves of the search's accepted attempts (a pair is never split), then searched on from there; this starts without records and makes one recording parse, through the same I5 comparison as before, only once it needs a third parse verdict, so that the per-start learning applies only where it pays. The parse budget (`CMARK_GFM_PARSE_BUDGET`) is removed: every candidate is decided, by the model or by a parse. `.changeset/mdast-cmark-gfm-model.md` (unreleased meta#9983 entry) now states the block-by-block search instead of the budget. The new tests on 179b52c: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts × searches only the blocks the gate passes 58ms × searches only the blocks the gate passes on each export 16ms × reads the whole original and the whole output once each 28ms × repairs at least what parse verdicts do, in few parses, with peek 1183ms × records once, after its second parse verdict, in the search on 3ms Tests 26 failed | 39 passed (65) ``` ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1226 passed (1226) $ pnpm run test-unit Tests 8 failed | 41176 passed | 423 expected fail | 174 skipped (41781) ``` The 8 failures are `packages/lexical-playground/__tests__/unit/DateTimeComponent.test.tsx`, which fails the same 8 on 179b52c when run on 2026-12-01 (KST); this change does not touch the playground. Production build (esbuild, `__DEV__=false`), 1 MB unless noted, median of 3 alternated rounds, ms: | case | before meta#9983 (60926d0) | origin/main | this | |---|---:|---:|---:| | GFM, one repair, 10 KB / 1 MB | 0.4 / 46 | 7.7 / 915 | 0.6 / 55 | | GFM, gated but no repair | 46 | 571 | 60 | | GFM, list / quote over half the export, one repair | 57 / 30 | 1,519 / 1,435 | 71 / 50 | | syntax of your own (`$`), one repair, 10 KB / 1 MB | 0.3 / 40 | 6.3 / 738 | 6.9 / 786 | | syntax of your own, rejected | 40 | 1,061 | 53 | | syntax of your own reading every edit, 14 KB, 400 repairs | — | 108,730 | 359 | | stateful syntax of your own, model wrong (1.26 MB) | 120 | 2,161 | 2,400 | Outputs: on 18 corpora (generated, literal, bracket-heavy, adversarial, large documents), no input gets fewer references than 179b52c with the budget off, and every output reads as its input in micromark; 31 inputs get more. Known costs: with syntax of your own a repair still reads the whole document twice (as origin/main); stateful syntax where the model is wrong is 1.11–1.27× origin/main; adversarial inputs whose rejected edits spread over many paragraphs are up to 6.9× origin/main (97 ms); a 1 MB block quote cut into 33,000 parts is 20 ms over the time before meta#9983.
…your own, and cut an export that is one list Two defects in the block-by-block search of the strict export: - The whole original and the whole output were compared only beside syntax of your own that the pairing model can judge (`isForeignSyntax`). A text construct of your own at `&`, `*`, `_` or `~` turns the model off and was taken for syntax lexical knows, so the small document's check alone decided, and a repair that syntax read otherwise across blocks was kept (`%` marking a state that a construct at `©` reads: `x ©_a_` became `x ©_a_`, which micromark reads otherwise). The whole check now runs unless every extension is syntax lexical knows. - An export that is a single list or block quote was never cut, so it took the whole path (1 MB: 1,007 ms on origin/main, the same here, against 16 ms before meta#9983). A single block is now cut like any other list or quote over half the export. The verdict that makes the fallback record its reads is now decided by its parse instead of being judged again with the records; that re-judging saved no parse on any input tried. The changeset sentence left without a verb when the budget was removed is rewritten. The new tests on 13bb1c5: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts × keeps no such edit beside a text construct of your own at 38 → expected false to be true × searches only the parts the gate passes where a tight list is the export → expected 1260 to be less than 630 Tests 6 failed | 69 passed (75) ``` ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1238 passed (1238) ``` Production build, 1 MB, one repair, median of 3 alternated rounds, ms: | case | before meta#9983 | origin/main | previous commit | this | |---|---:|---:|---:|---:| | a text construct of your own at `&` | 15.7 | 867.7 | 21.1 | 831.1 | | the same, nothing to repair | 15.1 | 16.9 | 13.5 | 18.7 | | the export is one list | 15.7 | 1,006.9 | — | 28.8 | | the export is one block quote | 10.3 | 996.9 | — | 26.2 | With a construct of your own at `&`, a repair now costs the two whole parses that every syntax of your own does.
…al provides The strict export writes character references where cmark-gfm (GitHub's renderer) reads a `*`, `_` or `~` run otherwise than micromark, checking each by reading the output again. Beside micromark extensions lexical does not know, parser state can cross blocks, so that check needs whole-document parses: a single repair in a 1 MB export cost about 900 ms, and inputs with many repairs reached 10× origin/main. GitHub does not read such syntax itself, so a document that uses it does not read there as it does in lexical anyway. The pass now runs only when every micromark extension is syntax lexical provides (emphasis, strikethrough, task list items, literal autolinks, tables, as lexical marked them) or only disables `autolink`, `codeIndented`, `htmlFlow` or `htmlText`. Any other extension, even one the document never uses and even a GFM extension added directly such as `gfmTable()`, turns the pass off for the whole export, which then writes what it wrote before meta#9983 (`x ©_a_`). The code that served only that case is removed (`foreignReads.ts`, the recording of what constructs read, the fallback that searched the whole document again). A marked extension now also keeps the functions of its constructs as they were when lexical marked it, so one whose construct is changed afterwards is not taken for lexical's. The README paragraph and the changesets say so, and the README no longer mentions a parse budget. The new tests on b3812d5: ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmBlocks.test.ts packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfmModel.test.ts × skips the pass, and parses nothing 22ms × skips the pass in the cells of a table 33ms × skips the pass beside GFM tables put in directly 12ms × skips the pass beside a copy of the syntax lexical has for the literals, with its marks 14ms × skips the pass where a construct takes the place of one in the syntax lexical has for the literals 15ms AssertionError: expected '©_a_' to be '©_a_' // Object.is equality ``` (25 failures in all.) ``` $ TZ=UTC ./node_modules/.bin/vitest run --project unit packages/lexical-mdast Tests 1183 passed (1183) $ pnpm run test-unit Tests 8 failed | 41132 passed | 423 expected fail | 174 skipped (41737) ``` The 8 failures are `packages/lexical-playground/__tests__/unit/DateTimeComponent.test.tsx`, which fails the same on 179b52c on 2026-12-01; `tsc`, `tsc-test`, `build` and the changeset check pass. Production build, median of 3 alternated rounds, ms: | case | before meta#9983 | origin/main | previous commit | this | |---|---:|---:|---:|---:| | GFM, one repair, 1 MB | 46.4 | 925 | 55.5 | 55.2 | | GFM, rejected, 1 MB | 46.7 | 568 | 60.3 | 60.0 | | `$` syntax of your own, one repair, 1 MB | 40.7 | 726 | 906 | 43.6 | | `$` syntax of your own, rejected, 1 MB | 40.3 | 1,045 | 1,315 | 43.1 | | stateful syntax of your own, model wrong | 125 | 2,121 | 2,365 | 131 | Outputs: with syntax lexical provides, 2,130 inputs export as in the previous commit; with syntax of your own, 8,165 inputs export as before meta#9983.
…of your own and the tight-list repair in new changesets ## Description 0.54.0 released the changesets of meta#9983 and meta#9988 that this branch had extended, so what this branch changes since 0.54.0 now has entries of its own: a `[perf]` one for the block search, saying when the whole document is still searched; a `[fix]` one, with `Verify:`, for writing the references only beside lexical's syntax, which also notes that lexical's syntax objects are now frozen copies; and a `[fix]` one, with `Verify:`, for the tight-list repair. ## Test plan ### After `pnpm run check-changesets`: `Changeset check passed: 3 pending changeset(s) valid.` Against the 0.54.0 sources, the same export rule writes `©_a_` with `micromarkExtensions: [gfmTable()]` and `- x ©_a_` in the tight list, and writing to lexical's syntax objects does not throw; on this branch they write `©_a_` and `- x ©_a_`, and the write throws a `TypeError`. With `bullet: '*'`, `fences: false`, a closed comment or a closed `<div>` around a list line, a 200-paragraph export with one repair parses the whole document (5,701 to 5,719 characters); with the defaults its longest parse is 47.
Description
Two commits.
Join touching strikethrough. An export rule that returns a
deletewithdata, or an inline decorator that returns one, is kept as it is instead of grouped with the text around it, so twodeletenodes can end up side by side. Markdown has no way to write those apart:~~a~~~~b~~reads back as one strikethrough ofa~~~~b. Markdown export now joins touchingdeletenodes while the stock strikethrough handler writes them, keeping the first one's fields;$convertToMdast()still returns both. The README sentence that such middleware "must ensure its output serializes correctly on its own" now says that means each node by itself.Write what cmark-gfm needs in a strict export. Reading the export with cmark-gfm 0.29.0.gfm.14 (what GitHub renders with) and markdown-it 14 and 15 showed two more places where cmark-gfm reads
*/_runs otherwise, unrelated to the join. It skips a~run and classifies the character beyond it, so~~a~~*`b`*keeps its asterisks on GitHub. It reads a symbol such as©as a letter, as CommonMark did before 0.31, so©_a_keeps its underscores. A strict export (exportPunctuation: 'strict-commonmark', the default) now re-reads the written Markdown with micromark and writes the character cmark-gfm reads beside such a run as a reference where that fixes the run (~~a~~*`b`*,©_a_). A reference is only kept if micromark reads every paragraph and the whole document as before, and only where it cannot change how markdown-it reads the text: a backtick that micromark does not read as syntax on a paragraph's lines, or on the lines joined to it, leaves the paragraph as it is, and a*,_or~it does not read as syntax keeps the runs of that marker beside a reference as they read; no reference goes in a word that linkify-it or cmark-gfm's bare-URL scanner may link, in an image or reference label, or in a table cell with brackets other than those of an inline link, with the same decisions when measuring column widths.exportPunctuationand the README list what stays different. A'cjk-friendly'export is unchanged.Design notes
Joining while writing, not in the tree. Joining the
deletenodes where the export appends rule output also changed what$convertToMdast()returns, so the join works on a copy of the tree that only Markdown serialization reads, and only while the stock strikethrough handler writes them: adeletehandler of your own still gets both nodes.Reading the written Markdown back instead of predicting it. I first tried adding non-ASCII symbols to the characters the export already stands in for and references where readers disagree (emoji, selectors). That decides from the flags of each run alone, so it changed more than twice as many outputs as needed and made cmark-gfm newly misread some that it read right. Reading the result with micromark gives the role each run actually has, so a reference is written only where cmark-gfm would miss a flag that role needs.
Guards on raw characters, not on constructs. markdown-it reads some text as plain text where micromark reads a link, an autolink, raw HTML or a definition (rejected schemes,
html: false, lines joined into a paragraph), and a~or backtick in it can then pair with what a reference changed. Listing those constructs kept missing one more, so the guard counts the backticks and markers on the subject's lines and the contiguous lines around it that micromark does not read as syntax. The word guard likewise takes its link shapes from linkify-it's and cmark-gfm's grammars: a:before more of the word,//,@,.and two letters, or digits around a., in a word bounded by Unicode spaces, and a bare URL bounded only by an ASCII space, a tab, a line ending or<.Cost. On 43 ordinary documents, export took about 4% longer. When every batch of references is accepted, the pass parses the Markdown twice, the second time for the final check; a rejected attempt costs more parses (the restart and split tests take three and seven). So a single paragraph with thousands of formatting runs costs at least two micromark parses of it (32 KB with 6,000 runs: 35 ms before, about 750 ms after), the order of importing the same text. The region and word checks are indexed once per document.
Not changed here: punctuation newer than cmark-gfm's Unicode table (
⹏_a_), runs of three or more markers that a pair uses only in part, characters without a lossless reference, and what a guard leaves out on purpose. These are listed inexportPunctuation.The tests are long because each guard of the second commit was added for an input where a reader newly misread the output, and each has a test that fails when the guard is removed, checked by mutation. The one exception is the CommonMark half of the nested
~~check, which only blocks references and which no input we found reaches; it is kept for markdown-it, which reads symbols as CommonMark does.Changeset
.changeset/mdast-touching-deletes.mdand.changeset/mdast-cmark-gfm-references.md(@lexical/mdastpatch).Test plan
Before
TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastCmarkGfm.test.ts packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.tswith the source ofmain:GitHub's
/markdownAPI on those exports:After
GitHub's
/markdownAPI:Generated inputs (5.4 million, from both commits' cases, editor exports, fuzzers and enumerations) were read before and after with micromark, cmark-gfm and markdown-it 14 and 15 (default,
linkify,fuzzyLink,fuzzyIP): no output that a reader read as micromark does before is read otherwise after. That run was on the patch before its last change, which only leaves morewww.URLs alone.pnpm run tsc,tsc-test,flow,prettier,eslint,check-changesets,build, and the tree-shaking tests (source and published) pass. The browser tests above are the ones that use@lexical/mdast. No e2e test was run: the playground does not use@lexical/mdast.