Skip to content

Commit

Permalink
feat(richtext-lexical): link node: disable client-side link validatio…
Browse files Browse the repository at this point in the history
…n. This allows overriding validation behavior by providing your own url field to the Link feature.

Allows you to, for example, allow anchor nodes to be inputted as URL by overriding validation.
  • Loading branch information
AlessioGr committed May 1, 2024
1 parent 10b99ce commit 01f38c4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ export function LinkPlugin(): null {
editor.registerCommand(
TOGGLE_LINK_COMMAND,
(payload: LinkPayload) => {
// validate
if (payload?.fields.linkType === 'custom') {
if (!(validateUrl === undefined || validateUrl(payload?.fields.url))) {
return false
}
}

toggleLink(payload)
return true
},
Expand Down
2 changes: 2 additions & 0 deletions packages/richtext-lexical/src/field/lexical/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function sanitizeUrl(url: string): string {
const urlRegExp =
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z\d.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z\d.-]+)((?:\/[+~%/.\w-]*)?\??[-+=&;%@.\w]*#?\w*)?)/

// Do not keep validateUrl function too loose. This is run when pasting in text, to determine if links are in that text and if it should create AutoLinkNodes.
// This is why we do not allow stuff like anchors here, as we don't want copied anchors to be turned into AutoLinkNodes.
export function validateUrl(url: string): boolean {
// TODO Fix UI for link insertion; it should never default to an invalid URL such as https://.
// Maybe show a dialog where they user can type the URL before inserting it.
Expand Down

0 comments on commit 01f38c4

Please sign in to comment.