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