← Back to Pull requests

[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

Closed
unknown closed this pull request without merging on Nov 22, 2026 18:38 UTC
View on GitHub
unknown unknown commented on Nov 22, 2026 02:58 UTC
Author

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 y right after www.example.com and the export is www.example.comy; import that again and you get a link to http://www.example.comy with a stray after 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 untouched www.example.com_ exports as www.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.comy

  • xwww.example.com

  • www.example.com\_
  • Everything that already round-trips keeps its bare form. see www.example.com, then, (www.example.com) and # www.example.com export exactly as they do on main.

    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 of main (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 on main. 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 on main. 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 on main.

    Handlers that write outside a container aren't checked. If a contributed paragraph handler appends a letter after the default output, the literal can still run into it, as it does on main. 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 has info.before/info.after and state.safe for 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 exportPunctuation modes 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 a Verify: paragraph.

    Test plan

    Before

    TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts with MdastExport.ts from main:

    × writes a literal in resource form when it would not read back bare: bold after
    × writes a literal in resource form when it would not read back bare: code after
    × writes a literal in resource form when it would not read back bare: a letter after
    × writes a literal in resource form when it would not read back bare: bold after an https literal
    × writes a literal in resource form when it would not read back bare: a letter before
    × writes a literal in resource form when it would not read back bare: bold punctuation after
    × writes a literal in resource form when it would not read back bare: `!` before and `<` after
    × writes a literal in resource form when it would not read back bare: an escaped bracket before
    × writes a literal in resource form when it would not read back bare: an emoji before
    × writes a literal in resource form when it would not read back bare: a second literal with the same text
    × writes a literal in resource form when it would not read back bare: ideographic spaces around
    × writes a literal in resource form when it would not read back bare: the same text in code
    × writes a literal in resource form when it would not read back bare: a parenthesis after a literal that opened one
    × writes a literal in resource form when it would not read back bare: a semicolon after a literal ending in an entity name
    × writes a literal in resource form when it would not read back bare: a space after a literal at the end of a table cell
    × writes a literal in resource form when it would not read back bare: a letter after a literal in an escaped heading marker
    × writes a literal in resource form when it would not read back bare: an underscore before an email literal
    × writes a literal in resource form when it would not read back bare: an escaped underscore after
    × writes a literal in resource form when it would not read back bare: an escaped underscore after an https literal
    × reads each container back once across repeated exports
    × drops the old containers of an edited block
    × drops the containers of removed blocks at a document export
    × drops the containers of removed blocks once twice as many are kept
    × counts only the blocks still kept toward the next check
    × drops a block that the update exporting the document removes
    × drops the old containers of an edited block after a rule runs another export
    × drops the old containers of an edited block after an export throws
    × drops the old containers of a block that reads none now
    × drops the old containers of a block a selection writes
    × leaves what it finds in a document export to that export
    × keeps only what the latest document export read
    × drops the old containers of a block whose rule hands back a node another block was written with
    × drops the old containers of a block whose handler writes copies of its children
    × drops the old containers of a block a handler writes with a node from an earlier export
    × drops the old containers the root keeps when a root handler writes copies of its children
    × drops the old containers of a block a handler writes with a block from an earlier export
    × keeps one version of a block that subtree and selection exports both write
    × checks a literal as the strict-commonmark export writes it
    
    AssertionError: expected 'www.example.com**y**' to be '[www.example.com](http://www.example.…' // Object.is equality
    
    Tests  38 failed | 149 passed (187)
    

    The cache tests fail on main because it never reads a container back, so it parses less; the ones that expect no parse pass there. The CJK-friendly row of checks a literal as the %s export writes it, the heading-tab row, the bare cases added to round-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 form and both stand-in tests pass on main. They are there to keep what main already gets right.

    After

    $ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
    Tests  187 passed (187)
    
    $ pnpm run test-unit
    Test Files  1022 passed | 6 skipped (1028)
    Tests  40322 passed | 423 expected fail | 174 skipped (40919)
    

    pnpm run tsc, pnpm run tsc-test, pnpm run flow (0 errors), pnpm run prettier, pnpm exec eslint on the changed files, pnpm run build and pnpm run check-changesets pass. #9956 also changes MdastExport.ts; merged with it, the @lexical/mdast unit tests still pass (620). There are no browser or e2e tests because the playground doesn't use @lexical/mdast.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: potatowagon Review duration: 939m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%
    Repository