Skip to content

[lexical-markdown] Bug Fix: don't import a code fence's info string as code - #9020

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-info-string
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-info-string

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

CommonMark defines everything after an opening code fence as the info string,
whose first word is conventionally the language, and states that the info
string is not part of the code block's content.
https://spec.commonmark.org/0.31.2/#fenced-code-blocks

CODE_START_REGEX captures only the leading word as the language
(([\w-]+)?). handleImportAfterStartMatch then takes whatever is left on the
fence line and prepends it to the block's content:

const afterFullMatch = currentLine.slice(startMatch[0].length);
if (afterFullMatch.length > 0) {
  linesInBetween.unshift(afterFullMatch);
}

So the very common attribute forms produce a code block whose first line of
code
is the metadata:

markdown imported code
```js title="x" / code / ``` title="x"\ncode
```ts {1,3} / code / ``` {1,3}\ncode
```js showLineNumbers / a / b / ``` showLineNumbers\na\nb

The user's code is corrupted with a line they never wrote, and it survives
export, so the round trip entrenches it.

This skips the remainder only when a language was actually captured — that is
exactly the case where the text is a trailing info string. Two existing
behaviours are deliberately untouched:

  • A fence with no language (``` code) keeps its remainder as content;
    nothing there looks like an info string to this parser.
  • An unterminated fence keeps its lenient reading (the
    '```javascript Incomplete tag' cases in LexicalMarkdown.test.ts), so this
    only applies where a closing fence makes the block unambiguous.

CodeNode stores a single language, and the serialization format is fixed,
so there is nowhere to retain the rest of the info string; dropping unsupported
metadata matches what the importer already does for anything else it cannot
model.

Test plan

New unit test packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts.
The last three cases are controls for the behaviours listed above — they pass
before and after.

Before

$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts

     × does not put a title attribute into the code content 18ms
     × does not put a line-highlight range into the code content 2ms
     × does not put a bare info-string word into the code content 1ms

AssertionError: expected { language: 'js', …(1) } to deeply equal { language: 'js', text: 'code' }

      Tests  3 failed | 3 passed (6)

After

$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts

      Tests  6 passed (6)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core

 Test Files  7 passed (7)
      Tests  467 passed (467)

…s code

## Description

CommonMark defines everything after an opening code fence as the *info string*,
whose first word is conventionally the language, and states that the info
string is not part of the code block's content.
https://spec.commonmark.org/0.31.2/#fenced-code-blocks

`CODE_START_REGEX` captures only the leading word as the language
(`([\w-]+)?`). `handleImportAfterStartMatch` then takes whatever is left on the
fence line and prepends it to the block's content:

```js
const afterFullMatch = currentLine.slice(startMatch[0].length);
if (afterFullMatch.length > 0) {
  linesInBetween.unshift(afterFullMatch);
}
```

So the very common attribute forms produce a code block whose *first line of
code* is the metadata:

| markdown | imported code |
| --- | --- |
| ` ```js title="x" ` / `code` / ` ``` ` | `title="x"\ncode` |
| ` ```ts {1,3} ` / `code` / ` ``` ` | `{1,3}\ncode` |
| ` ```js showLineNumbers ` / `a` / `b` / ` ``` ` | `showLineNumbers\na\nb` |

The user's code is corrupted with a line they never wrote, and it survives
export, so the round trip entrenches it.

This skips the remainder only when a language was actually captured — that is
exactly the case where the text is a trailing info string. Two existing
behaviours are deliberately untouched:

- A fence with **no** language (` ``` code `) keeps its remainder as content;
  nothing there looks like an info string to this parser.
- An **unterminated** fence keeps its lenient reading (the
  `'```javascript Incomplete tag'` cases in `LexicalMarkdown.test.ts`), so this
  only applies where a closing fence makes the block unambiguous.

`CodeNode` stores a single `language`, and the serialization format is fixed,
so there is nowhere to retain the rest of the info string; dropping unsupported
metadata matches what the importer already does for anything else it cannot
model.

## Test plan

New unit test `packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts`.
The last three cases are controls for the behaviours listed above — they pass
before and after.

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts

     × does not put a title attribute into the code content 18ms
     × does not put a line-highlight range into the code content 2ms
     × does not put a bare info-string word into the code content 1ms

AssertionError: expected { language: 'js', …(1) } to deeply equal { language: 'js', text: 'code' }

      Tests  3 failed | 3 passed (6)
```

### After

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockInfoString.test.ts

      Tests  6 passed (6)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core

 Test Files  7 passed (7)
      Tests  467 passed (467)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@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 Aug 9, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9055 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

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