Add support for custom protocols in extension-link (#2832)

This commit is contained in:
Shengchen Zhang
2022-06-06 23:33:45 +08:00
committed by GitHub
parent d85444c310
commit 50083f3be1
2 changed files with 23 additions and 1 deletions

View File

@@ -20,6 +20,17 @@ npm install @tiptap/extension-link
## Settings ## Settings
### protocols
Additional custom protocols you would like to be recognized as links.
Default: `[]`
```js
Link.configure({
protocols: ['ftp', 'mailto'],
})
```
### autolink ### autolink
If enabled, it adds links as you type. If enabled, it adds links as you type.

View File

@@ -1,4 +1,4 @@
import { find } from 'linkifyjs' import { find, registerCustomProtocol } from 'linkifyjs'
import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core' import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'
@@ -11,6 +11,10 @@ export interface LinkOptions {
* If enabled, it adds links as you type. * If enabled, it adds links as you type.
*/ */
autolink: boolean, autolink: boolean,
/**
* An array of custom protocols to be registered with linkifyjs.
*/
protocols: Array<string>,
/** /**
* If enabled, links will be opened on click. * If enabled, links will be opened on click.
*/ */
@@ -57,6 +61,12 @@ export const Link = Mark.create<LinkOptions>({
keepOnSplit: false, keepOnSplit: false,
onCreate() {
for (const protocol of this.options.protocols) {
registerCustomProtocol(protocol)
}
},
inclusive() { inclusive() {
return this.options.autolink return this.options.autolink
}, },
@@ -66,6 +76,7 @@ export const Link = Mark.create<LinkOptions>({
openOnClick: true, openOnClick: true,
linkOnPaste: true, linkOnPaste: true,
autolink: true, autolink: true,
protocols: [],
HTMLAttributes: { HTMLAttributes: {
target: '_blank', target: '_blank',
rel: 'noopener noreferrer nofollow', rel: 'noopener noreferrer nofollow',