fix: move markdown clipboard extension to client app

This commit is contained in:
Philipinho
2025-01-04 21:25:05 +00:00
parent f5bc99b449
commit 95715421c6
3 changed files with 3 additions and 10 deletions

View File

@ -1,2 +1 @@
export * from "./markdown-clipboard";
export * from "./utils/marked.utils";

View File

@ -1,57 +0,0 @@
// adapted from: https://github.com/aguingand/tiptap-markdown/blob/main/src/extensions/tiptap/clipboard.js - MIT
import { Extension } from "@tiptap/core";
import { Plugin, PluginKey } from "@tiptap/pm/state";
import { DOMParser } from "@tiptap/pm/model";
import { markdownToHtml } from "./utils/marked.utils";
import { find } from "linkifyjs";
export const MarkdownClipboard = Extension.create({
name: "markdownClipboard",
priority: 50,
export const MarkdownClipboard = Extension.create({
name: "markdownClipboard",
addOptions() {
return {
transformPastedText: false,
};
},
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey("markdownClipboard"),
props: {
clipboardTextParser: (text, context, plainText) => {
const link = find(text, {
defaultProtocol: "http",
}).find((item) => item.isLink && item.value === text);
if (plainText || !this.options.transformPastedText || link) {
// don't parse plaintext link to allow link paste handler to work
// pasting with shift key prevents formatting
return null;
}
const parsed = markdownToHtml(text);
return DOMParser.fromSchema(this.editor.schema).parseSlice(
elementFromString(parsed),
{
preserveWhitespace: true,
context,
},
);
},
},
}),
];
},
});
function elementFromString(value) {
// add a wrapper to preserve leading and trailing whitespace
const wrappedValue = `<body>${value}</body>`;
return new window.DOMParser().parseFromString(wrappedValue, "text/html").body;
}