diff --git a/apps/client/src/features/editor/extensions/markdown-clipboard.ts b/apps/client/src/features/editor/extensions/markdown-clipboard.ts index 1eb05d0a..e1e3707d 100644 --- a/apps/client/src/features/editor/extensions/markdown-clipboard.ts +++ b/apps/client/src/features/editor/extensions/markdown-clipboard.ts @@ -7,7 +7,7 @@ import { markdownToHtml } from "@docmost/editor-ext"; export const MarkdownClipboard = Extension.create({ name: "markdownClipboard", - priority: 50, + priority: 101, addOptions() { return { @@ -19,6 +19,40 @@ export const MarkdownClipboard = Extension.create({ new Plugin({ key: new PluginKey("markdownClipboard"), props: { + handlePaste: (view, event, slice) => { + if (!event.clipboardData) { + return false; + } + + if (this.editor.isActive("codeBlock")) { + return false; + } + + const text = event.clipboardData.getData("text/plain"); + const vscode = event.clipboardData.getData("vscode-editor-data"); + const vscodeData = vscode ? JSON.parse(vscode) : undefined; + const language = vscodeData?.mode; + + if (language !== "markdown") { + return false; + } + + const { tr } = view.state; + const { from, to } = view.state.selection; + + const html = markdownToHtml(text); + + const contentNodes = DOMParser.fromSchema( + this.editor.schema, + ).parseSlice(elementFromString(html), { + preserveWhitespace: true, + }); + + tr.replaceRange(from, to, contentNodes); + tr.setMeta('paste', true) + view.dispatch(tr); + return true; + }, clipboardTextParser: (text, context, plainText) => { const link = find(text, { defaultProtocol: "http", diff --git a/packages/editor-ext/src/lib/markdown/utils/marked.utils.ts b/packages/editor-ext/src/lib/markdown/utils/marked.utils.ts index c2b67db1..29482475 100644 --- a/packages/editor-ext/src/lib/markdown/utils/marked.utils.ts +++ b/packages/editor-ext/src/lib/markdown/utils/marked.utils.ts @@ -1,6 +1,6 @@ -import { marked } from 'marked'; -import { calloutExtension } from './callout.marked'; -import { mathBlockExtension } from './math-block.marked'; +import { marked } from "marked"; +import { calloutExtension } from "./callout.marked"; +import { mathBlockExtension } from "./math-block.marked"; import { mathInlineExtension } from "./math-inline.marked"; marked.use({ @@ -8,11 +8,11 @@ marked.use({ // @ts-ignore list(body: string, isOrdered: boolean, start: number) { if (isOrdered) { - const startAttr = start !== 1 ? ` start="${start}"` : ''; + const startAttr = start !== 1 ? ` start="${start}"` : ""; return `