Repository navigation
[lexical-markdown] Bug Fix: import a link whose URL contains parentheses - #9093
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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 |
|
Agreed on the round trip. The PR changed only the two import patterns, so Measured on the branch as it was, taking a LinkNode with the URL on the left, exporting it and importing the result back:
So the import fix on its own only closed the balanced case. Export now escapes On mdast, 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
|
|
Go ahead and add it |
|
Added. Both regexes now take a destination between
The raw form escapes Two shapes read exactly as they did before, both rejected by CommonMark and both long accepted here: With this PR's tests applied to |
Found some regressions and other divergences that can be resolved before merge
etrepum
left a comment
There was a problem hiding this comment.
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.
|
Confirmed, all five rows. I ran them through Fixed with your two disjoint branches, The five rows are tests now. They fail on the branch as you reviewed it and pass on There is one more difference from For the line ending I asked
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: 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
|
|
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.
|
The premise checks out. On 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 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 #8593 is what these patterns were written for, so I measured. The shortcut 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
|
…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&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 b" 3ms × preserves "https://example.com/a
b" 1ms × preserves "https://example.com/a&b" 1ms × preserves "&meta#38;" 1ms × preserves "a&meta#10;b" 1ms × preserves "a&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 b" 1ms ✓ link destination round trip > preserves "https://example.com/a
b" 1ms ✓ link destination round trip > preserves "https://example.com/a&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&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]>
…ses (meta#9093) Co-authored-by: Bob Ippolito <[email protected]> Co-authored-by: Claude <[email protected]>
…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.
… 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]>
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.
The CommonMark-escaped spelling fails the same way:
'[Ruby](https://en.wikipedia.org/wiki/Ruby_\\(programming_language\\))'Import
Both
LINKregexes 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. Thereplacehandler already runsunescapeText(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.exportwrote[${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:https://en.wikipedia.org/wiki/Ruby_(programming_language)[x](https://en.wikipedia.org/wiki/Ruby_(programming_language))https://example.com/a)b[x](https://example.com/a)b)https://example.com/ahttps://example.com/a(b[x](https://example.com/a(b)https://example.com/((a))[x](https://example.com/((a)))https://example.com/a\[x](https://example.com/a\)Export now escapes
\,(,)and<in a raw destination, which is the spellingimportRegExpaccepts andreplaceundoes, and every row above comes back as the URL it started from.mdast-util-to-markdownescapes the same parentheses, so@lexical/mdastwrites 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,replaceuses 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.https://example.com/a b[x](<https://example.com/a b>)https://example.com/a b(c)[x](<https://example.com/a b(c)>)https://example.com/a b>c[x](<https://example.com/a b\>c>)<foo>[x](\<foo>)[x](<>)Escaping
<in the raw form is what keeps the two shapes apart, and the<foo>row is where it shows:mdast-util-to-markdownwrites that URL as[x](<foo>)and reads it back asfoo.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 whatmdast-util-from-markdownreturns. 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:
[a](/uri 'title')/urititle[a](/uri (title))/urititle[a](/uri "title")/urititle[a]( /uri )/uri[a]()[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.
maindoes the same, so this one was never a regression of this PR.https://example.com/athen a line feed thenb[x](<https://example.com/a b>)[x](<https://example.com/a b>)unescapeTextalready decodes a decimal character reference, so the importer needed nothing, and a CommonMark reader gives the same URL back.mdast-util-to-markdownwrites the same destination in hexadecimal,
.Test plan
Before
The reference parser is
mdast-util-from-markdown, which already sits inpackages/lexical-mdast/node_modules. I took 279 spellings of an inline link through$convertFromMarkdownStringand compared the link it produces with the one that parser returns.mainThe 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-markdownfails 47.After
vitest --project unitover the whole repository: 260 files, 4333 passing and 1 skipped, 0 failing.packages/lexical-markdownalone is 498 passing.Markdown.specis 54 passing on chromium locally.tsc,eslintandprettier --checkare 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 destinationb\and now matches withb\)c, unescaped tob)cby the handler. Two shapes in the same family stop matching:[a](a\)and[a](\)used to give the destinationsa\and\, and now give no link, because\)is an escape and the destination never closes.mdast-util-from-markdownrejects 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.