Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure to validate pasted links #5061

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}

export function autolink(options: AutolinkOptions): Plugin {
Expand Down Expand Up @@ -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)) {
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 @@ -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' {
Expand Down Expand Up @@ -99,7 +99,7 @@ export const Link = Mark.create<LinkOptions>({
rel: 'noopener noreferrer nofollow',
class: null,
},
validate: undefined,
validate: url => !!url,
}
},

Expand Down Expand Up @@ -166,7 +166,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