Skip to content

[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

Merged
potatowagon merged 2 commits into
meta:mainfrom
mayrang:fix/mdast-literal-neighbours
Nov 22, 2026
Merged

potatowagon merged 2 commits into
meta:mainfrom
mayrang:fix/mdast-literal-neighbours

Conversation

@mayrang

@mayrang mayrang commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

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.com**y**; import that again and you get a link to http://www.example.com**y 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.com](http://www.example.com)**y**
  • x[www.example.com](http://www.example.com)
  • [www.example.com](http://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.

…olink form when what is beside it would change how it reads back

## Description

An imported GFM autolink literal is exported bare while its text alone parses as the same link (meta#9669). What is written beside it can still change how it reads back: with bold `y` added after `www.example.com`, the export is `www.example.com**y**`, which reimports as a link to `http://www.example.com**y` followed by a literal `**`. The same happens with a letter, code or punctuation it runs into, a letter right before it, and the writer's own escapes and references: unedited `www.example.com_` exports as `www.example.com\_`, and the link takes in the backslash.

This reads each phrasing container (a paragraph, a heading, a table cell) back once it is written, with the references a heading or a table cell writes at its edges and, in a CJK-friendly export, the characters it writes for references. When its links do not come back as written, every literal in it is written again in resource or autolink form: `[www.example.com](http://www.example.com)**y**`, `x[www.example.com](http://www.example.com)`, `[www.example.com](http://www.example.com)\_`. Links compare by text and destination, with NUL as it reads back. A literal between ASCII whitespace, with only trailing `?!.,:` after it, skips the parse. The parses share meta#9669's cache, so a document export parses no unchanged container again; a subtree or selection export keeps only the containers it read until the next one, so exporting the same content again parses nothing, while exporting other blocks in turn parses each. The check wraps `state.containerPhrasing` from a `root` handler that delegates to the configured one, and reads the written string with stand-ins put back.

Escaped plain text that GFM reads back as a link (`me\@x.co`, `www\.example.com`) is unchanged and out of scope, as is text a contributed handler writes outside a phrasing container; main writes both the same way.

## 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
× keeps the containers of the latest subtree or selection export only
× 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  22 failed | 132 passed (154)
```

`reads each container back once across repeated exports` and `keeps the containers of the latest subtree or selection export only` fail because `main` does not read containers back, so it parses less. The table runs each row in both `exportPunctuation` modes; the strict row of `checks a literal as the %s export writes it` fails on `main`, and its CJK-friendly row guards a bare form `main` keeps. The heading-tab row, the added bare cases in `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 guard what main already gets right.

### After

```
$ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
Tests  154 passed (154)

$ pnpm run test-unit
Test Files  1022 passed | 6 skipped (1028)
Tests  40289 passed | 423 expected fail | 174 skipped (40886)
```

`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. No browser or e2e tests: the playground does not use `@lexical/mdast`.
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 22, 2026 2:18pm UTC
lexical-playground Ready Ready Preview Nov 22, 2026 2:18pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 22, 2026
@rootnode-bot

rootnode-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 22, 2026, 2:38 PM UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9965

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9965

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9965

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9965

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9965

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9965

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9965

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9965

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9965

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9965

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9965

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9965

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9965

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9965

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9965

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9965

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9965

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9965

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9965

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9965

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9965

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9965

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9965

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9965

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9965

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9965

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9965

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9965

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9965

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9965

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9965

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9965

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9965

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9965

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9965

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9965

commit: 5b3ca34

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good. Reading each container back after it is written is a sound way to decide whether a literal can stay bare. It applies GFM's extended-autolink rules without restating them: the start condition, the trailing punctuation, balanced parentheses and entity-like ;. The isolated-token shortcut is conservative: anything but an exact whitespace-separated token goes to the parse.

I checked it independently. The new tests fail 22 of 154 on main and pass here. I also generated a sweep of 26,880 export → reimport cases: seven literal kinds, neighbours before or after in five formats, in paragraphs, headings, table cells, list items and quotes, in both exportPunctuation modes. It found 14,292 cases fixed, none regressed, and no change in output wherever main already round-tripped. The only cases still failing are the two you list as unchanged: me\@x.co, and literals reimported through the transform path.

Optional, non-blocking:

  • packages/lexical-mdast/src/MdastExport.ts:1498 (partLinks): only the latest subtree or selection export's containers are kept, so exporting several blocks in turn re-parses every container each time. Exporting 50 blocks ten times over took about 250 ms here against 22 ms on main, a larger gap than the 81 ms vs 19 ms in the description. One option is to keep part-export containers until the next document export prunes them, with a fixed cap, so a caller that alternates among a few blocks reuses them while memory stays bounded. It is fine as a follow-up, since you had no data to pick a size from.

Automated review (maintainer link)

… for each block

## Description

Subtree and selection exports kept only the containers of their latest export, so a caller that exported several blocks in turn, with no document export in between, parsed every container again each time. Now each block such an export writes keeps the containers the latest export read in it, and the export's root keeps those written outside its children. An edited block's old containers go when it is written again, so they do not add up however often it changes, and there is no limit to pick: what is kept is the latest version of each block that was exported. The containers of 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. To know which block a container belongs to, the export root's children are turned into mdast one at a time, which is how the exporter already does it, so the tree is the same; a container read while one of them is written belongs to its block, whatever a handler or a table copies inside it. An export that a rule or a handler runs while another is built or written keeps what it reads with the outer one, which is kept once that one is done or has thrown. A container that a document export already has is found there and kept by it, and a selection's container stays kept across a document export. A contributed `root` handler is called with the state as `this` again, as on `main`; the previous commit called it without one.

Blocks exported in turn, ten times over, with no document export (ms, the last of three runs; "before" is the previous commit; the bundled source run in Node):

| blocks | `main` | before | this commit |
| --- | --- | --- | --- |
| 10 | 1.6 | 6.3 | 1.9 |
| 50 | 7.2 | 28.5 | 7.5 |
| 100 | 12.1 | 50.6 | 16.9 |
| 300 | 39.1 | 145 | 47.5 |
| 1,000 | 184 | 543 | 222 |
| 1,100 | 213 | 606 | 253 |
| 3,000 | 1,174 | 2,258 | 1,274 |

## Test plan

### Before

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

```
× calls contributed root and paragraph handlers with the state as this in a document export
× calls contributed root and paragraph handlers with the state as this in a subtree export
× calls contributed root and paragraph handlers with the state as this in a selection export
× keeps the containers of every block exported in turn
× drops the containers of removed blocks at a document export
× drops a block that the update exporting the document removes
× keeps what an export read when a handler runs another export
× keeps what another block reads when a block that read it reads none
× keeps the containers of selections within different blocks
× keeps the containers of selections within different quotes
× leaves what it finds in a document export to that export
× keeps the containers of selections within different tables
× keeps the containers of selections within tables whose first link holds a break
× keeps the containers of a block a rule writes as several nodes
× keeps the containers of blocks that a rule writes with the same node
× keeps the containers of a block a handler writes as a copy
× keeps the block of a container a handler writes after its next sibling
× keeps the block of a container a handler writes after its next sibling throws
AssertionError: expected Set{ 'root false', 'paragraph true' } to deeply equal Set{ 'paragraph true', 'root true' }
AssertionError: expected Set{ 'root false' } to deeply equal Set{ 'root true' }
AssertionError: expected 3 to be +0 // Object.is equality
AssertionError: expected +0 to be 1 // Object.is equality
AssertionError: expected +0 to be 1 // Object.is equality
AssertionError: expected 4 to be +0 // Object.is equality
AssertionError: expected 1 to be +0 // Object.is equality
AssertionError: expected 2 to be +0 // Object.is equality
AssertionError: expected 2 to be +0 // Object.is equality
AssertionError: expected +0 to be 1 // Object.is equality
AssertionError: expected 4 to be +0 // Object.is equality
AssertionError: expected 4 to be +0 // Object.is equality
AssertionError: expected 2 to be +0 // Object.is equality
AssertionError: expected 1 to be +0 // Object.is equality
AssertionError: expected 3 to be +0 // Object.is equality
AssertionError: expected 1 to be +0 // Object.is equality
AssertionError: expected 1 to be +0 // Object.is equality
Tests  18 failed | 169 passed (187)
```

The three `this` tests fail there because the previous commit calls a contributed `root` handler without `this`. The other failures come from its keeping only the containers of the latest subtree or selection export: twelve of the tests listed parse again what an export in between did not read, and three find a container that is no longer kept, a removed block's or one a document export has. The new tests not listed above pass there too; they pin what this commit drops and keeps.

### 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 and `pnpm run build` pass. Each of these fails one of the tests: not dropping a block's old containers, dropping one that another block read in the same export, keeping the entry of a block that reads none now, not checking for removed blocks at a document export or when the blocks more than double, dropping blocks that are still there, keeping a removed block's entry or one the exporting update detached, keeping what is read when an export that a rule or a handler runs is done, or only when nothing throws, not clearing what was read or written, not setting the block for a block's node, or only for the first of its nodes, setting it for a block this export does not write, not setting it back once the node is written, or only when nothing throws, not wrapping the root's handle or not keeping its handlers, not passing `this` on to the root handler or to the handlers below it, not keeping the root's containers or a block's, counting a block that wrote nothing as written, writing a subtree or a selection as a whole, letting a document export use or clear these containers, keeping what a document export has among them too, and not looking in the document export's containers.

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — this looks good. Reading each paragraph, heading or table cell back after it is written fixes the case raised in the review of #9956, and literals that already round-trip keep their bare form. The new tests fail against main's exporter (the 38 in your Before) and pass on this head, and a randomized round-trip check of imported literals with random neighbours (formatting, punctuation, escapes, CJK, emoji; in paragraphs, headings, table cells, list items and quotes) found no literal that reads back as a different link on this head, against 940 of 3,000 cases on main, with no case that worked on main breaking here.

A few optional, non-blocking suggestions:

  1. Only the literal that breaks needs to change (packages/lexical-mdast/src/MdastExport.ts, lines 276–282). When one literal in a container fails the read-back, every literal in it is demoted. Importing see www.a.example, www.b.example and www.c.example and typing y right after the third exports see [www.a.example](http://www.a.example), [www.b.example](http://www.b.example) and [www.c.example](http://www.c.example)y, so a one-character edit rewrites every URL in a long paragraph. Demoting from the first link whose key differs and reading the container back once more would keep the earlier ones bare. If the whole-container rewrite is the trade-off you want, the changeset's Verify: sentence could say that literals keep their bare form when nothing else in their paragraph, heading or cell breaks.
  2. First-export cost. With the same harness on both branches, a 2,000-paragraph document whose literals sit in parentheses took about twice as long on its first export (each such container is parsed once more) and about 11% longer afterwards; documents without literals were unchanged. The Cost section covers the isolated-literal and single-paragraph cases; a line on this one would help maintainers weigh it.
  3. Docs. packages/lexical-website/docs/serialization/markdown-mdast.md (around line 122) lists the bare GFM literal among the preserved link styles; the "Some forms are normalized" sentence there could add that a literal whose neighbours would change how it reads back is written as [text](url) or <url>.
  4. Handler authors. Contributed toMarkdownExtensions inline handlers can now run a second time in a container that is rewritten, and a contributed root handler runs inside the package's own. Your description explains why that is safe; a sentence in the changeset (format in .changeset/README.md) would reach people who write handlers.

Automated review (maintainer link)

Comment on lines +279 to +281
for (const link of links) {
demoted.add(link);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, non-blocking: this demotes every literal in the container, including ones that read back fine. Importing see www.a.example, www.b.example and www.c.example and typing y after the third exports all three in resource form, so a one-character edit rewrites every URL in a long paragraph. Demoting from the first i where keys[i] !== linkKey(link) onward and reading the container back once more would keep the earlier literals bare, at the cost of one extra parse only in containers that already broke.

@potatowagon
potatowagon added this pull request to the merge queue Nov 22, 2026
Merged via the queue into meta:main with commit 249d7eb Nov 22, 2026
61 checks passed
@mayrang

mayrang commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

This got merged before I pushed the follow-ups, so they're in #9970: only the literals from the first one that breaks get rewritten now, and the docs and changeset have the lines you suggested.

On the first-export cost, I got about the same. 2,000 paragraphs with a literal in parentheses take ~183 ms on the first export against ~95 ms before this PR, then 29 vs 26 ms. Documents without literals don't change, and #9970 doesn't move these numbers.

This branch was successfully deployed

2 active deployments
Preview – lexical — 5b3ca341 Deployed Nov 22, 2026 by vercel[bot]
Preview – lexical-playground — 5b3ca341 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants