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

Add support for custom protocols in extension-link #2832

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions docs/api/marks/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ npm install @tiptap/extension-link

## Settings

### protocols
Additional custom protocols you would like to be recognized as links.

Default: `[]`

```js
Link.configure({
protocols: ['ftp', 'mailto'],
})
```

### autolink
If enabled, it adds links as you type.

Expand Down
13 changes: 12 additions & 1 deletion packages/extension-link/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find } from 'linkifyjs'
import { find, registerCustomProtocol } from 'linkifyjs'

import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'

Expand All @@ -11,6 +11,10 @@ export interface LinkOptions {
* If enabled, it adds links as you type.
*/
autolink: boolean,
/**
* An array of custom protocols to be registered with linkifyjs.
*/
protocols: Array<string>,
/**
* If enabled, links will be opened on click.
*/
Expand Down Expand Up @@ -57,6 +61,12 @@ export const Link = Mark.create<LinkOptions>({

keepOnSplit: false,

onCreate() {
for (const protocol of this.options.protocols) {
registerCustomProtocol(protocol)
}
},

inclusive() {
return this.options.autolink
},
Expand All @@ -66,6 +76,7 @@ export const Link = Mark.create<LinkOptions>({
openOnClick: true,
linkOnPaste: true,
autolink: true,
protocols: [],
HTMLAttributes: {
target: '_blank',
rel: 'noopener noreferrer nofollow',
Expand Down