Skip to content

[lexical-markdown] Bug Fix: Round-trip link titles and destinations through Markdown - #9464

Merged
etrepum merged 23 commits into
meta:mainfrom
clean99:contrib/markdown-link-title-line-endings
Oct 29, 2026
Merged

etrepum merged 23 commits into
meta:mainfrom
clean99:contrib/markdown-link-title-line-endings

Conversation

@clean99

@clean99 clean99 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

LINK export and import disagreed on several link titles and destinations, so the link (and sometimes the text after it) was lost on a round trip:

  • A line ending in a title was written raw, splitting the link across lines. Titles now write CR and LF as 
/
, as destinations already did.
  • A backtick in a title or destination could pair with one in the text after the link, and code spans are read first, so the link became code. Backticks in every link part are now backslash-escaped.
  • A URL with an ASCII control character was written raw, which CommonMark forbids; it now goes in the <...> form.
  • On import, a backslash before a title's closing quote or parenthesis was always taken as escaping it, so a title ending in an escaped backslash ("a\\") ran on into the following text. All three title spellings now consume \ plus one character as a unit, as CommonMark does, which also lets an escaped U+2028 through; a <...> destination now rejects a raw CR.

One escapeLinkPart helper now does the escaping for the destination and the title, so the two can't drift apart.

Changeset

.changeset/round-trip-link-titles-and-destinations.md: [fix] for @lexical/markdown, with Verify: covering the export changes (title line endings, backticks, control characters in URLs) and the import changes. A title whose closing quote is itself escaped, such as "C:\", no longer closes, so that link now imports as plain text, as in CommonMark.

Test plan

Before

pnpm run test-unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title round trip'

Tests 5 failed | 5 passed

After

pnpm run test-unit packages/lexical-markdown

Test Files 9 passed (9)
Tests 806 passed (806)

New cases: title line endings, backslashes and backticks; text and inline code right after a link; URLs with control characters, CR and NUL; single-quoted and parenthesized titles; and a check that LINK.regExp, used while typing, captures the same groups as LINK.importRegExp.

## Description

A link title containing a line ending is exported literally, splitting the
Markdown link across lines. Import then loses the link. Encode CR and LF as
character references, as link destinations already do, so the title and
link survive a round trip.

## Test plan

### Before

`pnpm run test-unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title round trip' --maxWorkers=1`

Tests  5 failed | 5 passed | 605 skipped (615)

### After

`pnpm run test-unit --maxWorkers=1`

Test Files  408 passed | 1 skipped (409)
Tests  10071 passed | 4 expected fail | 2 skipped (10077)

`pnpm run lint`, `pnpm run tsc`, `pnpm run tsc-test` and Prettier on the
changed files pass. Browser tests were not run; this changes only Markdown
serialization.
@vercel

vercel Bot commented Oct 25, 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 Oct 29, 2026 3:46pm UTC
lexical-playground Ready Ready Preview Oct 29, 2026 3:46pm UTC

Request Review

@meta-cla

meta-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

Hi @clean99!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 Oct 25, 2026
@meta-cla

meta-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@pkg-pr-new

pkg-pr-new Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: db34d06

…g, add changeset

## Description

On top of the line-ending fix for link titles, `LINK.export` escaped the destination and the title with the same three steps written out twice: character references, the shape's own syntax characters, then line endings as `&meta#13;`/`&meta#10;`. They now go through one `escapeLinkPart(value, syntax)` helper, so the destination and title can't drift apart again, and the comment that explained line endings for the destination alone now covers both. Output is unchanged from the previous commit.

Also adds the changeset for the title fix, with a `Verify:` paragraph since exported Markdown changes for titles that contain a line ending.

## Changeset

`.changeset/escape-line-endings-in-link-titles.md`: `[fix]` for `@lexical/markdown`, with `Verify:`.

## Test plan

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  785 passed (785)
$ pnpm run check-changesets
Changeset check passed: 6 pending changeset(s) valid.
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Part

## Description

`escapeLinkPart` replaced with `'\\$1'`, so each caller's regular expression had to wrap its character class in a capture group, and one that left the group out would have written a literal `\$1`. It now replaces with `'\\$&'` and the callers pass plain character classes; the helper's comment says the expression must be global. The note on which title characters need an escape moves from the end of the destination comment to the line that builds the title. Output is unchanged.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  785 passed (785)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  785 passed (785)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rip tests

## Description

The link title round-trip table covered `"` but never a backslash, so losing the backslash from the title's escape class would go unnoticed. It now has `a\b` and `a\"b`, and its comment explains why line endings, which most of its cases now cover, go out as character references.

## Test plan

### Before

With the backslash dropped from the title's escape class (`/["]/g`) the old table still passes:

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title round trip'
      Tests  10 passed | 605 skipped (615)
```

### After

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title round trip'
      Tests  12 passed | 605 skipped (617)
```

and with `/["]/g`: `Tests  2 failed | 10 passed | 605 skipped (617)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…itle's closing quote

## Description

The double- and single-quoted title alternatives in `LINK.importRegExp` and `LINK.regExp` treated any backslash in front of a quote as escaping it, even a backslash that was itself escaped. A title ending in a backslash exports as `"a\\"`, so on import the closing quote was taken as escaped and the title ran on into the text after the link: `[x](/uri "a\\") ")` read back the title `a") ` and dropped the trailing text. The alternatives now consume `\` plus one character as a unit, like the pointy destination does, which is also what CommonMark specifies.

Also folds the raw destination's leading-`<` escape into its `escapeLinkPart` pattern, corrects the helper's comment (a title may span lines in CommonMark; it is the line-by-line importer that a line ending breaks), and gives the round-trip tests `JSON.stringify`ed names so CR and LF don't garble reporter output.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title round trip'
AssertionError: expected 'a\") ' to be 'a\' // Object.is equality
      Tests  1 failed | 12 passed | 605 skipped (618)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  788 passed (788)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es in link titles

## Description

Adds the `[fix]` changeset for the link title import change in the previous commit, with `Verify:` since a title whose closing quote is escaped no longer closes.

## Test plan

### After

```
$ node scripts/changesets/check.mjs
Changeset check passed: 7 pending changeset(s) valid.
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…any escaped title character

## Description

Code spans are read before links, so a backtick in a link's title or destination could pair with a backtick in the text after the link and swallow the link: a title ``a` `` followed by `` `b`` imported as plain text with a code span. `LINK.export` now backslash-escapes backticks in the title and in both destination forms.

The title alternatives escaped with `\\.`, which does not match a line terminator such as U+2028, so a backslash before one stopped the link from matching at all. All three title spellings now use `\\[\s\S]`.

Tests: a `text after a link` table puts `") \`b` after links whose title ends in a backslash or holds a backtick, and whose destination holds one, replacing the single backslash test; a `link title import` table covers single-quoted and parenthesized titles and the U+2028 case. The changesets describe the backtick output change and that a title with an escaped closing quote now imports as plain text, as in CommonMark.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'text after a link|link title import'
     × keeps ["/uri","a`"] apart from it
     × keeps ["/a`b",null] apart from it
     × keeps ["/a `b",null] apart from it
     × reads "[x](/uri \"a\\ b\")"
      Tests  4 failed | 4 passed | 617 skipped (625)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  795 passed (795)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude added 12 commits October 29, 2026 03:53
…ty link destination

## Description

The pointy destination alternative escaped with `\\.`, which does not match U+2028 or U+2029, so `[x](</a\<U+2028>b>)` was not read as a link; it now uses `\\[^\n]`, like the title alternatives. The destination comment now says why every part escapes a backtick, and the changeset no longer claims a change for double-quoted titles, which already matched that case. Tests add the parenthesized and pointy spellings.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link title import'
     × reads "[x](</a\\ b> \"t\")"
      Tests  1 failed | 5 passed | 621 skipped (627)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  797 passed (797)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…destinations

## Description

The previous commit widened the pointy destination's escape to `\\[^\n]`, which let a backslash take a carriage return, and the unescaped class already let a raw CR through, although CommonMark (and the comment above the pattern) rule out any line ending there. Both classes now exclude `\r` as well. The import tests also check the URL, the changeset names every title spelling, and its Verify paragraph mentions the CR change.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link import'
     × reads no link when a pointy destination holds a carriage return
      Tests  1 failed | 6 passed | 621 skipped (628)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  798 passed (798)
$ node scripts/changesets/check.mjs
Changeset check passed: 7 pending changeset(s) valid.
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s, correct comments and changeset

## Description

Every new title test went through import, so the `$`-anchored `LINK.regExp` used while typing was untested. Two shortcut tests now type a title ending in an escaped backslash (a link with title `a\`) and one whose closing quote is escaped (no link). The comments in `LINK.export` no longer list the syntax characters as the only ones escaped, since the backtick is too, and the changeset's Verify paragraph says what changed for parenthesized titles (any escaped character, CR included, is now read) separately from `<...>` destinations (an escaped U+2028 is read, a CR is not).

## Test plan

### Before

With `MarkdownTransformers.ts` from the PR's base:

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/MarkdownTransformers.test.ts
     × a title whose closing quote is escaped makes no link
      Tests  1 failed | 14 passed (15)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  800 passed (800)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… link markup as references

## Description

The `escapeLinkPart` comment gave the importer's line splitting as the reason for escaping any line ending, but the importer splits only on LF. It now says CommonMark rules out a line ending in a destination and a blank line in a title, which two CRs make as well, and that the importer is what a single LF breaks. The `text after a link` test comment no longer claims to cover every construct, only the two it tests.

## Test plan

Comment-only change; no test run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… pointy form

## Description

A raw link destination cannot hold an ASCII control character in CommonMark, but only whitespace sent a URL to the `<...>` form, so `/a\u0001b` exported as `[x](/a\u0001b)`, which other readers don't see as a link. Control characters now choose the pointy form too. The backtick escape moves into `escapeLinkPart` so no caller can leave it out, and the three link round-trip tables share one `expectLinkRoundTrip` helper, which also checks the title and the paragraph text in each case.

## Test plan

### Before

The new destination cases `/a\u0001b` and `/a\u007fb` export raw and fail `toBe('[x](</a\u0001b>)')`.

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  802 passed (802)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tination out of the raw form

## Description

The test for the pointy destination form was an inline regular expression with a lint suppression in the middle of the ternary. It is now the module constant `NOT_RAW_DESTINATION`, whose comment also notes that a NUL still reads back as U+FFFD in CommonMark readers, which no spelling avoids. Behavior is unchanged.

## Test plan

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  802 passed (802)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d test CR and NUL URLs

## Description

The previous commit put `NOT_RAW_DESTINATION` between `escapeLinkPart` and its comment. It now sits above it with a comment of its own, which also says that import still reads control characters in a raw destination because older exports wrote them there. The destination table adds a lone CR and a NUL, and the changeset only claims the unescaped CR in a `<...>` destination as changed, since an escaped one was already rejected.

## Test plan

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  804 passed (804)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the export comment at NOT_RAW_DESTINATION

## Description

The backtick cases only followed the link with plain text, whose backticks export escaped. A new case puts inline code right after a link whose URL holds a backtick, where only the escape in the link keeps the two apart; `expectLinkRoundTrip` takes a flag to make the trailing text code. The comment in `LINK.export` now refers to `NOT_RAW_DESTINATION` instead of restating which characters it matches.

## Test plan

### Before

With the backtick escape removed from `escapeLinkPart`:

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'inline code after it'
AssertionError: expected '[x](/a`b)`c`' to be '[x](/a\`b)`c`' // Object.is equality
      Tests  1 failed | 632 skipped (633)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  805 passed (805)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…trol-character change in the changeset

## Description

`expectLinkRoundTrip` takes the trailing text's format instead of a boolean, the carriage-return import check is one named test per input, the `link import` comment lists everything its table covers, and the export changeset's summary names the move of URLs with control characters to the `<...>` form.

## Test plan

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  806 passed (806)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…importer needs rather than CommonMark

## Description

Two comments credited CommonMark with constraints that come from this package's importer: a backtick in a link's destination or title only threatens the link because the importer reads code spans first, and the pointy form is needed for any Unicode whitespace because the raw import branch excludes `\s`, while CommonMark forbids only ASCII spaces and control characters. Both comments now name the real constraint, so neither escape looks removable by appeal to the spec.

## Test plan

Comment-only change; no test run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… import pattern

## Description

The pointy-destination and single-quoted and parenthesized title changes in `LINK.regExp`, the pattern typing uses, were only tested through `importRegExp`. Each `link import` case now also checks that `LINK.regExp` captures the same groups as `LINK.importRegExp`, and the block uses the file's existing `importLinks` helper instead of building editors inline.

## Test plan

### Before

With `LINK.regExp`'s pointy branch put back to `\\.`, the new check fails:

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t 'link import'
      Tests  1 failed | 7 passed | 626 skipped (634)
```

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  806 passed (806)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…peLinkPart

## Description

`escapeCharacterReferences` had one caller left, `escapeLinkPart`, and its comment still described only the destination. Its replace is now the first step of `escapeLinkPart`, whose comment covers it for both destination and title. Behavior is unchanged.

## Test plan

### After

```
$ vitest run --project unit packages/lexical-markdown
 Test Files  9 passed (9)
      Tests  806 passed (806)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
## Description

PRs are squash-merged into one commit, so this one now carries a single changeset, `round-trip-link-titles-and-destinations.md`, covering both the export and import changes.

## Test plan

### After

```
$ node scripts/changesets/check.mjs
Changeset check passed: 6 pending changeset(s) valid.
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@etrepum etrepum changed the title [lexical-markdown] Bug Fix: Preserve line endings in link titles [lexical-markdown] Bug Fix: Round-trip link titles and destinations through Markdown Oct 29, 2026
etrepum
etrepum previously approved these changes Oct 29, 2026
@etrepum
etrepum dismissed their stale review October 29, 2026 06:16

Failing CI

claude and others added 2 commits October 29, 2026 06:25
…ut test

## Description

The shortcut test for a title ending in an escaped backslash passed a type argument to `getFirstChildOrThrow`, which takes none, so `tsc` (and `ci-check`) failed. It now reaches the link through its text node, as the other link tests do.

## Test plan

### Before

```
$ tsc
packages/lexical-markdown/src/__tests__/unit/MarkdownTransformers.test.ts(152,31): error TS2558: Expected 0 type arguments, but got 1.
packages/lexical-markdown/src/__tests__/unit/MarkdownTransformers.test.ts(153,10): error TS2339: Property 'getFirstChild' does not exist on type 'LexicalNode'.
```

### After

`tsc`, `tsc -p ./tsconfig.test.json`, `pnpm run flow`, `prettier --list-different .`, ESLint on `packages/lexical-markdown` and `check-changesets` report nothing, and the `packages/lexical-markdown` unit tests pass (806).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@etrepum
etrepum added this pull request to the merge queue Oct 29, 2026
Merged via the queue into meta:main with commit a9ad318 Oct 29, 2026
53 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — db34d068 Deployed Oct 29, 2026 by vercel[bot]
Preview – lexical-playground — db34d068 Deployed Oct 29, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants