Skip to content

Commit c7e7dda

Browse files
committed
Treat newlines as linebreaks when pasting as plaintext
Until now, every newline was transformed into a paragraph break when pasting plaintext. Instead, newlines will be treated as linebreaks. Two newlines in a row will be treated as paragraph break. Fixes: #3598 Manual backport of #3623 Signed-off-by: Jonas <jonas@freesources.org>
1 parent 47f6141 commit c7e7dda

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/extensions/Markdown.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
*/
4040

4141
import { Extension, getExtensionField } from '@tiptap/core'
42+
import { Plugin, PluginKey } from 'prosemirror-state'
4243
import { MarkdownSerializer, defaultMarkdownSerializer } from 'prosemirror-markdown'
44+
import { DOMParser } from 'prosemirror-model'
4345

4446
const Markdown = Extension.create({
4547

@@ -67,6 +69,24 @@ const Markdown = Extension.create({
6769
}
6870
},
6971

72+
addProseMirrorPlugins() {
73+
return [
74+
new Plugin({
75+
key: new PluginKey('pasteEventHandler'),
76+
props: {
77+
clipboardTextParser(str, $context, _, view) {
78+
const parser = DOMParser.fromSchema(view.state.schema)
79+
const doc = document.cloneNode(false)
80+
const dom = doc.createElement('div')
81+
// Treat single newlines as linebreaks and double newlines as paragraph breaks when pasting as plaintext
82+
dom.innerHTML = '<p>' + str.replaceAll('\n', '<br />').replaceAll('<br /><br />', '</p><p>') + '</p>'
83+
84+
return parser.parseSlice(dom, { preserveWhitespace: true, context: $context })
85+
},
86+
},
87+
}),
88+
]
89+
},
7090
})
7191

7292
const createMarkdownSerializer = ({ nodes, marks }) => {

0 commit comments

Comments
 (0)