Skip to content

[lexical-markdown] Bug Fix: keep a blank first line of a fenced code block - #9003

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-leading-blank
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-leading-blank

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

MultilineElementTransformer.replace documents linesInBetween as the lines
between the start and end matches. The default $importMultiline path builds
it so that linesInBetween[0] is always the remainder of the opening fence
line
— it pushes lines[startLineIndex].slice(startMatch[0].length) even when
that remainder is empty. CODE.replace relies on that contract and drops a
blank first entry:

if (linesInBetween[0].trim().length === 0) {
  linesInBetween.shift();
}

CODE.handleImportAfterStartMatch builds linesInBetween itself, and only
unshifted the fence-line remainder when it was non-empty. For the common
```js fence the remainder is empty, so index 0 became the first real line
of code instead. When that line is blank, replace mistakes it for the
fence-line remainder and deletes it.

The result is that a code block starting with a blank line silently loses it on
import, and markdown -> editor -> markdown is not idempotent: export writes
exactly one newline after the fence, so every extra round trip eats one more
blank line.

```\n\n\ncode\n```   ->   ```\n\ncode\n```   ->   ```\ncode\n```

This unshifts the remainder unconditionally, so linesInBetween[0] means the
same thing in both import paths. Text that follows the opening fence on the
same line is still treated as content (``` code), unterminated fences are
unchanged, and trailing blank lines are still trimmed as before.

Test plan

New unit test packages/lexical-markdown/src/__tests__/unit/CodeBlockLeadingBlankLine.test.ts.

Before

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

     × keeps a blank first line of a fenced code block 7ms
     × keeps several blank first lines 1ms
     × round trips a code block that starts with a blank line 1ms

AssertionError: expected 'code' to be '\ncode' // Object.is equality
AssertionError: expected '\na\nb' to be '\n\na\nb' // Object.is equality
AssertionError: expected '```js\ncode\n```' to be '```js\n\ncode\n```' // Object.is equality

      Tests  3 failed | 2 passed (5)

After

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

 Test Files  1 passed (1)
      Tests  5 passed (5)

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

 Test Files  13 passed (13)
      Tests  643 passed (643)

…block

## Description

`MultilineElementTransformer.replace` documents `linesInBetween` as the lines
between the start and end matches. The default `$importMultiline` path builds
it so that `linesInBetween[0]` is always the *remainder of the opening fence
line* — it pushes `lines[startLineIndex].slice(startMatch[0].length)` even when
that remainder is empty. `CODE.replace` relies on that contract and drops a
blank first entry:

```js
if (linesInBetween[0].trim().length === 0) {
  linesInBetween.shift();
}
```

`CODE.handleImportAfterStartMatch` builds `linesInBetween` itself, and only
unshifted the fence-line remainder when it was non-empty. For the common
` ```js ` fence the remainder *is* empty, so index 0 became the first real line
of code instead. When that line is blank, `replace` mistakes it for the
fence-line remainder and deletes it.

The result is that a code block starting with a blank line silently loses it on
import, and markdown -> editor -> markdown is not idempotent: export writes
exactly one newline after the fence, so every extra round trip eats one more
blank line.

```
```\n\n\ncode\n```   ->   ```\n\ncode\n```   ->   ```\ncode\n```
```

This unshifts the remainder unconditionally, so `linesInBetween[0]` means the
same thing in both import paths. Text that follows the opening fence on the
same line is still treated as content (` ``` code `), unterminated fences are
unchanged, and trailing blank lines are still trimmed as before.

## Test plan

New unit test `packages/lexical-markdown/src/__tests__/unit/CodeBlockLeadingBlankLine.test.ts`.

### Before

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

     × keeps a blank first line of a fenced code block 7ms
     × keeps several blank first lines 1ms
     × round trips a code block that starts with a blank line 1ms

AssertionError: expected 'code' to be '\ncode' // Object.is equality
AssertionError: expected '\na\nb' to be '\n\na\nb' // Object.is equality
AssertionError: expected '```js\ncode\n```' to be '```js\n\ncode\n```' // Object.is equality

      Tests  3 failed | 2 passed (5)
```

### After

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

 Test Files  1 passed (1)
      Tests  5 passed (5)

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

 Test Files  13 passed (13)
      Tests  643 passed (643)
```
@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.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
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