Repository navigation
[lexical-markdown] Chore: Cover backslash emphasis imports and formatted text round trips - #9414
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
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:
importTextcomparesgetTextContent(). 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
afollowed by bold_x, would cover the export side too.
If maintainers decide #9430's coverage is enough, closing this as superseded works as well.
…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.
8b91bc6 to
a1f8dab
Compare
There was a problem hiding this comment.
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
afollowed by formatted text that starts with an escaped character exports withaand 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 newEXPORT_CASESrows 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.
There was a problem hiding this comment.
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
afollowed by formatted text that starts with an escaped character exports withaand 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.
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: |
Description
#9430 fixed backslash handling in emphasis flanking and added the matching exporter protection. Rebase this PR onto current
mainand retain the remaining regression coverage for #9407 in the existing punctuation test tables.afollowed 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.
Bypassing numeric character reference generation in
joinPiecesand running the four new document round-trip cases produced the following output.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
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.