From f6e8dc0a88745dd3f01966a92fed74d8fa8061f8 Mon Sep 17 00:00:00 2001 From: Scott-will <48844810+Scott-will@users.noreply.github.com> Date: Mon, 21 Jul 2025 22:12:16 -0700 Subject: [PATCH] editor: fix pasting html links with titles that look like links Signed-off-by: Scott Williams scottwill1999@gmail.com editor: update tests for html handling Signed-off-by: Scott Williams scottwill1999@gmail.com editor: add logic for markdown links Signed-off-by: Scott Williams scottwill1999@gmail.com editor: remove plugin in favour of markPasteRule Signed-off-by: Scott Williams scottwill1999@gmail.com --- packages/editor/src/extensions/link/link.ts | 5 ++-- .../tests/__snapshots__/link.test.ts.snap | 2 ++ .../src/extensions/link/tests/link.test.ts | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) 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({ } }), markPasteRule({ - find: (text) => { + find: (text, ev) => { const foundLinks: PasteRuleMatch[] = []; - + const html = ev?.clipboardData?.getData("text/html"); + if (html && html.includes(" 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`] = `"
"`; + exports[`paste text > with markdown link 1`] = `"
"`; exports[`paste text > with multiple markdown links 1`] = `"

test some text test2

"`; 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" + ? `example.py` + : "" + }; + + editor.view.dom.dispatchEvent(clipboardEvent); + + await new Promise((resolve) => setTimeout(resolve, 100)); + + expect(editorElement.outerHTML).toMatchSnapshot(); + }); });