Repository navigation
Conversation
…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)
```
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 05:07
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MultilineElementTransformer.replacedocumentslinesInBetweenas the linesbetween the start and end matches. The default
$importMultilinepath buildsit so that
linesInBetween[0]is always the remainder of the opening fenceline — it pushes
lines[startLineIndex].slice(startMatch[0].length)even whenthat remainder is empty.
CODE.replacerelies on that contract and drops ablank first entry:
CODE.handleImportAfterStartMatchbuildslinesInBetweenitself, and onlyunshifted the fence-line remainder when it was non-empty. For the common
```jsfence the remainder is empty, so index 0 became the first real lineof code instead. When that line is blank,
replacemistakes it for thefence-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.
This unshifts the remainder unconditionally, so
linesInBetween[0]means thesame thing in both import paths. Text that follows the opening fence on the
same line is still treated as content (
``` code), unterminated fences areunchanged, and trailing blank lines are still trimmed as before.
Test plan
New unit test
packages/lexical-markdown/src/__tests__/unit/CodeBlockLeadingBlankLine.test.ts.Before
After