Repository navigation
fix(richtext-lexical): preserve hard line breaks on markdown import - #17060
Merged
Merged
Conversation
Backports lexical PR #8402 into Payload's forked @lexical/markdown. CommonMark hard line breaks (two-or-more trailing spaces or a trailing backslash) were silently merged into the previous line during $convertFromMarkdownString import. The marker is now detected during normalization/import, stripped from the text, and stored on the LineBreakNode via node state so it round trips on export. The upstream resetOnCopyNode option is omitted because it does not exist in the pinned lexical@0.41.0.
3 tasks done
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
Mirrors lexical PR #8402 coverage for hard line breaks that follow formatted text and links, where the trailing-space marker is split into its own text node and must be moved onto the LineBreakNode. Exercises the hasNonWhitespaceContentOnLine branch that the original tests did not cover.
r1tsuu
approved these changes
Jun 19, 2026
GermanJablo
added a commit
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>
1 task
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.
Summary
Markdown imported via
$convertFromMarkdownStringsilently dropped CommonMark 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 into Payload's forked
@lexical/markdown. We maintain a fork of@lexical/markdown(rather than re-exporting it), and it is pinned tolexical@0.41.0, so the upstream fix (shipped in0.45.0) is not picked up by a dependency bump alone.What changed
MarkdownTransformers.tsparseMarkdownHardLineBreakto detect a trailing\or 2+ spaces marker.hardLineBreakStatenode-state config and$createMarkdownLineBreakNode, which strips the marker from the text and stores it on the createdLineBreakNode.normalizeMarkdownno longer merges a following line into a line that ends with a hard break marker, and preserves the marker when merging.QUOTEtransformer uses$createMarkdownLineBreakNodefor multi-line quote joins.MarkdownImport.tsuses$createMarkdownLineBreakNodeinstead of a plain$createLineBreakNodewhen joining adjacent blocks.MarkdownExport.tsre-emits the stored marker (\nor\\\n) on export so the value round trips; line breaks without a marker still export as a plain\n(unchanged behavior).Note on
resetOnCopyNodeUpstream sets
resetOnCopyNode: trueon the node state. That option does not exist inlexical@0.41.0(it was added in0.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/markdowndirectly) is planned for v4 only and is not part of this PR.Test plan
LexicalMarkdownHardLineBreak.spec.tscovering normalization, import (marker stripped +LineBreakNodecreated), and export round trip for both the trailing-spaces and backslash forms, plus a soft-wrap regression check.vitest --project unit packages/richtext-lexical(existing + 10 new tests pass).eslint+prettierclean on changed files.