Skip to content

[lexical-mdast] Bug Fix: Escape the dot of www. so the characters after it import as typed - #9956

Open
mayrang wants to merge 3 commits into
meta:mainfrom
mayrang:www-reference
Open

mayrang wants to merge 3 commits into
meta:mainfrom
mayrang:www-reference

Conversation

@mayrang

@mayrang mayrang commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

With MdastAutolinkLiteralExtension (part of MdastGfmExtension), export escapes the . of www. only before an ASCII letter or digit, _, - or . as typed. What the export writes after it can still be something else: a reference when the character is non-ASCII and beside formatting, an escape when it is punctuation, or the marker of a format that starts or ends there, or of a link. micromark starts a literal on www. before any of these and reads it as part of the link, so the characters change on import. www.世 before bold ! is written as www.世**!** and read back as a link to http://www.&#x4E16, then ; and !; before bold 。 the link takes the ** and the bold is lost; www._x is read back as a link to http://www.\_x; www. before bold x is written as www.**x** and read back as a link to http://www.**x, then **. GitHub renders www.世**!** the same way.

This adds an unsafe rule to the extension that escapes the . after www (any case) in text, whatever follows it. The rule has no after, because mdast-util-to-markdown skips a rule that has an after where a rule with no condition escapes the next character, as one does *. The import may still link the decoded text, but its characters are the ones typed. Plain www.example.com was written as www\.example.com already. The export also joins adjacent text siblings before serializing, so a www that an inline element with no export rule (a MarkNode) splits is escaped as one text, and so is a 1 before a mark of . x, which would otherwise start an ordered list; a text with anything but its value, such as data or a position an export rule set, is kept apart. The escape is also written where it is not needed, such as see www\. then, which imports as before.

Changeset

.changeset/mdast-www-dot-escape.md (@lexical/mdast patch).

Test plan

Before

TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts with the new tests and the source of main:

 FAIL  |unit| packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts > Mdast punctuation modes > keeps the characters after `www.` in [["www.世",0],["!",1]]
AssertionError: expected 'www.世**!**' to be 'www\.世**!**' // Object.is equality
...
      Tests  17 failed | 106 passed (123)

Importing that export on main (both punctuation modes) gives LINK(http://www.&#x4E16)["www.&#x4E16",0] [";",0] ["!",1].

After

$ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
      Tests  242 passed (242)
$ TZ=UTC pnpm run test-unit
 Test Files  1021 passed | 6 skipped (1027)
      Tests  40261 passed | 423 expected fail | 174 skipped (40858)
$ VITEST_BROWSER=chromium,firefox,webkit TZ=UTC pnpm vitest run --project browser dev-examples/mdast-editor/src/__tests__/browser
 Test Files  12 passed (12)
      Tests  90 passed (90)

These exited 0 with this output, apart from a Browserslist data-age warning from eslint and build:

$ tsc
$ tsc -p ./tsconfig.test.json
$ prettier --list-different .
$ pnpm exec eslint packages/lexical-mdast/src/MdastExtension.ts packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
$ node scripts/build.mjs
$ node ./scripts/changesets/check.mjs
Changeset check passed: 192 pending changeset(s) valid.

The browser tests above are the ones that use @lexical/mdast. No e2e test was run: the playground does not use @lexical/mdast.

…er it import as typed

## Description

With `MdastAutolinkLiteralExtension` (part of `MdastGfmExtension`), export escapes the `.` of `www.` only before an ASCII letter or digit, `_`, `-` or `.` as typed. What the export writes after it can still be something else: a reference when the character is non-ASCII and beside formatting, an escape when it is punctuation, or the marker of a format that starts or ends there, or of a link. micromark starts a literal on `www.` before any of these and reads it as part of the link, so the characters change on import. `www.世` before bold `!` is written as `www.世**!**` and read back as a link to `http://www.&#x4E16`, then `;` and `!`; before bold `。` the link takes the `**` and the bold is lost; `www._x` is read back as a link to `http://www.\_x`; `www.` before bold `x` is written as `www.**x**` and read back as a link to `http://www.**x`, then `**`. GitHub renders `www.世**!**` the same way.

This adds an unsafe rule to the extension that escapes the `.` after `www` (any case) in text, whatever follows it. The rule has no `after`, because mdast-util-to-markdown skips a rule that has an `after` where a rule with no condition escapes the next character, as one does `*`. The import may still link the decoded text, but its characters are the ones typed. Plain `www.example.com` was written as `www\.example.com` already. The escape is also written where it is not needed, such as `see www\. then`, which imports as before.

## Changeset

`.changeset/mdast-www-dot-escape.md` (`@lexical/mdast` patch).

## Test plan

### Before

`TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts` with the new tests and the source of `main`:

```
 FAIL  |unit| packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts > Mdast punctuation modes > keeps the characters after `www.` in [["www.世",0],["!",1]]
AssertionError: expected 'www.世**!**' to be 'www\.世**!**' // Object.is equality
...
      Tests  9 failed | 101 passed (110)
```

Importing that export on `main` (both punctuation modes) gives `LINK(http://www.&#x4E16)["www.&#x4E16",0] [";",0] ["!",1]`.

### After

```
$ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
      Tests  229 passed (229)
$ TZ=UTC pnpm run test-unit
 Test Files  1021 passed | 6 skipped (1027)
      Tests  40248 passed | 423 expected fail | 174 skipped (40845)
$ VITEST_BROWSER=chromium,firefox,webkit TZ=UTC pnpm vitest run --project browser dev-examples/mdast-editor/src/__tests__/browser
 Test Files  12 passed (12)
      Tests  90 passed (90)
```

These exited 0 with this output, apart from a Browserslist data-age warning from eslint and build:

```
$ tsc
$ tsc -p ./tsconfig.test.json
$ prettier --list-different .
$ pnpm exec eslint packages/lexical-mdast/src/MdastExtension.ts packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
$ node scripts/build.mjs
$ node ./scripts/changesets/check.mjs
Changeset check passed: 192 pending changeset(s) valid.
```

The browser tests above are the ones that use `@lexical/mdast`. No e2e test was run: the playground does not use `@lexical/mdast`.
@vercel

vercel Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Building Building Preview Nov 20, 2026 4:11pm UTC
lexical-playground Ready Ready Preview Nov 20, 2026 4:11pm UTC

Request Review

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

rootnode-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 20, 2026, 9:20 AM UTC

@pkg-pr-new

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

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 5bd6080

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good, and thanks for the thorough reproduction. Escaping the . of www. in phrasing text, outside links and labels, matches how micromark's autolink-literal grammar works: it starts a literal after www. whatever character follows, so no after condition could list every case. Link titles and fence info strings stay unescaped, and the new cases fail against main and pass here.

Two optional, non-blocking suggestions:

  • .changeset/mdast-www-dot-escape.md:5: most editors get this extension through MdastGfmExtension, so naming that bundle in the summary would help changelog readers recognise the change. The worked example could also move into the Verify: paragraph, so the summary stays one short line (see .changeset/README.md).
  • packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts, the table ending at line 979: the old export also loses www. before inline code (see www.`x` now imports as a link that swallows the code span). A row like [['see www.', 0], ['x', code]] would pin that marker too.

Automated review (maintainer link)

Comment thread .changeset/mdast-www-dot-escape.md Resolved
…angeset summary

## Description

Follow-up to the review: the export on `main` also loses `www.` before inline code (``see www.`x` now`` imports as a link to ``http://www.`x` `` and the code format is lost). This adds that case to the `www.` test table, and shortens the changeset summary to one line that names `MdastGfmExtension`, with the example moved into `Verify:`. No source change.

## Changeset

`.changeset/mdast-www-dot-escape.md` (summary and `Verify:` reworded).

## Test plan

### Before

`TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts` with the source of `main`:

```
AssertionError: expected 'see www.`x` now' to be 'see www\.`x` now' // Object.is equality
...
      Tests  10 failed | 101 passed (111)
```

### After

```
$ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
      Tests  230 passed (230)
$ TZ=UTC pnpm run test-unit
 Test Files  1021 passed | 6 skipped (1027)
      Tests  40249 passed | 423 expected fail | 174 skipped (40846)
$ VITEST_BROWSER=chromium,firefox,webkit TZ=UTC pnpm vitest run --project browser dev-examples/mdast-editor/src/__tests__/browser
 Test Files  12 passed (12)
      Tests  90 passed (90)
$ node ./scripts/changesets/check.mjs
Changeset check passed: 192 pending changeset(s) valid.
```

These exited 0 with this output, apart from a Browserslist data-age warning from eslint:

```
$ tsc -p ./tsconfig.test.json
$ prettier --list-different .
$ pnpm exec eslint packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts
```

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good. Escaping the . of www. in phrasing text, outside links, images and labels, matches how micromark's GFM autolink-literal grammar behaves: it starts a literal at www. and reads through whatever the export writes next, so no after condition could list every case. Link titles and fence info strings are untouched, and the new cases fail against main and pass here. Thanks for adding the inline-code row and moving the worked example into Verify:.

One optional, non-blocking note:

  • packages/lexical-mdast/src/MdastExtension.ts:515: before: '[Ww]{3}' needs all three ws in the string safe() checks, but mdast-util-to-markdown gives a text node only the last character of its previous sibling as before. A www. split across sibling text nodes therefore still goes out unescaped. That happens when an inline element with no export rule of its own, such as a MarkNode, starts or ends inside www. For example, see ww + mark(w.世) + bold ! exports as see www.世**!** and reads back as a link to http://www.&#x4E16, then ;. main does the same, so this is not a regression. A follow-up could join adjacent text siblings before serializing, or the comment above the rule could note the limit.

Automated review (maintainer link)

{
unsafe: [
{
before: '[Ww]{3}',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional, non-blocking: before: '[Ww]{3}' needs all three ws in the string safe() checks, but mdast-util-to-markdown gives a text node only the last character of its previous sibling as before. A www. split across sibling text nodes, such as one where an inline element with no export rule (a MarkNode) starts inside www, still goes out unescaped: see ww + mark(w.世) + bold ! exports as see www.世**!** and reads back as a link to http://www.&#x4E16, then ;. main does the same, so this is not a regression; joining adjacent text siblings before serializing, or a line in this comment about the limit, would cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Joined adjacent text siblings before serializing in f4e59ed, so the rule sees the whole www. That also stops 1 + mark(. x) from starting an ordered list.

…ent splits

## Description

Follow-up to the review: an inline element with no export rule of its own, such as a `MarkNode`, exports its children in place, beside the text around it, as separate text nodes. `mdast-util-to-markdown` gives a text only the last character of the one before it to decide its escapes, so the `www` rule does not see a `www` split over them: `see ww` + mark(`w.世`) + bold `!` was still written as `see www.世**!**` and read back as a link to `http://www.&#x4E16`, then `;` and `!`. `main` does the same.

The export now joins each run of adjacent text siblings into one text before serializing, so escapes are decided on the whole text: the rule sees the whole `www`, and `1` before a mark of `. x`, which was written as `1. x` and read back as an ordered list, is written as `1\. x`. A text with anything but its value, such as `data` or a `position` that an export rule set for a handler to read, is kept apart.

## Changeset

`.changeset/mdast-www-dot-escape.md` (summary now names this case).

## Test plan

### Before

`TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts` with the new tests and the previous `MdastExport.ts`:

```
 FAIL  |unit| packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts > Mdast punctuation modes > keeps the characters after a `www.` a mark splits (strict-commonmark)
AssertionError: expected 'see www.世**!**' to be 'see www\.世**!**' // Object.is equality
 FAIL  |unit| packages/lexical-mdast/src/__tests__/unit/MdastPunctuation.test.ts > Mdast punctuation modes > escapes text a mark splits as one text (cjk-friendly)
AssertionError: expected '1. x' to be '1\. x' // Object.is equality
      Tests  7 failed | 116 passed (123)
```

### After

```
$ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast
      Tests  552 passed (552)
$ TZ=UTC pnpm run test-unit
 Test Files  1021 passed | 6 skipped (1027)
      Tests  40261 passed | 423 expected fail | 174 skipped (40858)
```

These exited 0 with this output, apart from a Browserslist data-age warning from eslint and build:

```
$ tsc
$ tsc -p ./tsconfig.test.json
$ node ./scripts/check-flow-types.mjs
Running Flow...
Found 0 errors
$ prettier --list-different .
$ pnpm exec eslint packages/lexical-mdast/src
$ node scripts/build.mjs
$ node ./scripts/changesets/check.mjs
Changeset check passed: 192 pending changeset(s) valid.
```

This branch was successfully deployed

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant