Repository navigation
[lexical-mdast][lexical-website] Bug Fix: Read literal autolinks on import as GitHub comments do - #9990
Conversation
…mport as GitHub comments do ## Description The export writes a URL typed as text with an escape, so that GitHub shows it as text: `https://x.com` exports as `https\://x.com` and `www.x.com` as `www\.x.com`. The import links both again, because the transform of `mdast-util-gfm-autolink-literal` finds URL literals in the decoded text, where the escape is gone. GitHub renders comments (`POST /markdown` with `mode: gfm`) with URL literals found only in the Markdown as written and email literals also in the decoded text: `https\://x.com` shows as text there and `a\@b.co` as a link. The library follows the file mode instead, where `https\://x.com` links too, so with it a URL typed as text cannot read back as text. This updates `mdast-util-gfm-autolink-literal` from `^2.0.1` to `^2.1.2` and reads literal autolinks as GitHub comments do: a `www.` or `http(s)://` URL links only where micromark finds it in the source, and an email, `mailto:` or `xmpp:` literal links where the library's transform finds it. Before that transform runs, the import puts `q` in place of the first letter of each `www.` or `http(s)://` in the text, so that the transform starts no URL there but reads emails as before, and then puts the letters back. Typed `https://x.com` and `www.x.com` now read back as text. Markdown written elsewhere imports differently where GitHub comments read it differently from the transform: a `www.` URL right after `.`, `!`, `"` or a code span (`see .www.x.com`, `` `code`www.a.example ``) or one written with a character reference (`www.x.com`) now imports as text, as GitHub shows it, and an email inside an escaped URL (`https\://x.com/[email protected]`) now links on its own instead of the whole URL. A URL that GitHub links but micromark does not find in the source now imports as text too: `www._x.com. next`, where GitHub links `www._x.com` because cmark-gfm checks the `_` of the domain before trimming the `.`, and `www.café_bar.com`. Plain URLs and emails import as before. Everything in `@lexical/mdast` is experimental, and 2.1.2 already changes some imports on its own (CJK before an email, `mailto:`, a domain that starts with punctuation), but this is still a change for documents written elsewhere, so the changeset lists it. 2.1.2 ships the rule that escapes the `.` of `www.` before any character, which meta#9956 added here, so that rule is removed. 2.1.2 also escapes `:` after an uppercase `P` or `S`, so Markdown `FTP://a.co/ ©_a_` now exports as `FTP\://a.co/ ©_a_`, which cmark-gfm reads as the text and italic that Lexical has, where before it read one link. Two literals after a code span both export in resource form now (`` `x`[www.a.example](http://www.a.example) and [www.b.example](http://www.b.example) ``): the first no longer reads back as a link, and the export from meta#9970 writes every literal from the first one that would read back differently in that form. Most of the new tests pin an input against what cmark-gfm 0.29.0.gfm.14 links, since each was a case where a simpler version of this change read differently from GitHub. A few replace the library's transform with one that returns pieces that do not line up, to check that the import then keeps the text as it was rather than leave a `q` in it. ## Test plan Compared by the link texts against cmark-gfm 0.29.0.gfm.14 (`-e autolink -e tasklist`), which agrees with GitHub comments for these inputs, the import with this change and with 2.1.2 alone: - 3 × 30,000 random paragraphs of letters, CJK, punctuation, escapes, references, `www`, `https://`, `mailto:` and `@`: 29,732 / 29,713 / 29,711 agree with 2.1.2 alone, 29,914 / 29,904 / 29,901 with this change; 2 that agreed no longer do. - 6,000 strict and 5,000 CJK-friendly (with `MdastHtmlExtension`) paragraphs built around escaped URLs and emails in emphasis, links, task lists, headings, quotes and tables: 1,925 and 1,583 agree with 2.1.2 alone, 5,059 and 4,210 with this change; the 12 and 12 that only 2.1.2 got right are emails such as `a\@www.x.com\@b.co`, where the library's email pattern differs from cmark-gfm. - 3,900 typed texts exported and imported again: no URL reads back as a link (1,120 with 2.1.2 alone). ### Before ``` $ pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastAutolinkLiteral.test.ts # on main, without the tests of the internal transform × reads back the export of "https://x.com" as text × reads back the export of "www.x.com" as text × imports "https\\://x.com" × imports "www.x.com" Test Files 1 failed (1) Tests 48 failed | 20 passed (68) ``` ### After ``` $ pnpm vitest run --project unit packages/lexical-mdast Tests 1075 passed (1075) $ pnpm run test-unit Test Files 1039 passed | 6 skipped (1045) Tests 41031 passed | 423 expected fail | 174 skipped (41628) ```
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
…e by the ratio of two sizes
## Description
The test that a long text is masked in linear time imported 100,000 URL starts and asserted that it took less than 4,000 ms. It took 359 to 615 ms locally but 8,460 ms on CI, where most of the time is the parse. It now runs the transforms alone on a text of 20,000 and of 80,000 URL starts and asserts that the larger takes less than eight times as long, taking the fastest of five runs of each; a mask that searched the text again from its start for each URL takes about twenty times as long.
## Test plan
### Before
```
FAIL packages/lexical-mdast/src/__tests__/unit/MdastAutolinkLiteral.test.ts > MdastAutolinkLiteralExtension > masks the URLs of a long text in linear time
AssertionError: expected 8460.325905000034 to be less than 4000
```
### After
```
$ pnpm vitest run --project unit packages/lexical-mdast
Tests 1075 passed (1075)
```
With the mask replaced by a loop that searches again from the start after each URL:
```
AssertionError: expected 20.052862472209252 to be less than 8
```
There was a problem hiding this comment.
Thanks, this looks good. Finding URL literals in the Markdown as written, and emails in the decoded text, follows the GFM spec's own wording (§6.9). Extended autolinks "can only come at the beginning of a line, after whitespace, or any of the delimiting characters *, _, ~, and (", while an email "will be recognised when an email address is recognised within any text node". The two cases where the import now differs from GitHub (www._x.com., www.café_bar.com) are also what the spec asks for, since "no underscores may be present in the last two segments of the domain". The masking transform lines up exactly with the URL pattern of [email protected], and typed URLs now round-trip as text in both punctuation modes.
A few optional, non-blocking suggestions:
-
Verify:could mention two export changes (.changeset/mdast-gfm-autolink-literal-2-1-2.md, line 7).- 2.0.1 also escaped a
.after a singlewwhen a letter, digit,-,.or_followed. So text likenew.fileused to export asnew\.file, and with 2.1.2's[Ww]{3}rule it exports bare. - After a code span, later bare literals in the same paragraph now export as
[text](url)(MdastImportExport.test.ts, the "the next literal too" case).
Both read back unchanged. AGENTS.md asks a fix that changes observable output to say what to test in
Verify:, so one sentence would cover it. - 2.0.1 also escaped a
-
The mask copies the library's URL pattern (
packages/lexical-mdast/src/autolinkLiteral.ts, line 16). The lockfile pins 2.1.2, but^2.1.2lets installs downstream resolve a later 2.x. If that version's URL pattern grows, those URLs would quietly link from the decoded text again. The restore keeps the text intact, so nothing breaks. Pinning~2.1.2, or a test that the library transform finds no URL in masked text, would surface that on a bump. -
The cmark-gfm comparison in the Test plan is described rather than reproducible, and the expected values in
MdastAutolinkLiteral.test.tscome from those runs. AGENTS.md asks the Test plan for real pasted output. Committing the generator, or pasting a few of the inputs that still differ, would let others rerun it.
…est that the mask leaves no URL ## Description The changeset said the export rule for the `.` of `www.` now comes from the library "with the same output", but 2.0.1 also escaped a `.` after a single `w` before a letter, digit, `-`, `.` or `_`: text `new.file` exported as `new\.file` and now exports as `new.file`. It also did not say that after a code span a bare literal that follows the first one now exports as `[text](url)`. `Verify:` now says both, and the export round-trip table has a `new.file` row. The mask copies the start of the URL pattern of `mdast-util-gfm-autolink-literal` 2.1.2, which `^2.1.2` lets a later 2.x widen. A new test runs the transforms on a text with `http://`, `HTTPS://`, `Http://`, `www.`, `WWW.`, `Www.`, `ftp://`, `FTP://`, `file://` and an email and expects only the email to link. ## Test plan ### Before With the mask reduced to `www.` (`const URL_START = /www(?=\.)/gi;`): ``` $ pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastAutolinkLiteral.test.ts -t "masked text" Tests 1 failed | 77 skipped (78) ``` ### After ``` $ pnpm vitest run --project unit packages/lexical-mdast Tests 1077 passed (1077) $ pnpm run test-unit Test Files 1039 passed | 6 skipped (1045) Tests 41033 passed | 423 expected fail | 174 skipped (41630) ```
|
Pasted the cmark-gfm output for the pinned cases into the Test plan, the three that differ from GitHub included. The random comparisons came from scratch scripts I didn't add to the PR. |
There was a problem hiding this comment.
This looks good to me. Reading URL literals only from the Markdown as written, while emails still come from the decoded text, matches how the GFM spec words the two rules: §6.9 recognises URL autolinks by their position in the source and email autolinks "within any text node". It also fixes the round trip where text like https://x.com was exported as https\://x.com and came back as a link. The new tests cover both punctuation modes, the surrounding syntax (task items, emphasis, headings, tables, links, raw HTML) and the restore's failure paths.
Thanks for adding the new.file and post-code-span export changes to the changeset's Verify: paragraph. Thanks too for the test that runs the real library over a masked sample (MdastAutolinkLiteral.test.ts:352): if a later version of mdast-util-gfm-autolink-literal widens its URL pattern, CI will now catch it.
One optional, non-blocking suggestion:
packages/lexical-mdast/src/__tests__/unit/MdastAutolinkLiteral.test.ts:132: the comment says the expected values in theimports %jtable are what cmark-gfm reads, but the comparison that chose them ran outside the PR. A short note with the cmark-gfm version and flags (0.29.0.gfm.14,-e autolink), or a small fixture of the inputs you compared, would let the next change to this transform re-check them.
…the expected autolink literals
## Description
The comment above the `imports %j` table in `MdastAutolinkLiteral.test.ts` said its expected values are what cmark-gfm reads, without the version or how to check one. It now names cmark-gfm 0.29.0.gfm.14 and the command (`printf '%s\n' '<markdown>' | cmark-gfm -e autolink`). Comment only.
## Test plan
### After
```
$ pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastAutolinkLiteral.test.ts
Tests 78 passed (78)
```
There was a problem hiding this comment.
This looks good to me. URL literals are now read only from the Markdown as written, while emails still come from the decoded text. That matches how the GFM spec words the two rules: §6.9 places extended www. and http(s):// autolinks by their position in the source, and recognises email autolinks "within any text node". It also fixes the round trip where text like https://x.com was exported as https\://x.com and came back as a link. Thanks for naming the cmark-gfm version and command beside the pinned cases (MdastAutolinkLiteral.test.ts:130-132), and for the test that runs the real library over a masked sample, which will catch a future version that widens its URL pattern.
One optional, non-blocking suggestion:
.changeset/mdast-gfm-autolink-literal-2-1-2.md:7: AGENTS.md (Changesets) and.changeset/README.md("Behavior changes") ask a fix that changes output to say what to verify. TheVerify:paragraph mentions the uppercase-scheme escape only forFTP://understrict-commonmark. 2.1.2 escapes:after an uppercasePorSin both modes, so textHTTPS://x.comnow exports asHTTPS\://x.com(pinned atMdastAutolinkLiteral.test.ts:102); before, it was written bare and read back as a link. One sentence saying so would let people who compare exported Markdown expect the new output.
…w export with an escape ## Description 2.1.2 escapes `:` after an uppercase `P` or `S` in both punctuation modes, so text `HTTPS://x.com`, which exported bare and read back as a link, now exports as `HTTPS\://x.com` and reads back as text. The changeset's `Verify:` paragraph only mentioned this for `FTP://` under `strict-commonmark`; it now says it for `HTTPS://x.com`, which `MdastAutolinkLiteral.test.ts` pins. Changeset only.
There was a problem hiding this comment.
This looks good to me. Thanks for the thorough follow-ups.
URL literals (www., http(s)://) now come only from what micromark finds in the source, while emails still come from the decoded text. That matches how GFM §6.9 separates the two: a www autolink is recognised when the text www. "is found followed by a valid domain", an email autolink "when an email address is recognised within any text node". It also matches how GitHub renders comments.
With this change, a URL typed as text survives an export and import as text in both punctuation modes (MdastAutolinkLiteral.test.ts:109-128). When the pieces do not line up, the restore in packages/lexical-mdast/src/autolinkLiteral.ts falls back to the original children (:154-155), so the stand-in q cannot leak into a document.
The earlier suggestions are all addressed at this head:
- The changeset's
Verify:paragraph now coversnew.file, the resource form after a code span andHTTPS\://x.com. MdastAutolinkLiteral.test.ts:353-377will fail if a later 2.x release widens the library's URL pattern beyond what the mask covers.
I have no further suggestions.
Description
The export writes a URL typed as text with an escape, so that GitHub shows it as text:
https://x.comexports ashttps\://x.comandwww.x.comaswww\.x.com. The import links both again, because the transform ofmdast-util-gfm-autolink-literalfinds URL literals in the decoded text, where the escape is gone. GitHub renders comments (POST /markdownwithmode: gfm) with URL literals found only in the Markdown as written and email literals also in the decoded text:https\://x.comshows as text there anda\@b.coas a link. The library follows the file mode instead, wherehttps\://x.comlinks too, so with it a URL typed as text cannot read back as text.This updates
mdast-util-gfm-autolink-literalfrom^2.0.1to^2.1.2and reads literal autolinks as GitHub comments do: awww.orhttp(s)://URL links only where micromark finds it in the source, and an email,mailto:orxmpp:literal links where the library's transform finds it. Before that transform runs, the import putsqin place of the first letter of eachwww.orhttp(s)://in the text, so that the transform starts no URL there but reads emails as before, and then puts the letters back. Typedhttps://x.comandwww.x.comnow read back as text.Markdown written elsewhere imports differently where GitHub comments read it differently from the transform: a
www.URL right after.,!,"or a code span (see .www.x.com,`code`www.a.example) or one written with a character reference (www.x.com) now imports as text, as GitHub shows it, and an email inside an escaped URL (https\://x.com/[email protected]) now links on its own instead of the whole URL. A URL that GitHub links but micromark does not find in the source now imports as text too:www._x.com. next, where GitHub linkswww._x.combecause cmark-gfm checks the_of the domain before trimming the., andwww.café_bar.com. Plain URLs and emails import as before. Everything in@lexical/mdastis experimental, and 2.1.2 already changes some imports on its own (CJK before an email,mailto:, a domain that starts with punctuation), but this is still a change for documents written elsewhere, so the changeset lists it.2.1.2 ships the rule that escapes the
.ofwww.before any character, which #9956 added here, so that rule is removed. 2.1.2 also escapes:after an uppercasePorS, so MarkdownFTP://a.co/ ©_a_now exports asFTP\://a.co/ ©_a_, which cmark-gfm reads as the text and italic that Lexical has, where before it read one link. Two literals after a code span both export in resource form now (`x`[www.a.example](http://www.a.example) and [www.b.example](http://www.b.example)): the first no longer reads back as a link, and the export from #9970 writes every literal from the first one that would read back differently in that form.Most of the new tests pin an input against what cmark-gfm 0.29.0.gfm.14 links, since each was a case where a simpler version of this change read differently from GitHub. A few replace the library's transform with one that returns pieces that do not line up, to check that the import then keeps the text as it was rather than leave a
qin it.Test plan
Compared by the link texts against cmark-gfm 0.29.0.gfm.14 (
-e autolink -e tasklist), which agrees with GitHub comments for these inputs, the import with this change and with 2.1.2 alone:www,https://,mailto:and@: 29,732 / 29,713 / 29,711 agree with 2.1.2 alone, 29,914 / 29,904 / 29,901 with this change; 2 that agreed no longer do.MdastHtmlExtension) paragraphs built around escaped URLs and emails in emphasis, links, task lists, headings, quotes and tables: 1,925 and 1,583 agree with 2.1.2 alone, 5,059 and 4,210 with this change; the 12 and 12 that only 2.1.2 got right are emails such asa\@www.x.com\@b.co, where the library's email pattern differs from cmark-gfm.The random comparisons were run with scratch scripts that are not part of this PR. The cases below are pinned in
MdastAutolinkLiteral.test.ts, which imports them as text, as text, as[email protected], as text, as text and as[email protected]; the last three are where the import differs from GitHub, and the test comments say why:Before
After