[lexical-mdast] Bug Fix: Write an autolink literal in resource or autolink form when what is beside it would change how it reads back #9965
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #9965"
general
weight: 0.35
potatowagon
Description
An imported autolink literal goes out bare, and since #9669 only when its text on its own still parses as the same link. What ends up next to it can still change how it reads back. Add bold
yright afterwww.example.comand the export iswww.example.comy; import that again and you get a link tohttp://www.example.comywith a strayafter it. A letter, code or punctuation the link runs into does the same, so does a letter right before it, and so do the writer's own escapes: an untouchedwww.example.com_exports aswww.example.com\_, and the link swallows the backslash. This is the case the review of #9956 pointed out.With this change, each paragraph, heading or table cell is read back after it is written. If its links don't come back as they went in, the literals in it are written again in resource or autolink form, which can't run into their neighbours:
www.example.comyxwww.example.comwww.example.com\_Everything that already round-trips keeps its bare form.
see www.example.com, then,(www.example.com)and# www.example.comexport exactly as they do onmain.Design notes
Reading the container back instead of checking the neighbours. I first tried to decide inside the link handler by looking ahead at the following siblings. It kept missing things the writer does after the handler has returned: attention encodes the literal's last character (
www.example.com!), a table cell encodes its trailing space, an emoji is written through a stand-in, and a CJK-friendly export rewrites references at the very end. Each one needed its own rule. Reading back what was actually written covers all of them with one rule, so the check reads the container with stand-ins put back and with those edge and CJK rewrites applied. Re-parsing the whole document would also work, but mapping a broken link back to its node is awkward and it roughly doubles the cost of every export.Cost. A literal that sits between spaces, with nothing but
?!.,:after it, can't be changed by its neighbours, so it skips the parse. With that, a 2,000-paragraph document exports within a few milliseconds ofmain(50 ms here against 48 ms), and the parses share #9669's cache, so an unchanged container is not parsed again. A paragraph with a literal that does touch something is parsed again after each edit, in time that grows with its length: a 10 KB paragraph takes about 1.5 ms per export, against 0.13 ms onmain. The slow case is a single paragraph packed with 2,000 literals that touch punctuation: after an edit it takes about 59–70 ms per export, against 14–25 ms onmain. These times are from the bundled source run in Node; under Vitest the same parse of a 10 KB paragraph is about 16 times slower.The cache for subtree and selection exports. Only a document export prunes #9669's cache. Adding container strings to it during subtree exports made it grow with every edit (1,000 subtree exports left 1,001 entries, against 1 on
main), and not caching them re-parsed an unchanged paragraph every time. So those exports keep their own cache by block: each block an export writes keeps the containers the latest export read in it, and the export's root keeps those outside its children. An edited block's old versions go when it is written again, and blocks exported in turn don't parse each other's containers again, so there is no limit to pick. The root handler sets the block while it writes each child, so a cell that a table copies, or a node that a rule or a handler writes, is still read for its block. Removed blocks go at the next document export, or once there are more than twice as many blocks as the last time they were checked. With no document export in between, exporting 1,000 blocks in turn ten times over takes 222 ms here against 184 ms onmain.Handlers that write outside a container aren't checked. If a contributed
paragraphhandler appends a letter after the default output, the literal can still run into it, as it does onmain. That letter is not in the tree, so the container check can't see it, and catching it would mean re-reading every block or the whole document. A handler that writes next to phrasing content hasinfo.before/info.afterandstate.safefor this, and the same problem would hit emphasis or code next to it.Contributed handlers run again when a container is rewritten. mdast-util-to-markdown already calls a handler more than once when it peeks at the next sibling, so handlers already have to give the same output when called again. This only adds calls for containers that had to be rewritten.
Not changed here, same as on
main: escaped plain text that GFM reads back as a link (me\@x.co,www\.example.com), and a literal imported through the transform path ("www.example.com"), which has no position and so keeps exporting as a resource link.The tests are long because the cases differ only in what sits next to the literal, and the writer handles each neighbour differently. Every row runs in both
exportPunctuationmodes and checks that the output reads back to the same text, formats and links.Changeset
.changeset/mdast-literal-neighbours.md:[fix]for@lexical/mdast, with aVerify:paragraph.Test plan
Before
TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.tswithMdastExport.tsfrommain:The cache tests fail on
mainbecause it never reads a container back, so it parses less; the ones that expect no parse pass there. The CJK-friendly row ofchecks a literal as the %s export writes it, the heading-tab row, the bare cases added toround-trips autolink literals without normalizing to <...>,keeps a literal bare beside a link whose text holds NUL,keeps a literal bare when plain text after it reads back as a link,writes an edited literal in resource formand both stand-in tests pass onmain. They are there to keep whatmainalready gets right.After
pnpm run tsc,pnpm run tsc-test,pnpm run flow(0 errors),pnpm run prettier,pnpm exec eslinton the changed files,pnpm run buildandpnpm run check-changesetspass. #9956 also changesMdastExport.ts; merged with it, the@lexical/mdastunit tests still pass (620). There are no browser or e2e tests because the playground doesn't use@lexical/mdast.