Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: fix pasting html links with titles that look like links

Scott-willf6e8dc0a8874
diff --git a/packages/editor/src/extensions/link/link.ts b/packages/editor/src/extensions/link/link.ts
index dbf1859c7..5211c9e16 100644
--- a/packages/editor/src/extensions/link/link.ts
+++ b/packages/editor/src/extensions/link/link.ts
@@ -232,9 +232,10 @@ export const Link = Mark.create<LinkOptions>({
}
}),
markPasteRule({
- find: (text) => {
+ find: (text, ev) => {
const foundLinks: PasteRuleMatch[] = [];
-
+ const html = ev?.clipboardData?.getData("text/html");
+ if (html && html.includes("<a")) return [];
if (text) {
const links = find(text).filter((item) => item.isLink);
diff --git a/packages/editor/src/extensions/link/tests/__snapshots__/link.test.ts.snap b/packages/editor/src/extensions/link/tests/__snapshots__/link.test.ts.snap
index e925b288e..ac550f0a7 100644
--- a/packages/editor/src/extensions/link/tests/__snapshots__/link.test.ts.snap
+++ b/packages/editor/src/extensions/link/tests/__snapshots__/link.test.ts.snap
@@ -1,5 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+exports[`paste text > with html link 1`] = `"<div><div contenteditable="true" translate="no" class="tiptap ProseMirror" tabindex="0"><p><a target="_blank" rel="noopener noreferrer nofollow" spellcheck="false" href="nn://note/68798972093c8c6ea22efca2">example.py</a></p></div></div>"`;
+
exports[`paste text > with markdown link 1`] = `"<div><div contenteditable="true" translate="no" class="tiptap ProseMirror" tabindex="0"><p><a target="_blank" rel="noopener noreferrer nofollow" spellcheck="false" href="example.com">test</a></p></div></div>"`;
exports[`paste text > with multiple markdown links 1`] = `"<div><div contenteditable="true" translate="no" class="tiptap ProseMirror" tabindex="0"><p><a target="_blank" rel="noopener noreferrer nofollow" spellcheck="false" href="example.com">test</a> some text <a target="_blank" rel="noopener noreferrer nofollow" spellcheck="false" href="example2.com">test2</a></p></div></div>"`;
diff --git a/packages/editor/src/extensions/link/tests/link.test.ts b/packages/editor/src/extensions/link/tests/link.test.ts
index d198b7697..449ba2d99 100644
--- a/packages/editor/src/extensions/link/tests/link.test.ts
+++ b/packages/editor/src/extensions/link/tests/link.test.ts
@@ -114,4 +114,33 @@ describe("paste text", () => {
expect(editorElement.outerHTML).toMatchSnapshot();
});
+
+ test("with html link", async () => {
+ const editorElement = h("div");
+ const { editor } = createEditor({
+ element: editorElement,
+ extensions: {
+ link: Link
+ }
+ });
+
+ const clipboardEvent = new Event("paste", {
+ bubbles: true,
+ cancelable: true,
+ composed: true
+ });
+
+ (clipboardEvent as unknown as any)["clipboardData"] = {
+ getData: (type: string) =>
+ type === "text/html"
+ ? `<meta charset='utf-8'><html><head></head><body><a href="nn://note/68798972093c8c6ea22efca2">example.py</a></body></html>`
+ : ""
+ };
+
+ editor.view.dom.dispatchEvent(clipboardEvent);
+
+ await new Promise((resolve) => setTimeout(resolve, 100));
+
+ expect(editorElement.outerHTML).toMatchSnapshot();
+ });
});