diff --git a/packages/extension-link/src/helpers/autolink.ts b/packages/extension-link/src/helpers/autolink.ts index 05934e6b3d..21285aa052 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 } export function autolink(options: AutolinkOptions): Plugin { @@ -126,12 +126,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 8ef67ed97a..01195b1f8d 100644 --- a/packages/extension-link/src/link.ts +++ b/packages/extension-link/src/link.ts @@ -41,7 +41,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' { @@ -99,7 +99,7 @@ export const Link = Mark.create({ rel: 'noopener noreferrer nofollow', class: null, }, - validate: undefined, + validate: url => !!url, } }, @@ -166,7 +166,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({