Skip to content

Commit

Permalink
fix: validate pasted links (#5061)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nantris committed May 24, 2024
1 parent ae14557 commit e95140c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 2 additions & 7 deletions packages/extension-link/src/helpers/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function isValidLinkStructure(tokens: Array<ReturnType<MultiToken['toObject']>>)

type AutolinkOptions = {
type: MarkType
validate?: (url: string) => boolean
validate: (url: string) => boolean
}

/**
Expand Down Expand Up @@ -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)) {
Expand Down
7 changes: 4 additions & 3 deletions packages/extension-link/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -144,7 +144,7 @@ export const Link = Mark.create<LinkOptions>({
rel: 'noopener noreferrer nofollow',
class: null,
},
validate: undefined,
validate: url => !!url,
}
},

Expand Down Expand Up @@ -222,7 +222,8 @@ export const Link = Mark.create<LinkOptions>({
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({
Expand Down

0 comments on commit e95140c

Please sign in to comment.