Skip to content

[lexical-markdown] Bug Fix: Preserve hard line breaks in default markdown import - #8402

Merged
etrepum merged 5 commits into
meta:mainfrom
pro-vi:preserve-default-markdown-hard-breaks
May 1, 2026
Merged

etrepum merged 5 commits into
meta:mainfrom
pro-vi:preserve-default-markdown-hard-breaks

Conversation

@pro-vi

@pro-vi pro-vi commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Preserve CommonMark hard line break markers when default markdown import joins adjacent lines into a LineBreakNode.

The preserve-newlines path already keeps hard-break spelling because it preserves source lines directly. In default import, the hard-break marker was stripped before export could distinguish it from a soft line break. In merge-adjacent mode, hard breaks could also be folded into spaces.

This adds markdown provenance to generated LineBreakNodes with NodeState, reuses that path for paragraph/list/quote line joins, and exports tagged line breaks with their original marker style.

Test plan

  • pnpm vitest --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts packages/lexical-markdown/src/__tests__/unit/MarkdownTransformers.test.ts --run
  • pnpm run test-unit
  • pnpm run ci-check
  • pnpm run build

@vercel

vercel Bot commented Apr 27, 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, Comment Apr 30, 2026 9:06am
lexical-playground Ready Ready Preview, Comment Apr 30, 2026 9:06am

Request Review

@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 Apr 27, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 28, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There are some conflicts that need to be resolved since #8395 was merged but otherwise I think this is in pretty good shape

@pro-vi

pro-vi commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks again for the detailed review. I applied the parser/state cleanup and then checked the import cases more closely and found one follow-up issue: inline transforms can split hard-break syntax into a separate trailing text node after formatted/link content (see new test cases), so the marker could stay as document text instead of moving onto the LineBreakNode.

I added coverage for formatted text, exact space counts, links, blockquotes, and a guard for spaces inside link labels.

@pro-vi
pro-vi requested a review from etrepum April 30, 2026 10:37
@etrepum
etrepum added this pull request to the merge queue May 1, 2026
Merged via the queue into meta:main with commit 0aaee65 May 1, 2026
39 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026
GermanJablo added a commit to payloadcms/payload that referenced this pull request Jun 19, 2026
…17060)

## Summary

Markdown imported via `$convertFromMarkdownString` silently dropped
CommonMark [hard line
breaks](https://spec.commonmark.org/0.31.2/#hard-line-breaks): a line
ending in two-or-more trailing spaces or a trailing backslash was merged
into the previous line as a soft wrap, losing the intended `<br>`. This
was reported by a customer importing markdown content.

This backports the relevant parts of upstream lexical [PR
#8402](meta/lexical#8402) into Payload's
forked `@lexical/markdown`. We maintain a fork of `@lexical/markdown`
(rather than re-exporting it), and it is pinned to `lexical@0.41.0`, so
the upstream fix (shipped in `0.45.0`) is not picked up by a dependency
bump alone.

### What changed

- **`MarkdownTransformers.ts`**
- Adds `parseMarkdownHardLineBreak` to detect a trailing `\` or 2+
spaces marker.
- Adds a `hardLineBreakState` node-state config and
`$createMarkdownLineBreakNode`, which strips the marker from the text
and stores it on the created `LineBreakNode`.
- `normalizeMarkdown` no longer merges a following line into a line that
ends with a hard break marker, and preserves the marker when merging.
- The `QUOTE` transformer uses `$createMarkdownLineBreakNode` for
multi-line quote joins.
- **`MarkdownImport.ts`** uses `$createMarkdownLineBreakNode` instead of
a plain `$createLineBreakNode` when joining adjacent blocks.
- **`MarkdownExport.ts`** re-emits the stored marker (` \n` or `\\\n`)
on export so the value round trips; line breaks without a marker still
export as a plain `\n` (unchanged behavior).

### Note on `resetOnCopyNode`

Upstream sets `resetOnCopyNode: true` on the node state. That option
does not exist in `lexical@0.41.0` (it was added in `0.42.0`), so it is
intentionally omitted here. It only affects copy/paste node duplication
and is not required for correct markdown import/export.

This is the v3/v4 backport into the fork. A separate, larger change
(removing the fork and consuming upstream `@lexical/markdown` directly)
is planned for v4 only and is not part of this PR.

## Test plan

- [x] Added `LexicalMarkdownHardLineBreak.spec.ts` covering
normalization, import (marker stripped + `LineBreakNode` created), and
export round trip for both the trailing-spaces and backslash forms, plus
a soft-wrap regression check.
- [x] `vitest --project unit packages/richtext-lexical` (existing + 10
new tests pass).
- [x] `eslint` + `prettier` clean on changed files.

---------

Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>
GermanJablo added a commit to payloadcms/payload that referenced this pull request Jun 19, 2026
…17061)

## Summary

Markdown imported via `$convertFromMarkdownString` silently dropped
CommonMark [hard line
breaks](https://spec.commonmark.org/0.31.2/#hard-line-breaks): a line
ending in two-or-more trailing spaces or a trailing backslash was merged
into the previous line as a soft wrap, losing the intended `<br>`.

This backports the relevant parts of upstream lexical [PR
#8402](meta/lexical#8402) into Payload's
forked `@lexical/markdown`. We maintain a fork of `@lexical/markdown`
(rather than re-exporting it), and it is pinned to `lexical@0.41.0`, so
the upstream fix (shipped in `0.45.0`) is not picked up by a dependency
bump alone.

This is the 3.x backport. The equivalent v4 PR is #17060.

### What changed

- **`MarkdownTransformers.ts`**
- Adds `parseMarkdownHardLineBreak` to detect a trailing `\` or 2+
spaces marker.
- Adds a `hardLineBreakState` node-state config and
`$createMarkdownLineBreakNode`, which strips the marker from the text
and stores it on the created `LineBreakNode`.
- `normalizeMarkdown` no longer merges a following line into a line that
ends with a hard break marker, and preserves the marker when merging.
- The `QUOTE` transformer uses `$createMarkdownLineBreakNode` for
multi-line quote joins.
- **`MarkdownImport.ts`** uses `$createMarkdownLineBreakNode` instead of
a plain `$createLineBreakNode` when joining adjacent blocks.
- **`MarkdownExport.ts`** re-emits the stored marker (` \n` or `\\\n`)
on export so the value round trips; line breaks without a marker still
export as a plain `\n` (unchanged behavior).

### Note on `resetOnCopyNode`

Upstream sets `resetOnCopyNode: true` on the node state. That option
does not exist in `lexical@0.41.0` (it was added in `0.42.0`), so it is
intentionally omitted here. It only affects copy/paste node duplication
and is not required for correct markdown import/export.

## Test plan

- [x] Added `LexicalMarkdownHardLineBreak.spec.ts` covering
normalization, import (marker stripped + `LineBreakNode` created), and
export round trip for both the trailing-spaces and backslash forms, plus
a soft-wrap regression check.
- [x] `vitest --project unit` for the new spec passes (10/10) on this
3.x branch.
- [x] `eslint` + `prettier` clean on changed files.

---------

Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>

This branch was successfully deployed

2 active deployments
Preview – lexical — f7814661 Deployed Apr 30, 2026 by vercel[bot]
Preview – lexical-playground — f7814661 Deployed Apr 30, 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.

2 participants