From 99de3eeff78c778aac2e8cec376fdf4eba3ac572 Mon Sep 17 00:00:00 2001 From: suyao Date: Fri, 24 Oct 2025 04:43:56 +0800 Subject: [PATCH] Improve paste behavior to preserve inline text formatting - Insert plain text when pasting mid-line to avoid unwanted line breaks - Only convert markdown to HTML when pasting at line start or in empty paragraphs - Disable automatic line break conversion in markdown parser to prevent extra paragraphs --- .../src/components/RichEditor/useRichEditor.ts | 14 +++++++++----- src/renderer/src/utils/markdownConverter.ts | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/components/RichEditor/useRichEditor.ts b/src/renderer/src/components/RichEditor/useRichEditor.ts index 576162fac7..536e7620bb 100644 --- a/src/renderer/src/components/RichEditor/useRichEditor.ts +++ b/src/renderer/src/components/RichEditor/useRichEditor.ts @@ -421,17 +421,21 @@ export const useRichEditor = (options: UseRichEditorOptions = {}): UseRichEditor // Default behavior for non-code blocks const text = event.clipboardData?.getData('text/plain') ?? '' if (text) { - const html = markdownToHtml(text) const { $from } = selection const atStartOfLine = $from.parentOffset === 0 const inEmptyParagraph = $from.parent.type.name === 'paragraph' && $from.parent.textContent === '' + // If pasting in the middle of a line, insert as plain text to avoid unwanted line breaks if (!atStartOfLine && !inEmptyParagraph) { - const cleanHtml = html.replace(/^

(.*?)<\/p>/s, '$1') - editor.commands.insertContent(cleanHtml) - } else { - editor.commands.insertContent(html) + // Insert plain text without creating new paragraphs + const tr = view.state.tr.insertText(text, selection.from, selection.to) + view.dispatch(tr) + return true } + + // At start of line or in empty paragraph: convert markdown to HTML and insert + const html = markdownToHtml(text) + editor.commands.insertContent(html) onPaste?.(html) return true } diff --git a/src/renderer/src/utils/markdownConverter.ts b/src/renderer/src/utils/markdownConverter.ts index 56f48cd465..51d684612d 100644 --- a/src/renderer/src/utils/markdownConverter.ts +++ b/src/renderer/src/utils/markdownConverter.ts @@ -81,7 +81,7 @@ export interface TaskListOptions { const md = new MarkdownIt({ html: true, // Enable HTML tags in source xhtmlOut: true, // Use '>' for single tags (
instead of
) - breaks: true, // Preserve line breaks when pasting text + breaks: false, linkify: false, // Autoconvert URL-like text to links typographer: false // Enable smartypants and other sweet transforms })