Skip to content

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

Merged
potatowagon merged 1 commit into
meta:mainfrom
luantaraschi:fix/markdown-selection-leading-newline
Aug 19, 2026
Merged

potatowagon merged 1 commit into
meta: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 [email protected]. 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!

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Aug 16, 2026
@potatowagon

Copy link
Copy Markdown
Contributor

LGTM — nice root-cause. The bug is that the leading \n was keyed off the child index (i > 0 / children[i - 1]), but unselected children emit nothing, so the index doesn't reflect the position in the output. Tracking previousExported (the last block that actually produced output) is the right fix.

A couple of things I verified:

  • When every child is selected, previousExported === children[i - 1], so there's no behavior change on the existing fully-selected path. ✅
  • LexicalNode is already imported in this file, so the LexicalNode | null annotation is fine. ✅
  • Agreed that createMarkdownExport has the same shape but not the same bug — $exportTopLevelElements only returns null for a node that's neither element nor decorator, which a root child never is, so every child there contributes.

Non-blocking nit: the test covers the core case (selection starting below the first block) well. If you feel like hardening it, a case with an empty paragraph between two selected blocks would exercise the isEmptyParagraph(previousExported) branch directly — but the existing suite already guards that logic, so not required.

@potatowagon
potatowagon added this pull request to the merge queue Aug 19, 2026
Merged via the queue into meta:main with commit 8a9d010 Aug 19, 2026
48 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — be3e35fb Deployed Aug 7, 2026 by vercel[bot]
Preview – lexical-playground — be3e35fb Deployed Aug 7, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants