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

docs: add validate and shouldAutoLink options to Link extension settings #60

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading