Skip to content

[lexical-markdown] Bug Fix: import a link whose URL contains parentheses - #9093

Merged
etrepum merged 10 commits into
meta:mainfrom
luantaraschi:fix/markdown-link-url-parentheses
Aug 25, 2026
Merged

etrepum merged 10 commits into
meta:mainfrom
luantaraschi:fix/markdown-link-url-parentheses

Conversation

@luantaraschi

@luantaraschi luantaraschi commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

A markdown link whose URL contains parentheses is not imported at all, and since export writes the URL raw, a link already in the editor is destroyed by a round trip.

$convertFromMarkdownString(
  '[Ruby](https://en.wikipedia.org/wiki/Ruby_(programming_language))',
  TRANSFORMERS,
);
// no LinkNode: the whole line stays as paragraph text

The CommonMark-escaped spelling fails the same way:

'[Ruby](https://en.wikipedia.org/wiki/Ruby_\\(programming_language\\))'

Import

Both LINK regexes describe the destination as ([^()\s]+), which rules out ( and ) outright and has no alternative for a backslash escape. CommonMark 6.3 allows both: a destination may hold balanced parentheses, or parentheses that are backslash-escaped. The replace handler already runs unescapeText(rawLinkUrl), so the transformer means to accept an escaped destination, but the pattern can never produce one.

The raw destination now reads as four alternatives: a backslash with whatever follows it, a backslash that only reaches whitespace and so escapes nothing, a balanced pair of parentheses, or any other character that is not a space, a parenthesis or a backslash. It may not begin with <, since that is what makes a destination pointy. No two of the alternatives can match in the same place, so the alternation has nothing to backtrack over, which matters here because #8593 hardened these same patterns.

Export

Reading those spellings is only half of the round trip. LINK.export wrote [${textContent}](${node.getURL()}), so a LinkNode holding a Wikipedia URL of that shape exported to exactly the first string above, and destinations that no reader can parse back were still what the editor produced. Measured with only the import fix in place, taking a LinkNode with the URL on the left, exporting it and importing the result:

URL export writes import reads back
https://en.wikipedia.org/wiki/Ruby_(programming_language) [x](https://en.wikipedia.org/wiki/Ruby_(programming_language)) same URL
https://example.com/a)b [x](https://example.com/a)b) https://example.com/a
https://example.com/a(b [x](https://example.com/a(b) no link
https://example.com/((a)) [x](https://example.com/((a))) no link
https://example.com/a\ [x](https://example.com/a\) no link

Export now escapes \, (, ) and < in a raw destination, which is the spelling importRegExp accepts and replace undoes, and every row above comes back as the URL it started from. mdast-util-to-markdown escapes the same parentheses, so @lexical/mdast writes that Wikipedia link the way this package now writes it.

The pointy form

A URL that holds whitespace has no raw spelling at all, since CommonMark 6.3 keeps spaces out of a raw destination and a backslash cannot escape one. The only way to write it is between < and >, which this parser never read. Both regexes now take that shape as the first of two alternatives, replace uses whichever of the two matched, and export writes it when the URL holds whitespace or is empty. Inside it only <, > and a backslash need an escape.

URL export writes import reads back
https://example.com/a b [x](<https://example.com/a b>) same URL
https://example.com/a b(c) [x](<https://example.com/a b(c)>) same URL
https://example.com/a b>c [x](<https://example.com/a b\>c>) same URL
a tab in place of that space the same, with the tab same URL
<foo> [x](\<foo>) same URL
the empty URL [x](<>) same URL

Escaping < in the raw form is what keeps the two shapes apart, and the <foo> row is where it shows: mdast-util-to-markdown writes that URL as [x](<foo>) and reads it back as foo.

A raw destination may no longer begin with <, so a pointy destination that never closes is not a link at all. [a](<foo), [a](<a<b>), [a](<>x) and [a](<a>b) used to resolve with the brackets sitting inside the URL, and now resolve to nothing, which is what the spec says and what mdast-util-from-markdown returns. That is a change in behavior for markdown already written, and it is deliberate: reading <...> at all is the same change, since before this the brackets were part of the URL wherever they fell.

Anywhere but in first place an angle bracket is an ordinary character, so [a](a<b>c) reads as it always did, and export stops escaping the ones that never needed it.

The shapes that never matched

Four more spellings of an inline link, none of which this parser has ever read:

markdown url title
[a](/uri 'title') /uri title
[a](/uri (title)) /uri title
[a](/uri "title") /uri title
[a]( /uri ) /uri
[a]() empty
[a](foo(and(bar))) foo(and(bar))

Title, whitespace and empty destination come straight from the grammar. The parentheses are the one place a regular expression cannot follow the spec, since it cannot count: they nest three deep here, the depth is written in the comment above the pattern, and past it the string is not a link. Before this every depth above one was already not a link.

A line ending in the URL

Neither shape may hold one. A raw destination ends at whitespace, and the pointy form is defined without a line ending. Export wrote it literally, which leaves a destination no reader can close and splits the paragraph in two. main does the same, so this one was never a regression of this PR.

URL export writes import reads back
https://example.com/a then a line feed then b [x](<https://example.com/a&#10;b>) same URL
the same with a carriage return in front of the line feed [x](<https://example.com/a&#13;&#10;b>) same URL

unescapeText already decodes a decimal character reference, so the importer needed nothing, and a CommonMark reader gives the same URL back. mdast-util-to-markdown writes the same destination in hexadecimal, &#xA;.

Test plan

Before

The reference parser is mdast-util-from-markdown, which already sits in packages/lexical-mdast/node_modules. I took 279 spellings of an inline link through $convertFromMarkdownString and compared the link it produces with the one that parser returns.

agrees with the reference parser
main 71 of 279
this branch 276 of 279

The three left over are parentheses nested past the depth the pattern is written for.

With the tests of this PR applied to main, packages/lexical-markdown fails 47.

After

vitest --project unit over the whole repository: 260 files, 4333 passing and 1 skipped, 0 failing. packages/lexical-markdown alone is 498 passing. Markdown.spec is 54 passing on chromium locally. tsc, eslint and prettier --check are clean.

I also ran the old and the new pattern side by side over the existing shapes to check nothing else moved. Identical results for [a](url), [a](url "title"), [a](b(c) (still no match, the parenthesis never closes), [a](b)c), [text](url) trailing, leading [a](b) and the nested [foo [bar](/uri)](/uri) case. The one other difference is [a](b\)c), which used to match with the destination b\ and now matches with b\)c, unescaped to b)c by the handler. Two shapes in the same family stop matching: [a](a\) and [a](\) used to give the destinations a\ and \, and now give no link, because \) is an escape and the destination never closes. mdast-util-from-markdown rejects both as well.

For backtracking, feeding the unanchored pattern a destination that never closes, 20000 characters of it: open parentheses 0.013 ms, backslash escapes 0.117 ms, open angle brackets 0.057 ms, escaped angle brackets 0.061 ms. All four grow linearly from 10 characters upward.

Both LINK regexes described the destination as [^()\s]+, so a URL holding a
balanced pair of parentheses, or parentheses that were backslash-escaped,
never matched and the line stayed as literal text. Export writes the URL
raw, so a LinkNode with such a URL was destroyed by a round trip.

CommonMark allows both spellings, and the replace handler already runs
unescapeText over the destination, so the intent was there. Read the
destination as an escape, a balanced pair, or any other character that is
not a space, a parenthesis or a backslash. The three start on different
characters, so the alternation has nothing to backtrack over.
@vercel

vercel Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 25, 2026 3:45pm
lexical-playground Ready Ready Preview Aug 25, 2026 3:45pm

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 Aug 23, 2026
@etrepum

etrepum commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

I think if we're going to fix this we should fix the round-trip. This only looks at export.

Note that you can already get more compliant markdown behavior with @lexical/mdast

@luantaraschi

Copy link
Copy Markdown
Contributor Author

Agreed on the round trip. The PR changed only the two import patterns, so LINK.export was still writing the URL raw and a link already in the editor could still be destroyed on the way out.

Measured on the branch as it was, taking a LinkNode with the URL on the left, exporting it and importing the result back:

URL export writes import reads back
https://en.wikipedia.org/wiki/Ruby_(programming_language) [x](https://en.wikipedia.org/wiki/Ruby_(programming_language)) same URL
https://example.com/a)b [x](https://example.com/a)b) https://example.com/a
https://example.com/a(b [x](https://example.com/a(b) no link
https://example.com/((a)) [x](https://example.com/((a))) no link
https://example.com/a\ [x](https://example.com/a\) no link

So the import fix on its own only closed the balanced case.

Export now escapes \, ( and ) in the destination, which is the spelling importRegExp accepts and replace undoes. Every row above comes back as the URL it started from, and the new test block asserts both halves for eight destinations: the markdown that export writes, and the URL that importing that markdown gives back.

On mdast, mdast-util-to-markdown escapes every parenthesis in a raw destination too, so @lexical/mdast and this package now write that Wikipedia link the same way. They differ on a lone backslash: mdast leaves a\b as it is because \b cannot start an escape, this escapes it anyway so that a URL ending in a backslash cannot swallow the closing parenthesis.

One case is still lost and I left it alone: a URL with whitespace in it. A raw destination cannot hold whitespace at all, so that one needs the pointy form <...>, which this importer has never accepted, and it fails on main the same way. Happy to add it here if you would rather have it in one PR.

vitest --project unit over the repository is 256 files, 4257 passing, 0 failing, and tsc, eslint and prettier --check are clean.

@etrepum

etrepum commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Go ahead and add it

@luantaraschi

Copy link
Copy Markdown
Contributor Author

Added. Both regexes now take a destination between < and > as the first of two alternatives, replace uses whichever shape matched, and export writes the pointy form when the URL holds whitespace or is empty. Inside it only <, > and a backslash need an escape.

URL export writes import reads back
https://example.com/a b [x](<https://example.com/a b>) same URL
https://example.com/a b(c) [x](<https://example.com/a b(c)>) same URL
https://example.com/a b>c [x](<https://example.com/a b\>c>) same URL
a tab in place of that space the same, with the tab same URL
<foo> [x](\<foo>) same URL
the empty URL [x](<>) same URL

The raw form escapes < now, which is what keeps a raw destination from being read as a pointy one. That is the <foo> row: mdast-util-to-markdown writes that URL as [x](<foo>) and reads it back as foo.

Two shapes read exactly as they did before, both rejected by CommonMark and both long accepted here: [a](<foo) gives the destination <foo, and [a](<a>b<c>) gives <a>b<c>. I left that leniency alone rather than tighten it in a bug fix.

With this PR's tests applied to main, packages/lexical-markdown fails 28, and the description lists them. It is 466 passing now, the repository is 4276 with 0 failing, and Markdown.spec is 54 passing on chromium here. One of the new tests types [test](<https://example.com/a b>) a character at a time, so the shortcut path is covered as well. tsc, eslint and prettier --check are clean.

etrepum
etrepum previously approved these changes Aug 24, 2026
@etrepum
etrepum dismissed their stale review August 24, 2026 18:15

Found some regressions and other divergences that can be resolved before merge

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I had Claude do some additional review and it found some regressions which need to be addressed before merge. The divergences could also be fixed, since bringing the implementation closer to compliance is the purpose of this PR.

Regressions vs. main

The raw-destination alternative starts \\., and . matches a space. CommonMark treats a backslash before a non-punctuation character as a literal backslash, after which a space ends a raw destination. main gets all five of these right; the PR head does not. Verified end-to-end through $convertFromMarkdownString, not just the regex.

markdown CommonMark main PR head
[x](b\ "t") url b\, title t url b\, title t url b\ "t", title dropped
[x](foo\ bar) not a link not a link url foo\ bar
[x](foo\ bar "t") not a link not a link url foo\ bar, title t
[x](a\ b) not a link not a link url a\ b
[x](a\ b "t") not a link not a link url a\ b, title t

The first row is the one that matters: a title that used to parse is now swallowed into the href.

A greedy optional (\\[^\s]?) would reintroduce ambiguity at every backslash, so the alternation needs two disjoint branches instead:

\\[^\s]|\\(?=\s)

Substituting that for both occurrences of \\. in the raw alternative only (the pointy alternative should keep \\. — whitespace is legal inside <...>) fixes all five rows with no other behavior change, and stays linear: 0.7–4.5 ms on 128,000-character adversarial inputs.

Pre-existing divergences from CommonMark

None of these are caused by this PR — main and the PR head behave identically on all of them. Listing them so the CommonMark-compliance claim can be scoped in the description.

Nested parentheses (only one level is accepted)

markdown CommonMark Lexical (main and PR head)
[x](foo(and(bar))) url foo(and(bar)) not a link
[x](a(b(c)d)e) url a(b(c)d)e not a link
[x](((a))) url ((a)) not a link

Single-quoted / parenthesized titles

markdown CommonMark Lexical (main and PR head)
[x](/uri 'title') url /uri, title title not a link
[x](/uri (title)) url /uri, title title not a link

More than one space before the title

markdown CommonMark Lexical (main and PR head)
[x](/uri "t") url /uri, title t not a link

Whitespace inside the parentheses

markdown CommonMark Lexical (main and PR head)
[x]( /uri) url /uri not a link
[x](/uri ) url /uri not a link

Empty raw destination (only <> is accepted)

markdown CommonMark Lexical (main and PR head)
[x]() url is empty not a link

< and > in the raw form, so a malformed pointy destination still matches

markdown CommonMark Lexical (main and PR head)
[x](<a<b>) not a link url <a<b>
[x](<>x) not a link url <>x
[x](<a>b) not a link url <a>b
[x](<\>) not a link url <>

Round-trip: URLs containing a line ending

/\s/ routes a line ending into the pointy branch, but no CommonMark destination form permits one, so the output is unparseable by both Lexical and a reference parser, and it splits the enclosing paragraph.

URL exported Lexical re-import CommonMark re-import
a\nb [x](<a\nb>) not a link not a link
a\r\nb [x](<a\r\nb>) not a link not a link
a b\nc [x](<a b\nc>) not a link not a link

The \t case is already in the round-trip test list; \n is not. Percent-encoding the line ending, or at minimum not emitting a form that corrupts block structure, would beat the current silent breakage.

… before whitespace

A backslash escapes ASCII punctuation only, so one sitting in front of
whitespace is a literal backslash and the whitespace closes the raw
destination. The alternative read `\\.` and `.` matches a space, so
`[x](b\ "t")` took `b\ "t"` as the destination and dropped the title,
and four spellings that are not links at all resolved to one.

Two disjoint branches replace it, one for a backslash with a character
after it and one for a backslash that only reaches whitespace, so the
alternation still has nothing to backtrack over.
…aracter reference

A raw destination ends at whitespace and the pointy form is defined
without a line ending, so a URL holding one had no spelling at all.
Export wrote it literally, which left a destination no reader can close
and split the paragraph in two, on `main` as much as here.

It goes out as `&meta#13;` and `&meta#10;` instead, which `unescapeText` already
decodes, so the URL survives the round trip and a CommonMark reader gets
the same one back. `mdast-util-to-markdown` writes the same destination
in its hexadecimal spelling.
@luantaraschi

Copy link
Copy Markdown
Contributor Author

Confirmed, all five rows. I ran them through $convertFromMarkdownString on main and on the branch and it breaks where you said it does: \\. swallowed the space, so b\ grew into b\ "t" and took the title with it.

Fixed with your two disjoint branches, \\[^\s]|\\(?=\s), in the raw alternative, pointy left on \\.. Inside the balanced-paren group I substituted only \\[^\s], since the other branch can never reach a match there. The character after it is whitespace, nothing in that group accepts whitespace, and the group still owes its ). I fuzzed the two spellings over 400,000 random destinations drawn from ab()\ \t"<>/ and they agree on every one, so the commit carries the shorter one. Still linear, and the worst of seven adversarial 128,000-character destinations that never close is 0.82 ms.

The five rows are tests now. They fail on the branch as you reviewed it and pass on main.

There is one more difference from main in the same family, and it should have been in the description: [x](a\) and [x](\) used to give the destinations a\ and \, and now give no link, because \) is an escape and the destination never closes. mdast-util-from-markdown rejects both too.

For the line ending I asked mdast-util-to-markdown rather than percent-encoding. It writes the character reference and reads the URL back whole:

'a\nb'    ->  [x](<a&#xA;b>)       ->  'a\nb'
'a\r\nb'  ->  [x](<a&#xD;&#xA;b>)  ->  'a\r\nb'

unescapeText already decodes the decimal form, so export writes &#13; and &#10; and import needed nothing. Percent-encoding keeps the paragraph in one piece as well, but it hands back a URL that is not the one that went in. Three more round-trip rows cover it. Those three fail on main too, so it was breakage on both sides rather than a regression of this PR.

On the divergences, I measured every row on both and they agree on all of them, so none of them is this PR's doing. Five look cheap: '...' and (...) titles, more than one space before the title, whitespace just inside the parentheses, and the empty destination. Nested parentheses past one level is the awkward one, since a regex only takes it to a fixed depth and #8593 is the reason that pattern reads the way it does. The < and > row is a different kind of change. [a](<foo) and [a](<a>b<c>) resolve to links here today, and rejecting the malformed pointy form takes that away from documents people already have.

I can take the five additive ones here. Nested parentheses I would cap at a fixed depth with the cap written in a comment, since a regex cannot count them. The < and > row is the one I would rather have a yes on from you before touching, because it takes behavior away from documents that already parse. Tell me which of the three you want and I will push it on top of this, or open a second PR if you would rather keep this one on the round trip.

packages/lexical-markdown is 474 passing, the repository 4309 with 1 skipped, and tsc, eslint and prettier --check are clean.

@etrepum

etrepum commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Supporting pointy form at all is a change from the previous behavior, as the angle brackets would be considered part of the URL in the parser before this change regardless of their position. If we're going to fix it, we ought to fix it all the way, so yes you should handle that.

… with an angle bracket

A destination written between `<` and `>` is the only place an angle
bracket is structural. A malformed one still matched the raw form, so
`[a](<a<b>)`, `[a](<>x)`, `[a](<a>b)` and `[a](<foo)` came back as links
whose URL carried the brackets, where CommonMark has no link at all.

Anywhere but the first character an angle bracket is ordinary, so
`[a](a<b>c)` keeps working and export stops escaping the ones that never
needed it.
…ves an inline link

Four of them never matched here. A title may be written `'...'` or
`(...)` and not only `"..."`. Whitespace may sit on either side of the
destination, and more than one space may separate it from the title. The
destination may be missing altogether, as in `[a]()`. Parentheses inside
it may nest, where only a single pair matched before.

The nesting is written three deep. A regular expression cannot count, so
the limit is in the pattern and in the comment above it rather than left
for someone to discover.

Measured against `mdast-util-from-markdown` over 279 spellings of an
inline link: `main` agrees on 71, the branch before this commit on 138,
and this on 276. The three that are left are parentheses nested past the
written depth.
@luantaraschi

Copy link
Copy Markdown
Contributor Author

The premise checks out. On main, [a](</uri>) gives the URL </uri>, so reading <...> at all had already moved that ground, and keeping the malformed spelling alive was the inconsistent half.

Two commits, because one takes behavior away and the other only adds. The first is the angle bracket rule on its own: a raw destination may no longer begin with <, which drops [a](<foo), [a](<a<b>), [a](<>x) and [a](<a>b). Anywhere else the bracket is an ordinary character, so [a](a<b>c) is untouched and export stopped escaping the ones that never needed it. The second adds what never matched: '...' and (...) titles, whitespace on either side of the destination, more than one space before the title, [a]() with nothing between the parentheses, and parentheses that nest.

The nesting is the one place the pattern cannot follow the spec, since a regular expression cannot count. It goes three deep and the depth is written in the comment above it. Deeper is one number, and the paragraph after next is what it would cost.

I compared against mdast-util-from-markdown over 279 spellings of an inline link, through $convertFromMarkdownString rather than the regex on its own. main returns the same link as the reference parser on 71 of them, the branch before these two commits on 138, and it is 276 now. The three left over are parentheses nested past three.

#8593 is what these patterns were written for, so I measured. The shortcut regExp stays linear and cheap: 32,000 characters of [a]( repeated is 0.43 ms against 0.12 ms on main, and a real shortcut is still too fast to measure. importRegExp is where it costs, and it was already quadratic before this. [a]( repeated goes 4.85 ms at 4,000 characters, 77 ms at 16,000 and 2,083 ms at 64,000 on main, and that curve comes from \[(.+?)\] rescanning rather than from the destination. The extra alternatives multiply the constant by about five, so 26 ms, 439 ms and 10,347 ms. Same complexity class, and no document produces that input, but the factor is real, and the nesting depth is what I would trade if you want it smaller.

On input that reads like a document it goes the other way, because the old pattern fails the match and rescans. 50,000 characters of paragraphs holding [Ruby](https://en.wikipedia.org/wiki/Ruby_(programming_language)) is 31 ms on main and under 0.01 ms here.

packages/lexical-markdown is 498 passing, the repository 4333 with 1 skipped, Markdown.spec is 54 on chromium, and tsc, eslint and prettier --check are clean. The description has the shapes and the comparison.

claude and others added 2 commits August 24, 2026 20:52
…eep a character reference in a URL

## Description

Two things are left over from reading the rest of the shapes CommonMark
gives an inline link.

Whitespace may sit on either side of the destination, and the destination
is optional, so the pattern read `\(\s*(?:...)?\s*\)`. With no destination
between them the two runs neighbour each other, and a run of whitespace can
be split between two adjacent `\s*` in as many ways as it is long. Every one
of them is walked before a run that never reaches the closing parenthesis is
given up on, so importing `[x](` followed by n spaces cost O(n squared):
16,000 spaces took 208ms where main and the commit before this branch took
1ms, and 200,000 spaces took over half a minute. Moving the trailing `\s*`
inside the optional group leaves the two runs with the destination between
them, which reads the same 4,536 spellings of an inline link identically
and finishes in milliseconds.

A URL is written raw, `unescapeText` decodes a numeric character reference,
and a CommonMark reader decodes the named ones as well. A URL holding
`a&meta#10;b` therefore came back as `a\nb`, and one holding `a&amp;b` came back
as `a&b` from any other reader. That is the same decoding a line ending in a
URL relies on to survive, so an `&` that begins a character reference now
goes out as `&meta#38;`, which both of them read back as a single `&`. An `&`
that begins nothing is untouched, so a query string keeps its separators. A
title is read back through `unescapeText` too and had the same defect, so it
gets the same treatment.

## Test plan

Ten cases added to `packages/lexical-markdown`: four whose assertion is that
reading 200,000 characters of whitespace finishes inside the time limit, and
six round trips through export and import for a character reference in a URL
and in a title, alongside two `&` that begin nothing.

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
     × reads a run that never closes 31754ms
     × reads a run in front of a quote 31837ms
     × reads a run of tabs 35856ms
     × reads a run on either side of a destination 31495ms
     × preserves "https://example.com/a&#10;b" 3ms
     × preserves "https://example.com/a&#x0A;b" 1ms
     × preserves "https://example.com/a&amp;b" 1ms
     × preserves "&meta#38;" 1ms
     × preserves "a&meta#10;b" 1ms
     × preserves "a&amp;b" 1ms
⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯
      Tests  10 failed | 490 passed (500)
```

### After

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
 ✓ link destination whitespace does not backtrack > reads a run that never closes 6ms
 ✓ link destination whitespace does not backtrack > reads a run in front of a quote 6ms
 ✓ link destination whitespace does not backtrack > reads a run of tabs 57ms
 ✓ link destination whitespace does not backtrack > reads a run on either side of a destination 11ms
 ✓ link destination round trip > preserves "https://example.com/a&#10;b" 1ms
 ✓ link destination round trip > preserves "https://example.com/a&#x0A;b" 1ms
 ✓ link destination round trip > preserves "https://example.com/a&amp;b" 0ms
 ✓ link destination round trip > preserves "&meta#38;" 0ms
 ✓ link destination round trip > preserves "https://example.com/?a=1&b=2" 1ms
 ✓ link destination round trip > preserves "https://example.com/?a=1&b=2;c=3" 1ms
 ✓ link title round trip > preserves "a&meta#10;b" 1ms
 ✓ link title round trip > preserves "a&amp;b" 1ms
 ✓ link title round trip > preserves "a&b" 1ms
      Tests  500 passed (500)

$ npx vitest run packages/lexical-markdown packages/lexical-link
 Test Files  11 passed (11)
      Tests  694 passed (694)

$ npx tsc --noEmit -p .        # clean
$ npx eslint packages/lexical-markdown/src   # clean
$ npx prettier --check ...     # All matched files use Prettier code style!
$ npx flow check               # 7 warnings, the same 7 as the unmodified tree
```

Checked against `mdast-util-from-markdown` as a CommonMark reference: the
whitespace change reads 4,536 spellings of an inline link with no behavioral
difference at all, and export followed by import returns the URL unchanged
for 70 URLs, 17 of which hold a character reference. Backtracking stays
linear to 512,000 characters, 36ms at the worst generator.

E2E was not exercised; the change is covered by unit tests.

Co-Authored-By: Claude Opus 5 <[email protected]>
@etrepum
etrepum added this pull request to the merge queue Aug 25, 2026
Merged via the queue into meta:main with commit 8006126 Aug 25, 2026
44 checks passed
unix-max pushed a commit to unix-max/lexical that referenced this pull request Aug 28, 2026
@etrepum etrepum mentioned this pull request Aug 31, 2026
phrogwrld added a commit to govtech-bb/gov-bb that referenced this pull request Sep 18, 2026
…ing does (#2690)

Tailwind's preflight strips list markers, so the Lexical theme sets them again. Lexical 0.47 read link destinations without parentheses and nested lists only at four spaces; 0.50.0 parses both per CommonMark (meta/lexical#9093, #8283), so the editor takes the upgrade instead of its own transformers.
IsaiahSama added a commit to govtech-bb/gov-bb that referenced this pull request Oct 15, 2026
… editor like landing (#2690) (#3050)

* fix(content): name the HTML tag that keeps a page in Markdown mode (#2690)

A bare <a> reads as a contradiction on pages whose <a data-start-link> markers are supported; quoting the tag as written tells the author what to change.

* fix(form-builder): render lists and links in Visual mode the way landing does (#2690)

Tailwind's preflight strips list markers, so the Lexical theme sets them again. Lexical 0.47 read link destinations without parentheses and nested lists only at four spaces; 0.50.0 parses both per CommonMark (meta/lexical#9093, #8283), so the editor takes the upgrade instead of its own transformers.

* test(form-builder): assert parenthesised links parse to real links (#2690)

The round trip alone could pass with the link left as literal text, so the
spec also checks that the editor holds a LinkNode with the decoded URL. The
escaped URL is now a literal instead of a regex replace, which CodeQL
flagged as incomplete escaping on #2730.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): pin the source-only banner to the tag as written (#2690)

A class= anchor keeps a page in Markdown mode; the banner must quote that
tag so the author knows what to change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Aaron Harris <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch was successfully deployed

2 active deployments
Preview – lexical — b9a3b77f Deployed Aug 25, 2026 by vercel[bot]
Preview – lexical-playground — b9a3b77f Deployed Aug 25, 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