Skip to content

fix(markdown): keep spaces for line breaks in pasted html - #8095

Open
No898 wants to merge 2 commits into
decaporg:mainfrom
No898:8094-fix-markdown-paste-line-breaks
Open

No898 wants to merge 2 commits into
decaporg:mainfrom
No898:8094-fix-markdown-paste-line-breaks

Conversation

@No898

@No898 No898 commented Nov 19, 2026 •

Copy link
Copy Markdown

Summary

Fixes #8094

Pasting rich text into the markdown widget merges words that are separated by a line break in the pasted HTML (first\r\nsecond → firstsecond). Many applications wrap long lines in the HTML they put on the clipboard and use a line break in place of the space between two words. Browsers render that line break as a space.

#7440 fixed extra blank lines around pasted content (#7364) by removing every line break from text nodes. That is correct for whitespace-only text between tags (<body>\n<p>), but it also removes line breaks inside text.

This PR keeps the #7364 fix and handles line breaks in text nodes as whitespace:

  • a line break inside text, or between inline elements, becomes a single space,
  • whitespace at the start or end of a block (next to block elements) is still dropped, so no extra lines are added around pasted content,
  • whitespace next to a <br> is dropped too, so a manual line break from Word (<br>\r\n) does not leave a leading space on the next line,
  • line breaks inside <pre> are kept, as whitespace is significant there (before this PR, #7440 also removed them from pasted code blocks).

Text without line breaks is returned unchanged.

Test plan

New cases in withHtml.spec.js:

  • <p>first\r\nsecond words</p> and <p>first\nsecond</p> keep the space,
  • <p><span>first</span>\r\n<span>second</span></p> serializes to first second,
  • <p>first<br>\r\nsecond</p> gives first\nsecond without a leading space,
  • <body>\r\n<p>one</p>\r\n<p>two\r\n</p>\r\n</body> serializes to one\n\ntwo (Extra whitespace and line breaks being inserted on paste in Windows. #7364 regression guard, it also passes on main),
  • <pre><code> keeps its line breaks.
NODE_ENV=test npx jest packages/decap-cms-widget-markdown
# Test Suites: 1 skipped, 17 passed, 17 of 18 total
# Tests:       652 skipped, 133 passed, 785 total

The cases for words, inline elements and code blocks fail on main and pass with this change. The <br> and #7364 cases pass on both and guard against regressions. eslint and prettier --check pass on the changed files.

I also checked it in a real browser: with decap-cms-app 3.18.0 and this change applied to decap-cms-widget-markdown, pasting <p>… rozhodujících\r\nchvílích …</p> via insertFromPaste gives rozhodujících chvílích instead of rozhodujícíchchvílích, and the result is otherwise identical to main (no extra lines).

Confirmed with a real copy and paste from Microsoft Word, see #8094 (comment).

Checklist

Please add a x inside each checkbox:

Pasted HTML often wraps long lines with a line break in place of the
space between two words. decaporg#7440 removed every line break from text nodes
to fix extra blank lines around pasted content (decaporg#7364), which merged
those words.

Line breaks inside text and between inline elements now become a space,
whitespace next to block elements is still dropped, and line breaks in
<pre> are kept.

Fixes decaporg#8094
@No898
No898 requested a review from a team as a code owner November 19, 2026 15:13
@netlify

netlify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

👷 Deploy request for decap-cms pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6809315

Word pastes a manual line break as `<br>\r\n`. The line break after
the `<br>` became a leading space on the next line. Treat `<br>` like a
block boundary, so whitespace next to it is dropped.

Refs decaporg#8094

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown widget: pasted HTML merges words separated by a line break

1 participant