From 7b6bd3db9d9afef99dd7b0deeb7b07f48c45979d Mon Sep 17 00:00:00 2001 From: Armando Guarino Date: Wed, 6 Nov 2024 16:11:30 +0100 Subject: [PATCH 1/2] docs: add validate and shouldAutoLink options to Link extension settings --- src/content/editor/extensions/marks/link.mdx | 37 +++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/content/editor/extensions/marks/link.mdx b/src/content/editor/extensions/marks/link.mdx index 1ee145c..41e4d85 100644 --- a/src/content/editor/extensions/marks/link.mdx +++ b/src/content/editor/extensions/marks/link.mdx @@ -146,14 +146,43 @@ Link.configure({ ### validate -A function that validates every autolinked link. If it exists, it will be called with the link href as argument. If it returns `false`, the link will be removed. +A function that allows customization of link validation, modifying the default verification logic. This function accepts the URL and a context object with additional properties. -Can be used to set rules for example excluding or including certain domains, tlds, etc. +**Parameters:** + +- `url`: The URL to validate. +- `ctx`: An object containing: + - `defaultValidate`: A function that performs the default URL validation. + - `protocols`: An array of allowed protocols for the URL, e.g., `["http", "https"]`. + - `defaultProtocol`: The default protocol (e.g., `'http'`). + +**Returns:** `boolean` - `true` if the URL is valid, `false` otherwise. + +This function enables you to enforce rules on allowed protocols or domains when autolinking URLs. + +```js +// Validate URLs to only accept specific protocols +Link.configure({ + validate: (url, ctx) => ctx.defaultValidate(url) && ctx.protocols.includes('https'), +}) +``` + +### shouldAutoLink + +Defines whether a valid link should be automatically linked within the editor content. + +**Parameters:** + +- `url`: The URL that has already passed validation. + +**Returns:** `boolean` - `true` if the link should be auto-linked, `false` if it should not. + +Use this function to control autolinking behavior based on the URL. ```js -// only autolink urls with a protocol +// Example: Auto-link only if the URL is secure Link.configure({ - validate: (href) => /^https?:\/\//.test(href), + shouldAutoLink: (url) => url.startsWith('https://'), }) ``` From 9edd9cfab38b0e339236b76b4b8ee7f7b9d6f121 Mon Sep 17 00:00:00 2001 From: Armando Guarino Date: Thu, 7 Nov 2024 09:49:23 +0100 Subject: [PATCH 2/2] docs: adjust example --- src/content/editor/extensions/marks/link.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/editor/extensions/marks/link.mdx b/src/content/editor/extensions/marks/link.mdx index 41e4d85..b5552a0 100644 --- a/src/content/editor/extensions/marks/link.mdx +++ b/src/content/editor/extensions/marks/link.mdx @@ -163,7 +163,7 @@ This function enables you to enforce rules on allowed protocols or domains when ```js // Validate URLs to only accept specific protocols Link.configure({ - validate: (url, ctx) => ctx.defaultValidate(url) && ctx.protocols.includes('https'), + validate: (url, ctx) => ctx.defaultValidate(url) && !url.startsWith('./'), }) ```