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

Added optional custom uri validator in link extension #5726

Closed
wants to merge 1 commit into from

Conversation

SanderLeenders
Copy link
Contributor

Changes Overview

Added custom url validation to link extension

Implementation Approach

Added an optional custom url validation to provide full control over (in)valid uri's

Testing Done

Added test

Verification Steps

Additional Notes

Checklist

  • I have created a changeset for this PR if necessary.
  • My changes do not break the library.
  • I have added tests where applicable.
  • I have followed the project guidelines.
  • I have fixed any lint issues.

Related Issues

[Bug]: Expose an API to overwrite the default link validation

Copy link

changeset-bot bot commented Oct 14, 2024

⚠️ No Changeset found

Latest commit: 82b0816

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

netlify bot commented Oct 14, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit 82b0816
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/670ce7f53eea510008f4dcf2
😎 Deploy Preview https://deploy-preview-5726--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

* @param url - The url to be validated.
* @returns - True if the url is valid, false otherwise.
*/
customUriValidator?: (url: string | undefined) => boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, but now we have two validation options:

  • validate
  • customUriValidator

This could be confusing, especially since they don't apply to the same places. Maybe we should merge the implementation into a single validation function that is used everywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, and I have thought about it. However, there must be a reason why the validate function was not used when this change was made: https://github.com/ueberdosis/tiptap/pull/5470/files#diff-1d1f108bd30f776ec9689dc1bb00a3cb3dafc89fde595994aa8fa400471a87d6. Therefore, I have decided to keep these functionalities separate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nperez0111 What are your thoughts here? Also, would it be possible to have this one fully reviewed soon? 🙏

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I, too, would not make them be separate functions since they are too similar in name.

I can't speak to why this what not done like this here: https://github.com/ueberdosis/tiptap/pull/5470/files#diff-1d1f108bd30f776ec9689dc1bb00a3cb3dafc89fde595994aa8fa400471a87d6 but, I think that if a custom validator is passed, it is expected to be used properly.

Ultimately, I'd have:

validate: (url: string, defaultValidate: (url: string) => boolean ) => boolean

This would allow the user to specify their own validation, and if they don't want to both writing their own rules, they can use our existing defaultValidate function (which currently maps to isAllowedUri with the protocols being prefilled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they are similar in name, but they serve different purposes. The validate function is solely used for the autolink option, while the newly added one ensures that there are no malicious URLs. If these functionalities were handled by a single function, it would be impossible to differentiate between them, meaning all valid links would be autolinked. I don't think that's something you would want. For example, should relative links be autolinked? In my use case, this is something I do not want.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point with auto linking but I feel that validation should be about validity. How about we add a new callback for shouldAutoLink where you could reject a relative link separately from validation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm mistaken, but it sounds like you're suggesting a simple rename from validate: (url: string) => boolean to shouldAutoLink(validate: (url: string) => boolean), right? From what I understand, the validate method is an optional/custom method meant to override the default behavior of the autolink: boolean.

The main issue seems to be that the method’s name doesn't clearly reflect its purpose —the validate function doesn’t actually validate links, but rather checks whether a link should be auto-linked.

The customUriValidator method could be renamed to validate, but personally, I’d keep the name customUriValidator since it more clearly describes the function’s role."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make my case, take this example:

Link.configure({
 validate: () => true,
 customUriValidator: () => true
})

This does not "more clearly describes the function’s role", it makes it confusing to know which applies to auto-linking and which applies to actual validation.

The current validate function, as I understand, is used in two cases, for auto-linking and matching against pasted text when looking for links. But, we are introducing a 3rd case where it will be useful for determining what we consider to be a "valid" URL (i.e. not malicious).

You wanted to support the ability to differentiate what would be considered for auto-linking from what is considered a valid link. I offered you a way to do this, a new function which would be specifically for auto-linking behavior to choose when something would be auto-linked (assuming it was already valid).

So, this is what I'm proposing:

Link.configure({
 validate: (url: string, defaultValidate: (url: string) => boolean) => boolean
 shouldAutoLink: (url: string) => boolean
})

If a link is not valid (according to the validate function), then it is not considered as a link that can be linked to and will be dropped.
If a link is considered valid (according to the validate function), then the shouldAutoLink function will be called to allow rejecting links that we may consider valid, but do not want to auto-link to.

Does this sound reasonable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize I wasn’t entirely clear in my PR. For me, the most important thing is that TipTap can handle relative links again. I initially tried to fix this using the validate function, but I found that it was only used for autolinking. That's why I added the additional customUriValidator. I forgot to add why I was adding this.

In my previous comment, what I was trying to propose was:

Link.configure({
validate:
shouldAutoLink:
})

For now, I’m going to enjoy my vacation, and I’ll get back to this afterward.

@SanderLeenders
Copy link
Contributor Author

SanderLeenders commented Nov 7, 2024

This #5808 pull request appears to contain the suggested change, so I’m closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants