Merge pull request #8385 from Scott-will/master

Handle pasting urls in html
This commit is contained in:
Abdullah Atta
2025-08-13 13:27:14 +05:00
committed by GitHub
3 changed files with 34 additions and 2 deletions

View File

@@ -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);

View File

@@ -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>"`;

View File

@@ -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();
});
});