Skip to content

[lexical-markdown] Chore: Cover backslash emphasis imports and formatted text round trips - #9414

Merged
potatowagon merged 2 commits into
meta:mainfrom
ZedingZhang:fix/markdown-punctuation-backslash
Nov 22, 2026
Merged

potatowagon merged 2 commits into
meta:mainfrom
ZedingZhang:fix/markdown-punctuation-backslash

Conversation

@ZedingZhang

@ZedingZhang ZedingZhang commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

#9430 fixed backslash handling in emphasis flanking and added the matching exporter protection. Rebase this PR onto current main and retain the remaining regression coverage for #9407 in the existing punctuation test tables.

  • Assert the text and format of each imported text node for emphasis before a backslash and after an escaped backslash.
  • Cover intraword underscores, escaped emphasis markers, and Markdown exports that preserve emphasis and the hard line break.
  • Add document → Markdown → document cases for plain a followed by bold _x, italic *x, strikethrough ~x, and bold \x. Assert the exported numeric character reference and the restored text-node formats.

Changeset

This PR adds 13 regression cases to the existing unit tests. The strict changeset coverage check reports coverage: no published code changed, which qualifies for the test-only exemption in CONTRIBUTING.md.

Test plan

Before

Verified that the new tests detect the two regressions with temporary, separately applied mutations. Each mutation was restored before the final suite run.

Excluding the backslash from punctuation classification and running the three new emphasis import cases produced the following output.

Test Files  1 failed (1)
     Tests  3 failed | 805 skipped (808)

Bypassing numeric character reference generation in joinPieces and running the four new document round-trip cases produced the following output.

Test Files  1 failed (1)
     Tests  4 failed | 804 skipped (808)
AssertionError: expected 'a**\_x**' to be 'a**\_x**' // Object.is equality

Both checks used pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t <selected case names> --reporter=verbose.

After

pnpm exec vitest run --project unit packages/lexical-markdown packages/lexical-mdast
Test Files  18 passed (18)
     Tests  1461 passed (1461)

pnpm run tsc-test
$ tsc -p ./tsconfig.test.json

pnpm exec eslint packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts

pnpm exec prettier --check packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
Checking formatting...
All matched files use Prettier code style!

pnpm run check-changesets -- --base upstream/main --coverage error
Changeset check passed: 100 pending changeset(s) valid; coverage: no published code changed.

Local validation covered the unit suites and static checks on macOS with Node 26.0.0 and pnpm 11.24.0. Browser and E2E coverage is delegated to the PR's CI matrix.

@vercel

vercel Bot commented Oct 21, 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 Nov 7, 2026 7:23am UTC
lexical-playground Ready Ready Preview Nov 7, 2026 7:23am UTC

Request Review

@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.

Thanks for tracking this down. The diagnosis is right: in the old explicit class, \] escapes the bracket, so the backslash was never part of PUNCTUATION, even though it is an ASCII punctuation character in CommonMark 0.31.2. I'm requesting changes for two reasons.

1. The fix has since landed on main. #9430 (merged on November 3) replaced the ASCII list in packages/lexical-markdown/src/utils.ts with a Unicode punctuation class that includes the backslash. Its changeset says: "On import, the backslash, which was missing from its ASCII list, counts too." #9407 was closed when it merged. This branch now conflicts with main in utils.ts, and after a rebase that hunk has nothing left to change.

2. On its own, the import change breaks Lexical's round trip. The exporter (exportTextFormat in packages/lexical-markdown/src/MarkdownExport.ts) writes the opening marker right after the previous character. Inside formatted text it escapes *, _, `, ~ and, by default, \. With this change, a document of plain a followed by bold _x exports as a**\_x** and imports back as the literal text a**_x**. The same happens for italic *x (a*\*x*), strikethrough ~x (a~~\~x~~) and bold \x (a**\\x**), and the next export escapes the markers, so the formatting is gone for good. CommonMark agrees that a**\_x** is not strong, so the import itself is correct, but the exporter has to change along with it. #9430 does that by writing the character next to such a marker as a numeric character reference.

Suggested path: rebase onto main and drop the utils.ts change. These four inputs are not tested on main yet, so they can stay as regression tests if they pass against main's flanking. A test-only PR then needs the skip-changeset label rather than a changeset (see "Changesets" in CONTRIBUTING.md on main). If you keep the tests, two optional improvements:

  • importText compares getTextContent(). That shows the markers were consumed, but not that the text became italic or bold. Asserting the format of each text node would make the import cases stand on their own.
  • A document → Markdown → document case, such as plain a followed by bold _x, would cover the export side too.

If maintainers decide #9430's coverage is enough, closing this as superseded works as well.

Comment thread packages/lexical-markdown/src/utils.ts Resolved
…ted text round trips

## Description

meta#9430 fixed backslash handling in emphasis flanking and added the matching exporter protection. Rebase this PR onto current `main` and retain the remaining regression coverage for meta#9407 in the existing punctuation test tables.

- Assert the text and format of each imported text node for emphasis before a backslash and after an escaped backslash.
- Cover intraword underscores, escaped emphasis markers, and Markdown exports that preserve emphasis and the hard line break.
- Add document → Markdown → document cases for plain `a` followed by bold `_x`, italic `*x`, strikethrough `~x`, and bold `\x`. Assert the exported numeric character reference and the restored text-node formats.

## Changeset

This PR adds 13 regression cases to the existing unit tests. The strict changeset coverage check reports `coverage: no published code changed`, which qualifies for the test-only exemption in CONTRIBUTING.md.

## Test plan

### Before

Verified that the new tests detect the two regressions with temporary, separately applied mutations. Each mutation was restored before the final suite run.

Excluding the backslash from punctuation classification and running the three new emphasis import cases produced the following output.

```text
Test Files  1 failed (1)
     Tests  3 failed | 805 skipped (808)
```

Bypassing numeric character reference generation in `joinPieces` and running the four new document round-trip cases produced the following output.

```text
Test Files  1 failed (1)
     Tests  4 failed | 804 skipped (808)
AssertionError: expected 'a**\_x**' to be '&meta#97;**\_x**' // Object.is equality
```

Both checks used `pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t <selected case names> --reporter=verbose`.

### After

```text
pnpm exec vitest run --project unit packages/lexical-markdown packages/lexical-mdast
Test Files  18 passed (18)
     Tests  1461 passed (1461)

pnpm run tsc-test
$ tsc -p ./tsconfig.test.json

pnpm exec eslint packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts

pnpm exec prettier --check packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
Checking formatting...
All matched files use Prettier code style!

pnpm run check-changesets -- --base upstream/main --coverage error
Changeset check passed: 100 pending changeset(s) valid; coverage: no published code changed.
```

Local validation covered the unit suites and static checks on macOS with Node 26.0.0 and pnpm 11.24.0. Browser and E2E coverage is delegated to the PR's CI matrix.
@ZedingZhang
ZedingZhang force-pushed the fix/markdown-punctuation-backslash branch from 8b91bc6 to a1f8dab Compare November 5, 2026 19:50
@ZedingZhang ZedingZhang changed the title [lexical-markdown] Bug Fix: Treat the backslash as ASCII punctuation in emphasis flanking [lexical-markdown] Chore: Cover backslash emphasis imports and formatted text round trips Nov 5, 2026

@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 to me. Thanks for rebasing onto main and narrowing the PR to tests: the utils.ts change is gone, and the new document → Markdown → document rows cover the export side that an import-only change would have broken.

What the new rows pin down:

  • Emphasis next to a backslash imports as formatting (_a_\ + line break, **a.**\x, \\_a_), while intraword and escaped underscores stay plain. This follows CommonMark 0.31.2, where \ is an ASCII punctuation character, and matches what micromark and markdown-it produce for the same inputs.
  • Plain a followed by formatted text that starts with an escaped character exports with &#97; and re-imports with its bold, italic or strikethrough intact.

These are the only tests in the lexical-markdown and lexical-mdast unit suites that fail if \ is dropped from PUNCTUATION, so they close a real coverage gap. They also pass on current main.

Optional, non-blocking:

  • packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts:4841-4842: the two new EXPORT_CASES rows sit under a comment about bold links and run under the title "without escaped asterisks", and neither says what they guard. A one-line comment like the one on your import rows (// The backslash is ASCII punctuation too (#9407).) would keep the table self-explanatory.

No changeset is needed: the "Changesets" section of CONTRIBUTING.md exempts test-only changes, and a maintainer can add the skip-changeset label if CI asks for one. The CI workflows for this commit are still waiting for maintainer approval to run.

…n cases

## Description

The two backslash export regression cases appear below a comment about bold links. Add a comment identifying the backslash as ASCII punctuation and referencing meta#9407, matching the import cases and addressing the review feedback on meta#9414.

## Test plan

### Before

This is a comment-only clarification; there is no failing behavior to reproduce. Baseline formatting check:

```text
node_modules/.bin/prettier --check packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
Checking formatting...
All matched files use Prettier code style!
```

### After

```text
node_modules/.bin/vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
 Test Files  1 passed (1)
      Tests  808 passed (808)

node_modules/.bin/prettier --check packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
Checking formatting...
All matched files use Prettier code style!
```

ESLint and git diff --check exited successfully. Validation ran on macOS with Node 26.0.0 using the existing workspace dependencies. Browser and E2E tests were not run for this comment-only change.

@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 to me. Thanks for adding the #9407 comment above the two new EXPORT_CASES rows in packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts: the table now says what those rows guard, the same way the import rows do.

What the new rows pin down:

  • Emphasis next to a backslash imports as formatting (_a_\ + line break, **a.**\x, \\_a_), while intraword and escaped underscores stay plain (snake_case\_x, a_b_\c, \_a_, _a\_). This follows CommonMark 0.31.2, where \ is an ASCII punctuation character, and matches what micromark and markdown-it produce for the same inputs.
  • Plain a followed by formatted text that starts with an escaped character exports with &#97; and re-imports with its bold, italic or strikethrough intact, so the export side is covered as well as the import side.

These are the only tests in the lexical-markdown and lexical-mdast unit suites that fail if \ is removed from PUNCTUATION in packages/lexical-markdown/src/utils.ts, so they close a real coverage gap.

No changeset is needed: the "Changesets" section of CONTRIBUTING.md exempts test-only changes, and a maintainer can add the skip-changeset label if CI asks for one. The CI workflows for this commit are still waiting for maintainer approval to run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 48b1497

@potatowagon
potatowagon added this pull request to the merge queue Nov 22, 2026
Merged via the queue into meta:main with commit 18577e9 Nov 22, 2026
86 checks passed
@ZedingZhang
ZedingZhang deleted the fix/markdown-punctuation-backslash branch November 23, 2026 00:40

This branch was successfully deployed

2 active deployments
Preview – lexical — 48b1497e Deployed Nov 7, 2026 by vercel[bot]
Preview – lexical-playground — 48b1497e Deployed Nov 7, 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.

2 participants