Skip to content

Commit

Permalink
Add support for custom protocols in extension-link (#2832)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunabanana authored Jun 6, 2022
1 parent d85444c commit 50083f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit 50083f3

Please sign in to comment.