Repository navigation
[lexical-markdown] Bug Fix: Preserve hard line breaks in default markdown import - #8402
Merged
Merged
Conversation
pro-vi
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon,
takuyakanbr and
zurfyx
as code owners
April 27, 2026 22:28
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
reviewed
Apr 28, 2026
etrepum
reviewed
Apr 29, 2026
pro-vi
force-pushed
the
preserve-default-markdown-hard-breaks
branch
from
April 30, 2026 04:49
28a9092 to
41a41f6
Compare
pro-vi
force-pushed
the
preserve-default-markdown-hard-breaks
branch
from
April 30, 2026 09:05
ff6653a to
f781466
Compare
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. |
etrepum
approved these changes
May 1, 2026
Merged
This was referenced Jun 19, 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>
1 task
This branch was successfully deployed
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
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 --runpnpm run test-unitpnpm run ci-checkpnpm run build