@rtdui/editor
Version:
React rich text editor based on tiptap
31 lines (28 loc) • 1.07 kB
JavaScript
import markdownItKatex from "./markdown-it-katex.mjs";
import { Extension, elementFromString } from "@tiptap/core";
import { Plugin, PluginKey } from "@tiptap/pm/state";
import MarkdownIt from "markdown-it";
import { DOMParser } from "@tiptap/pm/model";
//#region packages/editor/src/RichTextEditor/tiptap_extensions/extension-markdown-paste/markdownPaste.ts
const MarkdownPaste = Extension.create({
name: "markdownPaste",
addProseMirrorPlugins() {
return [new Plugin({
key: new PluginKey("markdownPaste"),
props: { handlePaste: (view, event) => {
if (view.state.selection.$head.parent.type.name !== "paragraph") return false;
const body = elementFromString(new MarkdownIt({
html: false,
highlight: (str, lang) => "",
linkify: true,
breaks: true
}).use(markdownItKatex).render(event.clipboardData?.getData("text") || ""));
const node = DOMParser.fromSchema(this.editor.schema).parse(body);
this.editor.commands.insertContent(node.toJSON());
return true;
} }
})];
}
});
//#endregion
export { MarkdownPaste };