Skip to content

Commit

Permalink
Revert "revert: "docs: add validate and shouldAutoLink options to Lin…
Browse files Browse the repository at this point in the history
…k extens…"

This reverts commit d57a0f6.
  • Loading branch information
nperez0111 authored Nov 7, 2024
1 parent d57a0f6 commit f203dd5
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/content/editor/extensions/marks/link.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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) && !url.startsWith('./'),
})
```

### 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://'),
})
```

Expand Down

0 comments on commit f203dd5

Please sign in to comment.