Skip to content

[lexical-mdast] Bug Fix: A name like __init__ keeps its underscores inside another construct - #10046

Open
vjeux wants to merge 1 commit into
meta:mainfrom
vjeux:mdast-names-inside-constructs
Open

vjeux wants to merge 1 commit into
meta:mainfrom
vjeux:mdast-names-inside-constructs

Conversation

@vjeux

@vjeux vjeux commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Description

Since #9921, __init__ typed on its own stays as typed. Inside another construct the name still lost its underscores when the outer construct's closing delimiter was typed: *the __init__*, ~~old __init__~~, **__init__** and [see __init__](https://example.com) each turned the name into a bold init.

When a construct converts, each name inside it (a strong from a __ run around one word of ASCII letters, digits and underscores) now becomes the text that was typed, as the review of #9921 suggested. #9921's own top-level check is refactored into the shared isTypedName helper that this walk reuses. __two words__ inside emphasis still converts to strong, and Markdown import is unchanged.

Changeset

.changeset/mdast-typed-names-inside-constructs.md (@lexical/mdast patch, [fix], with a Verify: paragraph).

Test plan

Before

MdastStream.ts from main (e8fbf41), with this branch's four new tests:

./node_modules/.bin/vitest --project unit --no-watch -t "a name inside" packages/lexical-mdast/src/__tests__/unit/MdastShortcuts.test.ts

All four fail, the name losing its underscores:

AssertionError: expected 'see init' to be 'see __init__'  (a name inside a link)
 Test Files  1 failed (1)
      Tests  4 failed | 98 skipped (102)

After

The same command on this branch:

 Test Files  1 passed (1)
      Tests  4 passed | 98 skipped (102)

The @lexical/mdast unit suite passes (Test Files 21 passed (21), Tests 1107 passed (1107)), and so does the full unit suite (pnpm run test-unit). pnpm run ci-check exits 0 (TypeScript, Flow Found 0 errors, Prettier, ESLint and the changeset check). Rebased onto main at e8fbf41 (which carries #9999).

Not run: the browser-mode tests (pnpm run test-browser) and the e2e suites. The change is in the shortcut scanner, which the unit tests above drive by typing into an editor.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Dec 4, 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 Dec 6, 2026 2:09am UTC
lexical-playground Ready Ready Preview Dec 6, 2026 2:09am 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 Dec 4, 2026
@rootnode-bot

rootnode-bot Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Dec 6, 2026, 2:25 AM UTC

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: c39bf24

@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 following up on #9921. Keeping __init__ literal inside *…*, ~~…~~, **…** and links is a nice improvement, and I reproduced locally that the four new tests fail on main and pass on this branch. There is one regression to fix before this lands.

___word___ no longer types as bold italic (packages/lexical-mdast/src/MdastStream.ts, lines 124-144, keepTypedNames)

CommonMark reads ___both___ as an _ emphasis around a __ strong (spec §6.2, rule 14). This package treats that as the underscore spelling of bold italic: see the "underscore bold italic" case in packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts. The inner strong passes isTypedName, so the new walk turns it back into text. Typing ___both___ now leaves an italic __both__ where main gives a bold italic both. ____init____ likewise becomes a bold __init__ instead of a bold init. Neither is a name inside another construct, and the description and changeset don't mention this change.

Skipping a __ run that touches _ on both sides fixes it and keeps all four new cases working:

if (
  isTypedName(child, value) &&
  child.position &&
  // `___word___` is one delimiter run (bold italic), not a name.
  !(
    value[(child.position.start.offset ?? 0) - 1] === '_' &&
    value[child.position.end.offset ?? 0] === '_'
  )
) {

I tried this guard locally: the @lexical/mdast unit suite still passes, ___both___ is bold italic again, ____init____ is bold init again, and _the __init___ still keeps its name. Adding ['___both___', 'both'] to the "still converts" it.each in MdastShortcuts.test.ts would pin it. The changeset's Verify: paragraph could also mention it next to __two words__.

Optional: pin the export side (packages/lexical-mdast/src/__tests__/unit/MdastShortcuts.test.ts, lines 400-425)

The #9921 cases just above also assert $convertToMarkdownString(). The new outputs export correctly today (*the \_\_init\_\_*, [see \_\_init\_\_](https://example.com)) and read back unchanged. The same assertion in the new cases would keep that round trip from regressing.

Automated review (maintainer link)

… inside another construct

## Description

Since meta#9921, `__init__` typed on its own stays as typed. Inside another construct the name still lost its underscores when the outer construct's closing delimiter was typed: `*the __init__*`, `~~old __init__~~`, `**__init__**` and `[see __init__](https://example.com)` each turned the name into a bold `init`.

When a construct converts, each name inside it (a `strong` from a `__` run around one word of ASCII letters, digits and underscores) now becomes the text that was typed, as the review of meta#9921 suggested. meta#9921's own top-level check is refactored into the shared `isTypedName` helper that this walk reuses. `__two words__` inside emphasis still converts to strong, and Markdown import is unchanged.

A `__` run flanked by `_` on both sides is left alone: `___word___` is `_`-emphasis around a `__`-strong (CommonMark §6.2), this package's underscore spelling of bold italic, not a name. Without the guard `___both___` would have typed as an italic `__both__` and `____init____` as a bold `__init__`; both still convert as before.

## Changeset

`.changeset/mdast-typed-names-inside-constructs.md` (`@lexical/mdast` patch, `[fix]`, with a `Verify:` paragraph).

## Test plan

### Before

`MdastStream.ts` from `main`, with this branch's new tests:

```
./node_modules/.bin/vitest --project unit --no-watch -t "a name inside|still converts" packages/lexical-mdast/src/__tests__/unit/MdastShortcuts.test.ts
```

The four "a name inside" cases fail (the name loses its underscores), and the two new "still converts" cases fail (the bold-italic runs lose theirs):

```
AssertionError: expected 'see init' to be 'see __init__'   (a name inside a link)
AssertionError: expected '__both__' to be 'both'           (___both___ still converts)
```

### After

The same command on this branch: the "a name inside" cases (text, format and `$convertToMarkdownString` round trip) and the "still converts" cases all pass.

```
 Test Files  1 passed (1)
      Tests  10 passed | 94 skipped (104)
```

The `@lexical/mdast` unit suite passes (`Test Files 21 passed (21)`, `Tests 1109 passed (1109)`), and so does the full unit suite (`pnpm run test-unit`). `pnpm run ci-check` exits 0 (TypeScript, Flow `Found 0 errors`, Prettier, ESLint and the changeset check). Rebased onto current `main` (which carries meta#9999).

Not run: the browser-mode tests and the e2e suites. The change is in the shortcut scanner, which the unit tests drive by typing into an editor.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

@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. The new guard in keepTypedNames (packages/lexical-mdast/src/MdastStream.ts:131-140) fixes the ___word___ case from the last round: ___both___ types as bold italic and ____init____ as bold, as on main, while a name inside *…*, ~~…~~, **…** or a link keeps its underscores and exports escaped. Reproduced locally: the four new "a name inside" tests fail against main's MdastStream.ts and pass on this branch, and the whole @lexical/mdast unit suite passes.

Two optional, non-blocking suggestions:

  • Re-paste the Test plan at this head. The pasted output (Tests 4 failed | 98 skipped (102), 1107 passed, "Rebased onto main at e8fbf41") comes from the previous revision; this branch now has 104 tests in MdastShortcuts.test.ts (1109 in the package suite) and sits on a newer main. AGENTS.md (Commits and Pull Requests) asks the Test plan to carry the real Before and After output of the command that was run.
  • Name ___word___ in the changeset's Verify: line. .changeset/mdast-typed-names-inside-constructs.md:7 says __two words__ inside emphasis still converts; adding that ___word___ still becomes bold italic would point upgraders at the one input the new guard exists for (.changeset/README.md, Behavior changes: the Verify: paragraph says what to test).

Automated review (maintainer link)

This branch was successfully deployed

2 active deployments
Preview – lexical — c39bf243 Deployed Dec 6, 2026 by vercel[bot]
Preview – lexical-playground — c39bf243 Deployed Dec 6, 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