Skip to content

[lexical-markdown] Bug Fix: Do not read a line with backticks after the fence as a code block - #9431

Open
x-labb wants to merge 3 commits into
meta:mainfrom
x-labb:fix-9404-backticks-after-fence
Open

x-labb wants to merge 3 commits into
meta:mainfrom
x-labb:fix-9404-backticks-after-fence

Conversation

@x-labb

@x-labb x-labb commented Oct 22, 2026 •

Copy link
Copy Markdown

Description

A line that starts with three or more backticks was always taken as an opening code fence, even when more backticks follow later on the line. CommonMark says the info string of a backtick fence cannot contain a backtick, so such a line is a paragraph, usually one that starts with a code span. With no closing fence after it, the code block swallowed the rest of the document.

Lexical's own export produces such lines: inline code that holds two or more backticks in a row is written with a longer fence, so a paragraph starting with it ( ``` ab ``` x ``) turned into a code block on the next load.

This change makes CODE_START_REGEX match only when the rest of the line has no backtick, or is the single-line ```code``` form whose code has none, and makes CODE.handleImportAfterStartMatch() decline a line whose closing run is shorter than the opening one ( a``` ````). normalizeMarkdown() shares the expression, so it no longer stops merging the lines after such a paragraph either.

These keep importing as before: ```Single line Code```, ```a```, a closed ```js fence, a fence with title="x" metadata, nested / ```` fences and the unclosed ````javascript Incomplete tag . The typing shortcut uses the same expression; typing ``` ```` and a space still matches. The fix follows the one suggested in the issue.

One behaviour changes: a single-line block whose code contains one or two backticks, such as ```a`b```, used to import as a code block and now imports as a paragraph, as CommonMark specifies. No existing test covered it. If you'd rather keep the old behaviour for this form, I can add an exception for it.

Closes #9404

Test plan

Before

pnpm exec vitest --project unit --no-watch packages/lexical-markdown/src/__tests__/unit/CodeFenceBacktickInfoString.test.ts

 × reads it as a paragraph that starts with a code span
 × re-imports the editor's own export of inline code
 × round trips such a line unchanged
 × does not take a shorter closing run as the end of a single-line block
 × does not stop merging the lines that follow
AssertionError: expected [ 'code: a``b ``` x' ] to deeply equal [ 'paragraph: a``b x' ]
AssertionError: expected [ 'code: a``b ``` x' ] to deeply equal [ 'paragraph: a``b x' ]
AssertionError: expected '````\na``b ``` x\n````' to be '``` a``b ``` x' // Object.is equality
AssertionError: expected [ 'code: ```' ] to deeply equal [ 'paragraph: ````a```' ]
AssertionError: expected '```` ``` ```` x\nnext' to be '```` ``` ```` x next' // Object.is equality
      Tests  5 failed | 1 passed (6)

After

pnpm exec vitest --project unit --no-watch packages/lexical-markdown packages/lexical-mdast packages/lexical-playground/__tests__/unit

 Test Files  45 passed (45)
      Tests  1181 passed (1181)

tsc, tsc -p tsconfig.test.json, ESLint and Prettier pass on the changed files. Only unit tests (Node) were run; the typing shortcut was not tried in a browser.

AI assistance

This change and its tests were written with help from an AI coding assistant (Claude Code). I reviewed the diff and the test results before opening this PR.

@meta-cla

meta-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Hi @x-labb!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@vercel

vercel Bot commented Oct 22, 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 20, 2026 11:59am UTC
lexical-playground Ready Ready Preview Nov 20, 2026 11:59am UTC

Request Review

@meta-cla

meta-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 Oct 22, 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.

Thanks for the fix and the careful description; the change looks good. Reading a line like ``` a``b ``` x as a paragraph follows CommonMark §4.5 ("If the info string comes after a backtick fence, it may not contain any backtick characters"), and the new tests fail on main and pass with the change. Everything below is optional and non-blocking.

  1. Typed fence shortcut (packages/lexical-markdown/src/MarkdownTransformers.ts:229). runMultilineElementTransformers() in MarkdownShortcuts.ts matches regExpStart against the whole text node, including the text after the caret, so the new lookahead applies there too. Typing ``` and a space at the start of an existing paragraph const x = `y`; used to turn it into a code block and now leaves the text as it is (the same for ```js before a `b` c). AGENTS.md (Backwards Compatibility) asks that existing behaviour stay intact where callers may rely on it. One way to keep it: leave CODE.regExpStart as it was and apply the info-string rule in handleImportAfterStartMatch(), which only import calls and which already has afterFence, and in normalizeMarkdown() through a separate expression.
  2. Stored meta with a backtick (CODE.export, MarkdownTransformers.ts:874-884). Since v0.50.0 the importer kept everything after the language as code meta, backticks included (```js title=`x`). Such a block now exports a fence line that no longer re-imports as a fence, so it comes back as a paragraph and its closing fence opens a code block that takes the rest of the document. Leaving a meta that contains a backtick out of the export would keep those documents round-tripping. It would also help to say in the description that multi-line fences with a backtick in their info string now import as CommonMark specifies, not only ```a`b```.
  3. Tests (AGENTS.md, Testing Strategy: unit tests for editor-state behaviour). A unit test that types the shortcut into an existing paragraph, like the "should preserve a quote when typing" tests in LexicalMarkdown.test.ts, and one assertion for ```a`b``` would pin both behaviours.
  4. Nit: CODE_SINGLE_LINE_REGEX (MarkdownTransformers.ts:231-232) still accepts ``` a``b ``` x, so with shouldMergeAdjacentLines that line is not merged into the line above it (it gets a hard line break, while ```` ``` ```` x merges). Applying the same rule there would make the two cases agree.

On the question in the description: importing ```a`b``` as a paragraph is what CommonMark specifies, and Lexical's exporter never writes that form, so keeping it as a paragraph looks reasonable; maintainers can say if they want the old single-line behaviour kept.

…he fence as a code block

## Description

A line that starts with three or more backticks was always taken as an opening
code fence, even when more backticks follow later on the line. CommonMark says
the info string of a backtick fence cannot contain a backtick, so such a line
is a paragraph, usually one that starts with a code span. With no closing
fence after it, the code block swallowed the rest of the document.

Lexical's own export produces such lines: inline code that holds two or more
backticks in a row is written with a longer fence, so a paragraph starting with
it (`` ``` a``b ``` x ``) turned into a code block on the next load.

This change makes `CODE_START_REGEX` match only when the rest of the line has
no backtick, or is the single-line ```` ```code``` ```` form whose code has
none, and makes `CODE.handleImportAfterStartMatch()` decline a line whose
closing run is shorter than the opening one (```` ````a``` ````).
`normalizeMarkdown()` shares the expression, so it no longer stops merging the
lines after such a paragraph either.

These keep importing as before: ```` ```Single line Code``` ````,
```` ```a``` ````, a closed ```` ```js ```` fence, a fence with `title="x"`
metadata, nested ```` ```` ````/```` ``` ```` fences and the unclosed
```` ```javascript Incomplete tag ````. The typing shortcut uses the same
expression; typing ```` ``` ```` and a space still matches. The fix follows
the one suggested in the issue.

Closes meta#9404

## Test plan

### Before

`pnpm exec vitest --project unit --no-watch packages/lexical-markdown/src/__tests__/unit/CodeFenceBacktickInfoString.test.ts`

```
 × reads it as a paragraph that starts with a code span
 × re-imports the editor's own export of inline code
 × round trips such a line unchanged
 × does not take a shorter closing run as the end of a single-line block
 × does not stop merging the lines that follow
AssertionError: expected [ 'code: a``b ``` x' ] to deeply equal [ 'paragraph: a``b x' ]
AssertionError: expected [ 'code: a``b ``` x' ] to deeply equal [ 'paragraph: a``b x' ]
AssertionError: expected '````\na``b ``` x\n````' to be '``` a``b ``` x' // Object.is equality
AssertionError: expected [ 'code: ```' ] to deeply equal [ 'paragraph: ````a```' ]
AssertionError: expected '```` ``` ```` x\nnext' to be '```` ``` ```` x next' // Object.is equality
      Tests  5 failed | 1 passed (6)
```

### After

`pnpm exec vitest --project unit --no-watch packages/lexical-markdown packages/lexical-mdast packages/lexical-playground/__tests__/unit`

```
 Test Files  45 passed (45)
      Tests  1181 passed (1181)
```

`tsc`, `tsc -p tsconfig.test.json`, ESLint and Prettier pass on the changed
files. Only unit tests (Node) were run; the typing shortcut was not tried in a
browser.
…and round-trip a backtick meta

## Description

Review of the previous commit found that moving the info-string rule into
`CODE.regExpStart` also changed the typing shortcut, which matches that
expression against the whole text node, including the text after the caret.
Typing three backticks and a space at the start of an existing paragraph such
as `` const x = `y`; `` no longer made a code block.

This change:

- restores `CODE.regExpStart` to what it was, so the shortcut is unchanged
- applies the info-string rule to imported Markdown only, through a separate
  expression used in `CODE.handleImportAfterStartMatch()` and
  `normalizeMarkdown()`
- applies the same rule to the single-line check in `normalizeMarkdown()`, so
  `` ``` a``b ``` x `` is merged into the line above it like
  ```` ```` ``` ```` x ````
- leaves a stored meta that contains a backtick out of `CODE.export`: a fence
  line with a backtick in its info string does not read back as a fence, and
  its closing fence would open a block that takes the rest of the document

Multi-line fences whose info string has a backtick now import as CommonMark
specifies, not only the single-line form. A changeset with a `Verify:`
paragraph is added.

## Test plan

### Before

The new tests, run against the code before this commit:
`pnpm exec vitest --project unit --no-watch packages/lexical-markdown/src/__tests__/unit/CodeFenceBacktickInfoString.test.ts`

```
 × merges a line like that into the line above it, as for a longer fence
 × still turns an existing paragraph that has backticks into a code block
 × leaves out a meta that has a backtick, which would not read back as a fence
AssertionError: expected 'a\n``` a``b ``` x' to be 'a ``` a``b ``` x' // Object.is equality
AssertionError: expected [ 'paragraph: ``` const x = `y`;' ] to deeply equal [ 'code: const x = `y`;' ]
AssertionError: expected '```js title=`x`\ncode\n```' to be '```js\ncode\n```' // Object.is equality
      Tests  3 failed | 8 passed (11)
```

### After

`pnpm exec vitest --project unit --no-watch packages/lexical-markdown packages/lexical-mdast packages/lexical-playground/__tests__/unit`

```
 Test Files  57 passed (57)
      Tests  1845 passed (1845)
```

`tsc`, `tsc -p tsconfig.test.json`, ESLint, Prettier and
`pnpm run check-changesets` pass. Only unit tests (Node) were run; the typing
shortcut is covered by a unit test, not tried in a browser.

@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 the follow-up — the change looks good. Reading a line like ``` a``b ``` x as a paragraph follows CommonMark §4.5 ("If the info string comes after a backtick fence, it may not contain any backtick characters"), both points from the earlier review are addressed (the typing shortcut still matches the unchanged CODE.regExpStart, and normalizeMarkdown() now treats ``` a``b ``` x like the longer-fence form), and the new tests fail on main and pass with the change. Everything below is optional and non-blocking.

  1. Changeset Verify: paragraph (.changeset/markdown-fence-backtick-info-string.md:7). It names the inline-code case and the shortcut. Two other observable changes are worth a line for people upgrading: a fence whose info string holds a backtick (```a`b```, ```js title=`x`) now imports as a paragraph, so Markdown that earlier versions exported for a code block with such a meta now reads as a paragraph followed by an unclosed code block; and CODE.export now leaves out a code-block meta that holds a backtick. CONTRIBUTING.md (Changesets) and .changeset/README.md (Behavior changes) ask the Verify: paragraph to say what to test when output or behaviour changes.
  2. Description and Test plan. Both still describe the first commit: the description says the change "makes CODE_START_REGEX match only when…" and that the typing shortcut uses the same expression, while this head adds CODE_IMPORT_START_REGEX for import only and also changes CODE.export; the Test plan's output is a 6-test run, and the file now has 11 tests. AGENTS.md (Commits and Pull Requests) asks the Description to say what the change does and the Test plan to carry the real Before and After output.

On the question in the description: importing ```a`b``` as a paragraph is what CommonMark specifies, Lexical's exporter never writes that form, and the new test pins it, so keeping it as a paragraph looks reasonable; maintainers can say if they want the old single-line behaviour kept.

The CI workflows have not run on this commit yet; they need a maintainer's approval to start.

…nce changeset

## Description

The `Verify:` paragraph of the changeset now also names the two other
observable changes: a fence whose info string has a backtick imports as a
paragraph, and `CODE.export` leaves out a stored meta that contains a backtick.

Changeset text only, so there is no before and after to show.

@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 this fix — it looks good. Lines such as ``` a``b ``` x now import as paragraphs, as CommonMark's info-string rule says (§4.5: the info string after a backtick fence may not contain a backtick), Lexical's own inline-code export round-trips, and keeping CODE.regExpStart unchanged leaves the typing shortcut alone. The new test fails on the base and passes here.

A few optional, non-blocking suggestions:

  1. normalizeMarkdown() and ````a``` (packages/lexical-markdown/src/MarkdownTransformers.ts:1475). The lookahead's second alternative accepts a closing run shorter than the opening fence, so normalizeMarkdown() still treats ````a``` as opening a four-backtick block, while handleImportAfterStartMatch() now declines the same line (line 946). With shouldMergeAdjacentLines, every later line then stays unmerged: normalizeMarkdown('x\n````a```\ny', true) returns its input. Requiring the closing run to be at least as long as the opening one keeps the two in step (only group 1 is read, at line 1477):

    const CODE_IMPORT_START_REGEX =
      /^([ \t]*(`{3,}))(?=[^`]*$|[^`]+\2`*$)([\w-]+)?[ \t]?/;

    A normalizeMarkdown(…, true) case for ````a``` in CodeFenceBacktickInfoString.test.ts would cover it.

  2. Changeset Verify: paragraph. Markdown that earlier versions wrote for a code block whose meta held a backtick (```js title=`x`) now reads as a paragraph, and that block's closing fence then opens a code block running to the end of the document (CommonMark reads it the same way). Saying so in Verify: would tell people holding such Markdown what to check; CONTRIBUTING.md (Changesets) asks Verify: to say what to test when output changes.

  3. Pull request description. It still describes the first version (changing CODE_START_REGEX; "the typing shortcut uses the same expression"), and the Test plan's Before block lists 6 tests while the file now has 11. AGENTS.md (Commits and Pull Requests) asks the description to follow .github/pull_request_template.md with the real Before and After output, and it seeds the commit message, so an update before merge keeps the history accurate.

Whether ```a`b``` may move from a code block to a paragraph is a maintainer call; CommonMark supports the new reading.

Automated review (maintainer link)

if (codeBlockFenceLength === 0) {
// An opening fence may carry an info string (e.g. ```ts)
const openMatch = line.match(CODE_START_REGEX);
const openMatch = line.match(CODE_IMPORT_START_REGEX);

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: the lookahead's second alternative ([^`]+`{3,}$) accepts a closing run shorter than the opening fence, so this match sets codeBlockFenceLength = 4 for ````a```, while handleImportAfterStartMatch() now declines that line (line 946) and reads a paragraph. With shouldMergeAdjacentLines, every later line then stays unmerged until a bare fence of four or more backticks; normalizeMarkdown('x\n````a```\ny', true) returns its input. Requiring the closing run to be at least the opening length keeps the two in step (only group 1 is read below):

const CODE_IMPORT_START_REGEX =
  /^([ \t]*(`{3,}))(?=[^`]*$|[^`]+\2`*$)([\w-]+)?[ \t]?/;

This branch was successfully deployed

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

Bug: A line starting with three backticks and with more backticks later is imported as a code block, including Lexical's own export of inline code

1 participant