@tiptap/extension-link 2.11.6 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,417 +1,383 @@
1
- import { combineTransactionSteps, getChangedRanges, findChildrenInRange, getMarksBetween, getAttributes, Mark, mergeAttributes, markPasteRule } from '@tiptap/core';
2
- import { tokenize, find, registerCustomProtocol, reset } from 'linkifyjs';
3
- import { Plugin, PluginKey } from '@tiptap/pm/state';
1
+ // src/link.ts
2
+ import { Mark, markPasteRule, mergeAttributes } from "@tiptap/core";
3
+ import { find as find2, registerCustomProtocol, reset } from "linkifyjs";
4
4
 
5
- /**
6
- * Check if the provided tokens form a valid link structure, which can either be a single link token
7
- * or a link token surrounded by parentheses or square brackets.
8
- *
9
- * This ensures that only complete and valid text is hyperlinked, preventing cases where a valid
10
- * top-level domain (TLD) is immediately followed by an invalid character, like a number. For
11
- * example, with the `find` method from Linkify, entering `example.com1` would result in
12
- * `example.com` being linked and the trailing `1` left as plain text. By using the `tokenize`
13
- * method, we can perform more comprehensive validation on the input text.
14
- */
5
+ // src/helpers/autolink.ts
6
+ import { combineTransactionSteps, findChildrenInRange, getChangedRanges, getMarksBetween } from "@tiptap/core";
7
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
8
+ import { tokenize } from "linkifyjs";
15
9
  function isValidLinkStructure(tokens) {
16
- if (tokens.length === 1) {
17
- return tokens[0].isLink;
18
- }
19
- if (tokens.length === 3 && tokens[1].isLink) {
20
- return ['()', '[]'].includes(tokens[0].value + tokens[2].value);
21
- }
22
- return false;
10
+ if (tokens.length === 1) {
11
+ return tokens[0].isLink;
12
+ }
13
+ if (tokens.length === 3 && tokens[1].isLink) {
14
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
15
+ }
16
+ return false;
23
17
  }
24
- /**
25
- * This plugin allows you to automatically add links to your editor.
26
- * @param options The plugin options
27
- * @returns The plugin instance
28
- */
29
18
  function autolink(options) {
30
- return new Plugin({
31
- key: new PluginKey('autolink'),
32
- appendTransaction: (transactions, oldState, newState) => {
33
- /**
34
- * Does the transaction change the document?
35
- */
36
- const docChanges = transactions.some(transaction => transaction.docChanged) && !oldState.doc.eq(newState.doc);
37
- /**
38
- * Prevent autolink if the transaction is not a document change or if the transaction has the meta `preventAutolink`.
39
- */
40
- const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'));
41
- /**
42
- * Prevent autolink if the transaction is not a document change
43
- * or if the transaction has the meta `preventAutolink`.
44
- */
45
- if (!docChanges || preventAutolink) {
46
- return;
19
+ return new Plugin({
20
+ key: new PluginKey("autolink"),
21
+ appendTransaction: (transactions, oldState, newState) => {
22
+ const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
23
+ const preventAutolink = transactions.some((transaction) => transaction.getMeta("preventAutolink"));
24
+ if (!docChanges || preventAutolink) {
25
+ return;
26
+ }
27
+ const { tr } = newState;
28
+ const transform = combineTransactionSteps(oldState.doc, [...transactions]);
29
+ const changes = getChangedRanges(transform);
30
+ changes.forEach(({ newRange }) => {
31
+ const nodesInChangedRanges = findChildrenInRange(newState.doc, newRange, (node) => node.isTextblock);
32
+ let textBlock;
33
+ let textBeforeWhitespace;
34
+ if (nodesInChangedRanges.length > 1) {
35
+ textBlock = nodesInChangedRanges[0];
36
+ textBeforeWhitespace = newState.doc.textBetween(
37
+ textBlock.pos,
38
+ textBlock.pos + textBlock.node.nodeSize,
39
+ void 0,
40
+ " "
41
+ );
42
+ } else if (nodesInChangedRanges.length && // We want to make sure to include the block seperator argument to treat hard breaks like spaces.
43
+ newState.doc.textBetween(newRange.from, newRange.to, " ", " ").endsWith(" ")) {
44
+ textBlock = nodesInChangedRanges[0];
45
+ textBeforeWhitespace = newState.doc.textBetween(textBlock.pos, newRange.to, void 0, " ");
46
+ }
47
+ if (textBlock && textBeforeWhitespace) {
48
+ const wordsBeforeWhitespace = textBeforeWhitespace.split(" ").filter((s) => s !== "");
49
+ if (wordsBeforeWhitespace.length <= 0) {
50
+ return false;
51
+ }
52
+ const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
53
+ const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
54
+ if (!lastWordBeforeSpace) {
55
+ return false;
56
+ }
57
+ const linksBeforeSpace = tokenize(lastWordBeforeSpace).map((t) => t.toObject(options.defaultProtocol));
58
+ if (!isValidLinkStructure(linksBeforeSpace)) {
59
+ return false;
60
+ }
61
+ linksBeforeSpace.filter((link) => link.isLink).map((link) => ({
62
+ ...link,
63
+ from: lastWordAndBlockOffset + link.start + 1,
64
+ to: lastWordAndBlockOffset + link.end + 1
65
+ })).filter((link) => {
66
+ if (!newState.schema.marks.code) {
67
+ return true;
47
68
  }
48
- const { tr } = newState;
49
- const transform = combineTransactionSteps(oldState.doc, [...transactions]);
50
- const changes = getChangedRanges(transform);
51
- changes.forEach(({ newRange }) => {
52
- // Now let’s see if we can add new links.
53
- const nodesInChangedRanges = findChildrenInRange(newState.doc, newRange, node => node.isTextblock);
54
- let textBlock;
55
- let textBeforeWhitespace;
56
- if (nodesInChangedRanges.length > 1) {
57
- // Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
58
- textBlock = nodesInChangedRanges[0];
59
- textBeforeWhitespace = newState.doc.textBetween(textBlock.pos, textBlock.pos + textBlock.node.nodeSize, undefined, ' ');
60
- }
61
- else if (nodesInChangedRanges.length
62
- // We want to make sure to include the block seperator argument to treat hard breaks like spaces.
63
- && newState.doc.textBetween(newRange.from, newRange.to, ' ', ' ').endsWith(' ')) {
64
- textBlock = nodesInChangedRanges[0];
65
- textBeforeWhitespace = newState.doc.textBetween(textBlock.pos, newRange.to, undefined, ' ');
66
- }
67
- if (textBlock && textBeforeWhitespace) {
68
- const wordsBeforeWhitespace = textBeforeWhitespace.split(' ').filter(s => s !== '');
69
- if (wordsBeforeWhitespace.length <= 0) {
70
- return false;
71
- }
72
- const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
73
- const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
74
- if (!lastWordBeforeSpace) {
75
- return false;
76
- }
77
- const linksBeforeSpace = tokenize(lastWordBeforeSpace).map(t => t.toObject(options.defaultProtocol));
78
- if (!isValidLinkStructure(linksBeforeSpace)) {
79
- return false;
80
- }
81
- linksBeforeSpace
82
- .filter(link => link.isLink)
83
- // Calculate link position.
84
- .map(link => ({
85
- ...link,
86
- from: lastWordAndBlockOffset + link.start + 1,
87
- to: lastWordAndBlockOffset + link.end + 1,
88
- }))
89
- // ignore link inside code mark
90
- .filter(link => {
91
- if (!newState.schema.marks.code) {
92
- return true;
93
- }
94
- return !newState.doc.rangeHasMark(link.from, link.to, newState.schema.marks.code);
95
- })
96
- // validate link
97
- .filter(link => options.validate(link.value))
98
- // check whether should autolink
99
- .filter(link => options.shouldAutoLink(link.value))
100
- // Add link mark.
101
- .forEach(link => {
102
- if (getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) {
103
- return;
104
- }
105
- tr.addMark(link.from, link.to, options.type.create({
106
- href: link.href,
107
- }));
108
- });
109
- }
110
- });
111
- if (!tr.steps.length) {
112
- return;
69
+ return !newState.doc.rangeHasMark(link.from, link.to, newState.schema.marks.code);
70
+ }).filter((link) => options.validate(link.value)).filter((link) => options.shouldAutoLink(link.value)).forEach((link) => {
71
+ if (getMarksBetween(link.from, link.to, newState.doc).some((item) => item.mark.type === options.type)) {
72
+ return;
113
73
  }
114
- return tr;
115
- },
116
- });
74
+ tr.addMark(
75
+ link.from,
76
+ link.to,
77
+ options.type.create({
78
+ href: link.href
79
+ })
80
+ );
81
+ });
82
+ }
83
+ });
84
+ if (!tr.steps.length) {
85
+ return;
86
+ }
87
+ return tr;
88
+ }
89
+ });
117
90
  }
118
91
 
92
+ // src/helpers/clickHandler.ts
93
+ import { getAttributes } from "@tiptap/core";
94
+ import { Plugin as Plugin2, PluginKey as PluginKey2 } from "@tiptap/pm/state";
119
95
  function clickHandler(options) {
120
- return new Plugin({
121
- key: new PluginKey('handleClickLink'),
122
- props: {
123
- handleClick: (view, pos, event) => {
124
- var _a, _b;
125
- if (event.button !== 0) {
126
- return false;
127
- }
128
- if (!view.editable) {
129
- return false;
130
- }
131
- let a = event.target;
132
- const els = [];
133
- while (a.nodeName !== 'DIV') {
134
- els.push(a);
135
- a = a.parentNode;
136
- }
137
- if (!els.find(value => value.nodeName === 'A')) {
138
- return false;
139
- }
140
- const attrs = getAttributes(view.state, options.type.name);
141
- const link = event.target;
142
- const href = (_a = link === null || link === void 0 ? void 0 : link.href) !== null && _a !== void 0 ? _a : attrs.href;
143
- const target = (_b = link === null || link === void 0 ? void 0 : link.target) !== null && _b !== void 0 ? _b : attrs.target;
144
- if (link && href) {
145
- window.open(href, target);
146
- return true;
147
- }
148
- return false;
149
- },
150
- },
151
- });
96
+ return new Plugin2({
97
+ key: new PluginKey2("handleClickLink"),
98
+ props: {
99
+ handleClick: (view, pos, event) => {
100
+ var _a, _b;
101
+ if (event.button !== 0) {
102
+ return false;
103
+ }
104
+ if (!view.editable) {
105
+ return false;
106
+ }
107
+ let a = event.target;
108
+ const els = [];
109
+ while (a.nodeName !== "DIV") {
110
+ els.push(a);
111
+ a = a.parentNode;
112
+ }
113
+ if (!els.find((value) => value.nodeName === "A")) {
114
+ return false;
115
+ }
116
+ const attrs = getAttributes(view.state, options.type.name);
117
+ const link = event.target;
118
+ const href = (_a = link == null ? void 0 : link.href) != null ? _a : attrs.href;
119
+ const target = (_b = link == null ? void 0 : link.target) != null ? _b : attrs.target;
120
+ if (link && href) {
121
+ window.open(href, target);
122
+ return true;
123
+ }
124
+ return false;
125
+ }
126
+ }
127
+ });
152
128
  }
153
129
 
130
+ // src/helpers/pasteHandler.ts
131
+ import { Plugin as Plugin3, PluginKey as PluginKey3 } from "@tiptap/pm/state";
132
+ import { find } from "linkifyjs";
154
133
  function pasteHandler(options) {
155
- return new Plugin({
156
- key: new PluginKey('handlePasteLink'),
157
- props: {
158
- handlePaste: (view, event, slice) => {
159
- const { state } = view;
160
- const { selection } = state;
161
- const { empty } = selection;
162
- if (empty) {
163
- return false;
164
- }
165
- let textContent = '';
166
- slice.content.forEach(node => {
167
- textContent += node.textContent;
168
- });
169
- const link = find(textContent, { defaultProtocol: options.defaultProtocol }).find(item => item.isLink && item.value === textContent);
170
- if (!textContent || !link) {
171
- return false;
172
- }
173
- return options.editor.commands.setMark(options.type, {
174
- href: link.href,
175
- });
176
- },
177
- },
178
- });
134
+ return new Plugin3({
135
+ key: new PluginKey3("handlePasteLink"),
136
+ props: {
137
+ handlePaste: (view, event, slice) => {
138
+ const { state } = view;
139
+ const { selection } = state;
140
+ const { empty } = selection;
141
+ if (empty) {
142
+ return false;
143
+ }
144
+ let textContent = "";
145
+ slice.content.forEach((node) => {
146
+ textContent += node.textContent;
147
+ });
148
+ const link = find(textContent, { defaultProtocol: options.defaultProtocol }).find(
149
+ (item) => item.isLink && item.value === textContent
150
+ );
151
+ if (!textContent || !link) {
152
+ return false;
153
+ }
154
+ return options.editor.commands.setMark(options.type, {
155
+ href: link.href
156
+ });
157
+ }
158
+ }
159
+ });
179
160
  }
180
161
 
181
- const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
182
- // From DOMPurify
183
- // https://github.com/cure53/DOMPurify/blob/main/src/regexp.js
184
- // eslint-disable-next-line no-control-regex
185
- const ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
162
+ // src/link.ts
163
+ var pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
164
+ var ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
186
165
  function isAllowedUri(uri, protocols) {
187
- const allowedProtocols = [
188
- 'http',
189
- 'https',
190
- 'ftp',
191
- 'ftps',
192
- 'mailto',
193
- 'tel',
194
- 'callto',
195
- 'sms',
196
- 'cid',
197
- 'xmpp',
198
- ];
199
- if (protocols) {
200
- protocols.forEach(protocol => {
201
- const nextProtocol = typeof protocol === 'string' ? protocol : protocol.scheme;
202
- if (nextProtocol) {
203
- allowedProtocols.push(nextProtocol);
204
- }
205
- });
206
- }
207
- return (!uri
208
- || uri
209
- .replace(ATTR_WHITESPACE, '')
210
- .match(new RegExp(
211
- // eslint-disable-next-line no-useless-escape
212
- `^(?:(?:${allowedProtocols.join('|')}):|[^a-z]|[a-z0-9+.\-]+(?:[^a-z+.\-:]|$))`, 'i')));
166
+ const allowedProtocols = ["http", "https", "ftp", "ftps", "mailto", "tel", "callto", "sms", "cid", "xmpp"];
167
+ if (protocols) {
168
+ protocols.forEach((protocol) => {
169
+ const nextProtocol = typeof protocol === "string" ? protocol : protocol.scheme;
170
+ if (nextProtocol) {
171
+ allowedProtocols.push(nextProtocol);
172
+ }
173
+ });
174
+ }
175
+ return !uri || uri.replace(ATTR_WHITESPACE, "").match(
176
+ new RegExp(
177
+ // eslint-disable-next-line no-useless-escape
178
+ `^(?:(?:${allowedProtocols.join("|")}):|[^a-z]|[a-z0-9+.-]+(?:[^a-z+.-:]|$))`,
179
+ "i"
180
+ )
181
+ );
213
182
  }
214
- /**
215
- * This extension allows you to create links.
216
- * @see https://www.tiptap.dev/api/marks/link
217
- */
218
- const Link = Mark.create({
219
- name: 'link',
220
- priority: 1000,
221
- keepOnSplit: false,
222
- exitable: true,
223
- onCreate() {
224
- if (this.options.validate && !this.options.shouldAutoLink) {
225
- // Copy the validate function to the shouldAutoLink option
226
- this.options.shouldAutoLink = this.options.validate;
227
- console.warn('The `validate` option is deprecated. Rename to the `shouldAutoLink` option instead.');
183
+ var Link = Mark.create({
184
+ name: "link",
185
+ priority: 1e3,
186
+ keepOnSplit: false,
187
+ exitable: true,
188
+ onCreate() {
189
+ if (this.options.validate && !this.options.shouldAutoLink) {
190
+ this.options.shouldAutoLink = this.options.validate;
191
+ console.warn("The `validate` option is deprecated. Rename to the `shouldAutoLink` option instead.");
192
+ }
193
+ this.options.protocols.forEach((protocol) => {
194
+ if (typeof protocol === "string") {
195
+ registerCustomProtocol(protocol);
196
+ return;
197
+ }
198
+ registerCustomProtocol(protocol.scheme, protocol.optionalSlashes);
199
+ });
200
+ },
201
+ onDestroy() {
202
+ reset();
203
+ },
204
+ inclusive() {
205
+ return this.options.autolink;
206
+ },
207
+ addOptions() {
208
+ return {
209
+ openOnClick: true,
210
+ linkOnPaste: true,
211
+ autolink: true,
212
+ protocols: [],
213
+ defaultProtocol: "http",
214
+ HTMLAttributes: {
215
+ target: "_blank",
216
+ rel: "noopener noreferrer nofollow",
217
+ class: null
218
+ },
219
+ isAllowedUri: (url, ctx) => !!isAllowedUri(url, ctx.protocols),
220
+ validate: (url) => !!url,
221
+ shouldAutoLink: (url) => !!url
222
+ };
223
+ },
224
+ addAttributes() {
225
+ return {
226
+ href: {
227
+ default: null,
228
+ parseHTML(element) {
229
+ return element.getAttribute("href");
228
230
  }
229
- this.options.protocols.forEach(protocol => {
230
- if (typeof protocol === 'string') {
231
- registerCustomProtocol(protocol);
232
- return;
233
- }
234
- registerCustomProtocol(protocol.scheme, protocol.optionalSlashes);
235
- });
236
- },
237
- onDestroy() {
238
- reset();
239
- },
240
- inclusive() {
241
- return this.options.autolink;
242
- },
243
- addOptions() {
244
- return {
245
- openOnClick: true,
246
- linkOnPaste: true,
247
- autolink: true,
248
- protocols: [],
249
- defaultProtocol: 'http',
250
- HTMLAttributes: {
251
- target: '_blank',
252
- rel: 'noopener noreferrer nofollow',
253
- class: null,
254
- },
255
- isAllowedUri: (url, ctx) => !!isAllowedUri(url, ctx.protocols),
256
- validate: url => !!url,
257
- shouldAutoLink: url => !!url,
258
- };
259
- },
260
- addAttributes() {
261
- return {
262
- href: {
263
- default: null,
264
- parseHTML(element) {
265
- return element.getAttribute('href');
266
- },
267
- },
268
- target: {
269
- default: this.options.HTMLAttributes.target,
270
- },
271
- rel: {
272
- default: this.options.HTMLAttributes.rel,
273
- },
274
- class: {
275
- default: this.options.HTMLAttributes.class,
276
- },
277
- };
278
- },
279
- parseHTML() {
280
- return [
281
- {
282
- tag: 'a[href]',
283
- getAttrs: dom => {
284
- const href = dom.getAttribute('href');
285
- // prevent XSS attacks
286
- if (!href
287
- || !this.options.isAllowedUri(href, {
288
- defaultValidate: url => !!isAllowedUri(url, this.options.protocols),
289
- protocols: this.options.protocols,
290
- defaultProtocol: this.options.defaultProtocol,
291
- })) {
292
- return false;
293
- }
294
- return null;
295
- },
296
- },
297
- ];
298
- },
299
- renderHTML({ HTMLAttributes }) {
300
- // prevent XSS attacks
301
- if (!this.options.isAllowedUri(HTMLAttributes.href, {
302
- defaultValidate: href => !!isAllowedUri(href, this.options.protocols),
231
+ },
232
+ target: {
233
+ default: this.options.HTMLAttributes.target
234
+ },
235
+ rel: {
236
+ default: this.options.HTMLAttributes.rel
237
+ },
238
+ class: {
239
+ default: this.options.HTMLAttributes.class
240
+ }
241
+ };
242
+ },
243
+ parseHTML() {
244
+ return [
245
+ {
246
+ tag: "a[href]",
247
+ getAttrs: (dom) => {
248
+ const href = dom.getAttribute("href");
249
+ if (!href || !this.options.isAllowedUri(href, {
250
+ defaultValidate: (url) => !!isAllowedUri(url, this.options.protocols),
303
251
  protocols: this.options.protocols,
304
- defaultProtocol: this.options.defaultProtocol,
305
- })) {
306
- // strip out the href
307
- return [
308
- 'a',
309
- mergeAttributes(this.options.HTMLAttributes, { ...HTMLAttributes, href: '' }),
310
- 0,
311
- ];
252
+ defaultProtocol: this.options.defaultProtocol
253
+ })) {
254
+ return false;
255
+ }
256
+ return null;
312
257
  }
313
- return ['a', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
314
- },
315
- addCommands() {
316
- return {
317
- setLink: attributes => ({ chain }) => {
318
- const { href } = attributes;
319
- if (!this.options.isAllowedUri(href, {
320
- defaultValidate: url => !!isAllowedUri(url, this.options.protocols),
321
- protocols: this.options.protocols,
322
- defaultProtocol: this.options.defaultProtocol,
323
- })) {
324
- return false;
325
- }
326
- return chain().setMark(this.name, attributes).setMeta('preventAutolink', true).run();
327
- },
328
- toggleLink: attributes => ({ chain }) => {
329
- const { href } = attributes;
330
- if (!this.options.isAllowedUri(href, {
331
- defaultValidate: url => !!isAllowedUri(url, this.options.protocols),
332
- protocols: this.options.protocols,
333
- defaultProtocol: this.options.defaultProtocol,
334
- })) {
335
- return false;
336
- }
337
- return chain()
338
- .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
339
- .setMeta('preventAutolink', true)
340
- .run();
341
- },
342
- unsetLink: () => ({ chain }) => {
343
- return chain()
344
- .unsetMark(this.name, { extendEmptyMarkRange: true })
345
- .setMeta('preventAutolink', true)
346
- .run();
347
- },
348
- };
349
- },
350
- addPasteRules() {
351
- return [
352
- markPasteRule({
353
- find: text => {
354
- const foundLinks = [];
355
- if (text) {
356
- const { protocols, defaultProtocol } = this.options;
357
- const links = find(text).filter(item => item.isLink
358
- && this.options.isAllowedUri(item.value, {
359
- defaultValidate: href => !!isAllowedUri(href, protocols),
360
- protocols,
361
- defaultProtocol,
362
- }));
363
- if (links.length) {
364
- links.forEach(link => foundLinks.push({
365
- text: link.value,
366
- data: {
367
- href: link.href,
368
- },
369
- index: link.start,
370
- }));
371
- }
372
- }
373
- return foundLinks;
374
- },
375
- type: this.type,
376
- getAttributes: match => {
377
- var _a;
378
- return {
379
- href: (_a = match.data) === null || _a === void 0 ? void 0 : _a.href,
380
- };
381
- },
382
- }),
383
- ];
384
- },
385
- addProseMirrorPlugins() {
386
- const plugins = [];
387
- const { protocols, defaultProtocol } = this.options;
388
- if (this.options.autolink) {
389
- plugins.push(autolink({
390
- type: this.type,
391
- defaultProtocol: this.options.defaultProtocol,
392
- validate: url => this.options.isAllowedUri(url, {
393
- defaultValidate: href => !!isAllowedUri(href, protocols),
394
- protocols,
395
- defaultProtocol,
396
- }),
397
- shouldAutoLink: this.options.shouldAutoLink,
398
- }));
258
+ }
259
+ ];
260
+ },
261
+ renderHTML({ HTMLAttributes }) {
262
+ if (!this.options.isAllowedUri(HTMLAttributes.href, {
263
+ defaultValidate: (href) => !!isAllowedUri(href, this.options.protocols),
264
+ protocols: this.options.protocols,
265
+ defaultProtocol: this.options.defaultProtocol
266
+ })) {
267
+ return ["a", mergeAttributes(this.options.HTMLAttributes, { ...HTMLAttributes, href: "" }), 0];
268
+ }
269
+ return ["a", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
270
+ },
271
+ addCommands() {
272
+ return {
273
+ setLink: (attributes) => ({ chain }) => {
274
+ const { href } = attributes;
275
+ if (!this.options.isAllowedUri(href, {
276
+ defaultValidate: (url) => !!isAllowedUri(url, this.options.protocols),
277
+ protocols: this.options.protocols,
278
+ defaultProtocol: this.options.defaultProtocol
279
+ })) {
280
+ return false;
399
281
  }
400
- if (this.options.openOnClick === true) {
401
- plugins.push(clickHandler({
402
- type: this.type,
403
- }));
282
+ return chain().setMark(this.name, attributes).setMeta("preventAutolink", true).run();
283
+ },
284
+ toggleLink: (attributes) => ({ chain }) => {
285
+ const { href } = attributes;
286
+ if (!this.options.isAllowedUri(href, {
287
+ defaultValidate: (url) => !!isAllowedUri(url, this.options.protocols),
288
+ protocols: this.options.protocols,
289
+ defaultProtocol: this.options.defaultProtocol
290
+ })) {
291
+ return false;
404
292
  }
405
- if (this.options.linkOnPaste) {
406
- plugins.push(pasteHandler({
407
- editor: this.editor,
408
- defaultProtocol: this.options.defaultProtocol,
409
- type: this.type,
410
- }));
293
+ return chain().toggleMark(this.name, attributes, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
294
+ },
295
+ unsetLink: () => ({ chain }) => {
296
+ return chain().unsetMark(this.name, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
297
+ }
298
+ };
299
+ },
300
+ addPasteRules() {
301
+ return [
302
+ markPasteRule({
303
+ find: (text) => {
304
+ const foundLinks = [];
305
+ if (text) {
306
+ const { protocols, defaultProtocol } = this.options;
307
+ const links = find2(text).filter(
308
+ (item) => item.isLink && this.options.isAllowedUri(item.value, {
309
+ defaultValidate: (href) => !!isAllowedUri(href, protocols),
310
+ protocols,
311
+ defaultProtocol
312
+ })
313
+ );
314
+ if (links.length) {
315
+ links.forEach(
316
+ (link) => foundLinks.push({
317
+ text: link.value,
318
+ data: {
319
+ href: link.href
320
+ },
321
+ index: link.start
322
+ })
323
+ );
324
+ }
325
+ }
326
+ return foundLinks;
327
+ },
328
+ type: this.type,
329
+ getAttributes: (match) => {
330
+ var _a;
331
+ return {
332
+ href: (_a = match.data) == null ? void 0 : _a.href
333
+ };
411
334
  }
412
- return plugins;
413
- },
335
+ })
336
+ ];
337
+ },
338
+ addProseMirrorPlugins() {
339
+ const plugins = [];
340
+ const { protocols, defaultProtocol } = this.options;
341
+ if (this.options.autolink) {
342
+ plugins.push(
343
+ autolink({
344
+ type: this.type,
345
+ defaultProtocol: this.options.defaultProtocol,
346
+ validate: (url) => this.options.isAllowedUri(url, {
347
+ defaultValidate: (href) => !!isAllowedUri(href, protocols),
348
+ protocols,
349
+ defaultProtocol
350
+ }),
351
+ shouldAutoLink: this.options.shouldAutoLink
352
+ })
353
+ );
354
+ }
355
+ if (this.options.openOnClick === true) {
356
+ plugins.push(
357
+ clickHandler({
358
+ type: this.type
359
+ })
360
+ );
361
+ }
362
+ if (this.options.linkOnPaste) {
363
+ plugins.push(
364
+ pasteHandler({
365
+ editor: this.editor,
366
+ defaultProtocol: this.options.defaultProtocol,
367
+ type: this.type
368
+ })
369
+ );
370
+ }
371
+ return plugins;
372
+ }
414
373
  });
415
374
 
416
- export { Link, Link as default, isAllowedUri, pasteRegex };
417
- //# sourceMappingURL=index.js.map
375
+ // src/index.ts
376
+ var index_default = Link;
377
+ export {
378
+ Link,
379
+ index_default as default,
380
+ isAllowedUri,
381
+ pasteRegex
382
+ };
383
+ //# sourceMappingURL=index.js.map