Skip to content

[lexical-markdown] Bug Fix: don't prefix a newline when a selection starts below the first block - #8960

Open
luantaraschi wants to merge 1 commit into
facebook:mainfrom
luantaraschi:fix/markdown-selection-leading-newline
Open

[lexical-markdown] Bug Fix: don't prefix a newline when a selection starts below the first block#8960
luantaraschi wants to merge 1 commit into
facebook:mainfrom
luantaraschi:fix/markdown-selection-leading-newline

Conversation

@luantaraschi

Copy link
Copy Markdown
Contributor

Description

$convertSelectionToMarkdownString decides whether to prefix a block with \n by looking at the block's index among the root's children:

isNewlineDelimited &&
  i > 0 &&
  !isEmptyParagraph(child) &&
  !isEmptyParagraph(children[i - 1])

Unselected children emit nothing, so the index is not the position in the output. When the selection starts below the top of the document, the first block that does get emitted still has i > 0, and a newline is prefixed to it. The returned markdown then opens with a blank line.

With three paragraphs and a selection covering only the last two:

$convertSelectionToMarkdownString(TRANSFORMERS, $getSelection())
// '\nSecond\n\nThird', expected 'Second\n\nThird'

The separator now tracks the previously emitted block instead of the previous child. When every child is selected, the previously emitted block is children[i - 1], so nothing changes for the existing cases.

createMarkdownExport has the same shape but not the same bug: $exportTopLevelElements only returns null for a node that is neither an element nor a decorator, which a root child never is, so every child there contributes to the output.

Test plan

Before

FAIL packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
  > $convertSelectionToMarkdownString
    > does not prefix a newline when the selection starts after the first block

AssertionError: expected '\nSecond\n\nThird' to be 'Second\n\nThird'

After

Test Files  1 passed (1)
     Tests  417 passed (417)

pnpm run prettier and pnpm run tsc are both clean.

…tarts below the first block

## Description

`$convertSelectionToMarkdownString` decided whether to prefix a block with
`\n` by looking at the block's index among the root's children. Unselected
children emit nothing, so that index is not the position in the output: when
the selection starts below the top of the document the first emitted block
still has `i > 0` and gets a newline prefixed, so the markdown opens with a
blank line.

The separator now tracks the previously emitted block. When every child is
selected the previously emitted block is `children[i - 1]`, so the existing
cases are unchanged.

## Test plan

### Before

expected '\nSecond\n\nThird' to be 'Second\n\nThird'

### After

417 passed, prettier and tsc clean.
Copilot AI lite review requested due to automatic review settings August 7, 2026 16:39
@vercel

vercel Bot commented Aug 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 7, 2026 4:40pm
lexical-playground Ready Ready Preview Aug 7, 2026 4:40pm

Request Review

@meta-cla

meta-cla Bot commented Aug 7, 2026

Copy link
Copy Markdown

Hi @luantaraschi!

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!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 7, 2026
@meta-cla

meta-cla Bot commented Aug 7, 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!

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