From e95140c8893739c4b08d50f1561a6d4e025e1790 Mon Sep 17 00:00:00 2001 From: Nantris <6835891+Nantris@users.noreply.github.com> Date: Fri, 24 May 2024 08:02:37 -0400 Subject: [PATCH] fix: validate pasted links (#5061) --- packages/extension-link/src/helpers/autolink.ts | 9 ++------- packages/extension-link/src/link.ts | 7 ++++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/extension-link/src/helpers/autolink.ts b/packages/extension-link/src/helpers/autolink.ts index adb451891a..bb38f92b85 100644 --- a/packages/extension-link/src/helpers/autolink.ts +++ b/packages/extension-link/src/helpers/autolink.ts @@ -33,7 +33,7 @@ function isValidLinkStructure(tokens: Array>) type AutolinkOptions = { type: MarkType - validate?: (url: string) => boolean + validate: (url: string) => boolean } /** @@ -142,12 +142,7 @@ export function autolink(options: AutolinkOptions): Plugin { ) }) // validate link - .filter(link => { - if (options.validate) { - return options.validate(link.value) - } - return true - }) + .filter(link => options.validate(link.value)) // Add link mark. .forEach(link => { if (getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) { diff --git a/packages/extension-link/src/link.ts b/packages/extension-link/src/link.ts index 4aad82dd4f..bf5c809421 100644 --- a/packages/extension-link/src/link.ts +++ b/packages/extension-link/src/link.ts @@ -68,7 +68,7 @@ export interface LinkOptions { * @param url - The url to be validated. * @returns - True if the url is valid, false otherwise. */ - validate?: (url: string) => boolean + validate: (url: string) => boolean } declare module '@tiptap/core' { @@ -144,7 +144,7 @@ export const Link = Mark.create({ rel: 'noopener noreferrer nofollow', class: null, }, - validate: undefined, + validate: url => !!url, } }, @@ -222,7 +222,8 @@ export const Link = Mark.create({ const foundLinks: PasteRuleMatch[] = [] if (text) { - const links = find(text).filter(item => item.isLink) + const { validate } = this.options + const links = find(text).filter(item => item.isLink && validate(item.value)) if (links.length) { links.forEach(link => (foundLinks.push({