Skip to content

Commit 01f38c4

Browse files
committed
feat(richtext-lexical): link node: disable client-side link validation. 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.
1 parent 10b99ce commit 01f38c4

File tree

2 files changed

+2
-7
lines changed
  • packages/richtext-lexical/src/field

2 files changed

+2
-7
lines changed

packages/richtext-lexical/src/field/features/link/plugins/link/index.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ export function LinkPlugin(): null {
2727
editor.registerCommand(
2828
TOGGLE_LINK_COMMAND,
2929
(payload: LinkPayload) => {
30-
// validate
31-
if (payload?.fields.linkType === 'custom') {
32-
if (!(validateUrl === undefined || validateUrl(payload?.fields.url))) {
33-
return false
34-
}
35-
}
36-
3730
toggleLink(payload)
3831
return true
3932
},

packages/richtext-lexical/src/field/lexical/utils/url.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export function sanitizeUrl(url: string): string {
1717
const urlRegExp =
1818
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z\d.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z\d.-]+)((?:\/[+~%/.\w-]*)?\??[-+=&;%@.\w]*#?\w*)?)/
1919

20+
// 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.
21+
// This is why we do not allow stuff like anchors here, as we don't want copied anchors to be turned into AutoLinkNodes.
2022
export function validateUrl(url: string): boolean {
2123
// TODO Fix UI for link insertion; it should never default to an invalid URL such as https://.
2224
// Maybe show a dialog where they user can type the URL before inserting it.

0 commit comments

Comments
 (0)