@tiptap/extension-link 2.6.2 → 3.0.0-next.1

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.
Files changed (174) hide show
  1. package/dist/index.cjs +338 -325
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/{packages/extension-link/src/link.d.ts → index.d.cts} +7 -5
  4. package/dist/index.d.ts +105 -0
  5. package/dist/index.js +321 -321
  6. package/dist/index.js.map +1 -1
  7. package/package.json +8 -10
  8. package/dist/index.umd.js +0 -350
  9. package/dist/index.umd.js.map +0 -1
  10. package/dist/packages/core/src/CommandManager.d.ts +0 -20
  11. package/dist/packages/core/src/Editor.d.ts +0 -163
  12. package/dist/packages/core/src/EventEmitter.d.ts +0 -11
  13. package/dist/packages/core/src/Extension.d.ts +0 -344
  14. package/dist/packages/core/src/ExtensionManager.d.ts +0 -55
  15. package/dist/packages/core/src/InputRule.d.ts +0 -42
  16. package/dist/packages/core/src/Mark.d.ts +0 -452
  17. package/dist/packages/core/src/Node.d.ts +0 -612
  18. package/dist/packages/core/src/NodePos.d.ts +0 -44
  19. package/dist/packages/core/src/NodeView.d.ts +0 -31
  20. package/dist/packages/core/src/PasteRule.d.ts +0 -50
  21. package/dist/packages/core/src/Tracker.d.ts +0 -11
  22. package/dist/packages/core/src/commands/blur.d.ts +0 -13
  23. package/dist/packages/core/src/commands/clearContent.d.ts +0 -14
  24. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -13
  25. package/dist/packages/core/src/commands/command.d.ts +0 -18
  26. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -13
  27. package/dist/packages/core/src/commands/cut.d.ts +0 -20
  28. package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +0 -13
  29. package/dist/packages/core/src/commands/deleteNode.d.ts +0 -15
  30. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -14
  31. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -13
  32. package/dist/packages/core/src/commands/enter.d.ts +0 -13
  33. package/dist/packages/core/src/commands/exitCode.d.ts +0 -13
  34. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -25
  35. package/dist/packages/core/src/commands/first.d.ts +0 -14
  36. package/dist/packages/core/src/commands/focus.d.ts +0 -27
  37. package/dist/packages/core/src/commands/forEach.d.ts +0 -14
  38. package/dist/packages/core/src/commands/index.d.ts +0 -55
  39. package/dist/packages/core/src/commands/insertContent.d.ts +0 -34
  40. package/dist/packages/core/src/commands/insertContentAt.d.ts +0 -47
  41. package/dist/packages/core/src/commands/join.d.ts +0 -41
  42. package/dist/packages/core/src/commands/joinItemBackward.d.ts +0 -13
  43. package/dist/packages/core/src/commands/joinItemForward.d.ts +0 -13
  44. package/dist/packages/core/src/commands/joinTextblockBackward.d.ts +0 -12
  45. package/dist/packages/core/src/commands/joinTextblockForward.d.ts +0 -12
  46. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -14
  47. package/dist/packages/core/src/commands/lift.d.ts +0 -17
  48. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -13
  49. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -15
  50. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -13
  51. package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -16
  52. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -13
  53. package/dist/packages/core/src/commands/selectAll.d.ts +0 -13
  54. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -13
  55. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -13
  56. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -13
  57. package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +0 -13
  58. package/dist/packages/core/src/commands/selectTextblockStart.d.ts +0 -13
  59. package/dist/packages/core/src/commands/setContent.d.ts +0 -40
  60. package/dist/packages/core/src/commands/setMark.d.ts +0 -15
  61. package/dist/packages/core/src/commands/setMeta.d.ts +0 -16
  62. package/dist/packages/core/src/commands/setNode.d.ts +0 -16
  63. package/dist/packages/core/src/commands/setNodeSelection.d.ts +0 -14
  64. package/dist/packages/core/src/commands/setTextSelection.d.ts +0 -14
  65. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -15
  66. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -17
  67. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -16
  68. package/dist/packages/core/src/commands/toggleList.d.ts +0 -18
  69. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -30
  70. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -17
  71. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -16
  72. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -13
  73. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -13
  74. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -25
  75. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -24
  76. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -16
  77. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -16
  78. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -5
  79. package/dist/packages/core/src/extensions/commands.d.ts +0 -3
  80. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  81. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  82. package/dist/packages/core/src/extensions/index.d.ts +0 -6
  83. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  84. package/dist/packages/core/src/extensions/tabindex.d.ts +0 -2
  85. package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +0 -10
  86. package/dist/packages/core/src/helpers/createChainableState.d.ts +0 -10
  87. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -12
  88. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -15
  89. package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +0 -7
  90. package/dist/packages/core/src/helpers/findChildren.d.ts +0 -9
  91. package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +0 -10
  92. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -16
  93. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -17
  94. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -8
  95. package/dist/packages/core/src/helpers/generateJSON.d.ts +0 -8
  96. package/dist/packages/core/src/helpers/generateText.d.ts +0 -12
  97. package/dist/packages/core/src/helpers/getAttributes.d.ts +0 -9
  98. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  99. package/dist/packages/core/src/helpers/getChangedRanges.d.ts +0 -11
  100. package/dist/packages/core/src/helpers/getDebugJSON.d.ts +0 -8
  101. package/dist/packages/core/src/helpers/getExtensionField.d.ts +0 -9
  102. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  103. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -3
  104. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  105. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  106. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  107. package/dist/packages/core/src/helpers/getNodeAtPosition.d.ts +0 -11
  108. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -3
  109. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  110. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  111. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -4
  112. package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +0 -10
  113. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -8
  114. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -8
  115. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -9
  116. package/dist/packages/core/src/helpers/getText.d.ts +0 -15
  117. package/dist/packages/core/src/helpers/getTextBetween.d.ts +0 -14
  118. package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +0 -8
  119. package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +0 -8
  120. package/dist/packages/core/src/helpers/index.d.ts +0 -50
  121. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  122. package/dist/packages/core/src/helpers/isActive.d.ts +0 -2
  123. package/dist/packages/core/src/helpers/isAtEndOfNode.d.ts +0 -2
  124. package/dist/packages/core/src/helpers/isAtStartOfNode.d.ts +0 -2
  125. package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +0 -2
  126. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  127. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -3
  128. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -3
  129. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -14
  130. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  131. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  132. package/dist/packages/core/src/helpers/posToDOMRect.d.ts +0 -2
  133. package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +0 -4
  134. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  135. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  136. package/dist/packages/core/src/index.d.ts +0 -24
  137. package/dist/packages/core/src/inputRules/index.d.ts +0 -5
  138. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -13
  139. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -23
  140. package/dist/packages/core/src/inputRules/textInputRule.d.ts +0 -10
  141. package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +0 -15
  142. package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +0 -28
  143. package/dist/packages/core/src/pasteRules/index.d.ts +0 -3
  144. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -13
  145. package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +0 -14
  146. package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +0 -10
  147. package/dist/packages/core/src/style.d.ts +0 -1
  148. package/dist/packages/core/src/types.d.ts +0 -253
  149. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -9
  150. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  151. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -6
  152. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  153. package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +0 -1
  154. package/dist/packages/core/src/utilities/findDuplicates.d.ts +0 -1
  155. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  156. package/dist/packages/core/src/utilities/index.d.ts +0 -20
  157. package/dist/packages/core/src/utilities/isAndroid.d.ts +0 -1
  158. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  159. package/dist/packages/core/src/utilities/isFunction.d.ts +0 -1
  160. package/dist/packages/core/src/utilities/isMacOS.d.ts +0 -1
  161. package/dist/packages/core/src/utilities/isNumber.d.ts +0 -1
  162. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  163. package/dist/packages/core/src/utilities/isRegExp.d.ts +0 -1
  164. package/dist/packages/core/src/utilities/isString.d.ts +0 -1
  165. package/dist/packages/core/src/utilities/isiOS.d.ts +0 -1
  166. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -1
  167. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -1
  168. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  169. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -8
  170. package/dist/packages/core/src/utilities/removeDuplicates.d.ts +0 -8
  171. package/dist/packages/extension-link/src/helpers/autolink.d.ts +0 -14
  172. package/dist/packages/extension-link/src/helpers/clickHandler.d.ts +0 -7
  173. package/dist/packages/extension-link/src/helpers/pasteHandler.d.ts +0 -10
  174. package/dist/packages/extension-link/src/index.d.ts +0 -3
package/dist/index.cjs CHANGED
@@ -1,348 +1,361 @@
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 src_exports = {};
22
+ __export(src_exports, {
23
+ Link: () => Link,
24
+ default: () => src_default,
25
+ pasteRegex: () => pasteRegex
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
4
28
 
5
- var core = require('@tiptap/core');
6
- var linkifyjs = require('linkifyjs');
7
- var state = require('@tiptap/pm/state');
29
+ // src/link.ts
30
+ var import_core3 = require("@tiptap/core");
31
+ var import_linkifyjs3 = require("linkifyjs");
8
32
 
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
- */
33
+ // src/helpers/autolink.ts
34
+ var import_core = require("@tiptap/core");
35
+ var import_state = require("@tiptap/pm/state");
36
+ var import_linkifyjs = require("linkifyjs");
19
37
  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;
38
+ if (tokens.length === 1) {
39
+ return tokens[0].isLink;
40
+ }
41
+ if (tokens.length === 3 && tokens[1].isLink) {
42
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
43
+ }
44
+ return false;
27
45
  }
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
46
  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;
47
+ return new import_state.Plugin({
48
+ key: new import_state.PluginKey("autolink"),
49
+ appendTransaction: (transactions, oldState, newState) => {
50
+ const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
51
+ const preventAutolink = transactions.some((transaction) => transaction.getMeta("preventAutolink"));
52
+ if (!docChanges || preventAutolink) {
53
+ return;
54
+ }
55
+ const { tr } = newState;
56
+ const transform = (0, import_core.combineTransactionSteps)(oldState.doc, [...transactions]);
57
+ const changes = (0, import_core.getChangedRanges)(transform);
58
+ changes.forEach(({ newRange }) => {
59
+ const nodesInChangedRanges = (0, import_core.findChildrenInRange)(
60
+ newState.doc,
61
+ newRange,
62
+ (node) => node.isTextblock
63
+ );
64
+ let textBlock;
65
+ let textBeforeWhitespace;
66
+ if (nodesInChangedRanges.length > 1) {
67
+ textBlock = nodesInChangedRanges[0];
68
+ textBeforeWhitespace = newState.doc.textBetween(
69
+ textBlock.pos,
70
+ textBlock.pos + textBlock.node.nodeSize,
71
+ void 0,
72
+ " "
73
+ );
74
+ } else if (nodesInChangedRanges.length && newState.doc.textBetween(newRange.from, newRange.to, " ", " ").endsWith(" ")) {
75
+ textBlock = nodesInChangedRanges[0];
76
+ textBeforeWhitespace = newState.doc.textBetween(
77
+ textBlock.pos,
78
+ newRange.to,
79
+ void 0,
80
+ " "
81
+ );
82
+ }
83
+ if (textBlock && textBeforeWhitespace) {
84
+ const wordsBeforeWhitespace = textBeforeWhitespace.split(" ").filter((s) => s !== "");
85
+ if (wordsBeforeWhitespace.length <= 0) {
86
+ return false;
87
+ }
88
+ const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
89
+ const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
90
+ if (!lastWordBeforeSpace) {
91
+ return false;
92
+ }
93
+ const linksBeforeSpace = (0, import_linkifyjs.tokenize)(lastWordBeforeSpace).map((t) => t.toObject(options.defaultProtocol));
94
+ if (!isValidLinkStructure(linksBeforeSpace)) {
95
+ return false;
96
+ }
97
+ linksBeforeSpace.filter((link) => link.isLink).map((link) => ({
98
+ ...link,
99
+ from: lastWordAndBlockOffset + link.start + 1,
100
+ to: lastWordAndBlockOffset + link.end + 1
101
+ })).filter((link) => {
102
+ if (!newState.schema.marks.code) {
103
+ return true;
51
104
  }
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
- // Add link mark.
103
- .forEach(link => {
104
- if (core.getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) {
105
- return;
106
- }
107
- tr.addMark(link.from, link.to, options.type.create({
108
- href: link.href,
109
- }));
110
- });
111
- }
112
- });
113
- if (!tr.steps.length) {
114
- return;
105
+ return !newState.doc.rangeHasMark(
106
+ link.from,
107
+ link.to,
108
+ newState.schema.marks.code
109
+ );
110
+ }).filter((link) => options.validate(link.value)).forEach((link) => {
111
+ if ((0, import_core.getMarksBetween)(link.from, link.to, newState.doc).some((item) => item.mark.type === options.type)) {
112
+ return;
115
113
  }
116
- return tr;
117
- },
118
- });
114
+ tr.addMark(
115
+ link.from,
116
+ link.to,
117
+ options.type.create({
118
+ href: link.href
119
+ })
120
+ );
121
+ });
122
+ }
123
+ });
124
+ if (!tr.steps.length) {
125
+ return;
126
+ }
127
+ return tr;
128
+ }
129
+ });
119
130
  }
120
131
 
132
+ // src/helpers/clickHandler.ts
133
+ var import_core2 = require("@tiptap/core");
134
+ var import_state2 = require("@tiptap/pm/state");
121
135
  function clickHandler(options) {
122
- return new state.Plugin({
123
- key: new state.PluginKey('handleClickLink'),
124
- props: {
125
- handleClick: (view, pos, event) => {
126
- var _a, _b;
127
- if (event.button !== 0) {
128
- return false;
129
- }
130
- if (!view.editable) {
131
- return false;
132
- }
133
- let a = event.target;
134
- const els = [];
135
- while (a.nodeName !== 'DIV') {
136
- els.push(a);
137
- a = a.parentNode;
138
- }
139
- if (!els.find(value => value.nodeName === 'A')) {
140
- return false;
141
- }
142
- const attrs = core.getAttributes(view.state, options.type.name);
143
- const link = event.target;
144
- const href = (_a = link === null || link === void 0 ? void 0 : link.href) !== null && _a !== void 0 ? _a : attrs.href;
145
- const target = (_b = link === null || link === void 0 ? void 0 : link.target) !== null && _b !== void 0 ? _b : attrs.target;
146
- if (link && href) {
147
- window.open(href, target);
148
- return true;
149
- }
150
- return false;
151
- },
152
- },
153
- });
136
+ return new import_state2.Plugin({
137
+ key: new import_state2.PluginKey("handleClickLink"),
138
+ props: {
139
+ handleClick: (view, pos, event) => {
140
+ var _a, _b;
141
+ if (event.button !== 0) {
142
+ return false;
143
+ }
144
+ if (!view.editable) {
145
+ return false;
146
+ }
147
+ let a = event.target;
148
+ const els = [];
149
+ while (a.nodeName !== "DIV") {
150
+ els.push(a);
151
+ a = a.parentNode;
152
+ }
153
+ if (!els.find((value) => value.nodeName === "A")) {
154
+ return false;
155
+ }
156
+ const attrs = (0, import_core2.getAttributes)(view.state, options.type.name);
157
+ const link = event.target;
158
+ const href = (_a = link == null ? void 0 : link.href) != null ? _a : attrs.href;
159
+ const target = (_b = link == null ? void 0 : link.target) != null ? _b : attrs.target;
160
+ if (link && href) {
161
+ window.open(href, target);
162
+ return true;
163
+ }
164
+ return false;
165
+ }
166
+ }
167
+ });
154
168
  }
155
169
 
170
+ // src/helpers/pasteHandler.ts
171
+ var import_state3 = require("@tiptap/pm/state");
172
+ var import_linkifyjs2 = require("linkifyjs");
156
173
  function pasteHandler(options) {
157
- return new state.Plugin({
158
- key: new state.PluginKey('handlePasteLink'),
159
- props: {
160
- handlePaste: (view, event, slice) => {
161
- const { state } = view;
162
- const { selection } = state;
163
- const { empty } = selection;
164
- if (empty) {
165
- return false;
166
- }
167
- let textContent = '';
168
- slice.content.forEach(node => {
169
- textContent += node.textContent;
170
- });
171
- const link = linkifyjs.find(textContent, { defaultProtocol: options.defaultProtocol }).find(item => item.isLink && item.value === textContent);
172
- if (!textContent || !link) {
173
- return false;
174
- }
175
- options.editor.commands.setMark(options.type, {
176
- href: link.href,
177
- });
178
- return true;
179
- },
180
- },
181
- });
174
+ return new import_state3.Plugin({
175
+ key: new import_state3.PluginKey("handlePasteLink"),
176
+ props: {
177
+ handlePaste: (view, event, slice) => {
178
+ const { state } = view;
179
+ const { selection } = state;
180
+ const { empty } = selection;
181
+ if (empty) {
182
+ return false;
183
+ }
184
+ let textContent = "";
185
+ slice.content.forEach((node) => {
186
+ textContent += node.textContent;
187
+ });
188
+ const link = (0, import_linkifyjs2.find)(textContent, { defaultProtocol: options.defaultProtocol }).find((item) => item.isLink && item.value === textContent);
189
+ if (!textContent || !link) {
190
+ return false;
191
+ }
192
+ options.editor.commands.setMark(options.type, {
193
+ href: link.href
194
+ });
195
+ return true;
196
+ }
197
+ }
198
+ });
182
199
  }
183
200
 
184
- const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
185
- // From DOMPurify
186
- // https://github.com/cure53/DOMPurify/blob/main/src/regexp.js
187
- const ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g; // eslint-disable-line no-control-regex
188
- const IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i; // eslint-disable-line no-useless-escape
201
+ // src/link.ts
202
+ var pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
203
+ var ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
204
+ var IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
189
205
  function isAllowedUri(uri) {
190
- return !uri || uri.replace(ATTR_WHITESPACE, '').match(IS_ALLOWED_URI);
206
+ return !uri || uri.replace(ATTR_WHITESPACE, "").match(IS_ALLOWED_URI);
191
207
  }
192
- /**
193
- * This extension allows you to create links.
194
- * @see https://www.tiptap.dev/api/marks/link
195
- */
196
- const Link = core.Mark.create({
197
- name: 'link',
198
- priority: 1000,
199
- keepOnSplit: false,
200
- exitable: true,
201
- onCreate() {
202
- this.options.protocols.forEach(protocol => {
203
- if (typeof protocol === 'string') {
204
- linkifyjs.registerCustomProtocol(protocol);
205
- return;
206
- }
207
- linkifyjs.registerCustomProtocol(protocol.scheme, protocol.optionalSlashes);
208
- });
209
- },
210
- onDestroy() {
211
- linkifyjs.reset();
212
- },
213
- inclusive() {
214
- return this.options.autolink;
215
- },
216
- addOptions() {
217
- return {
218
- openOnClick: true,
219
- linkOnPaste: true,
220
- autolink: true,
221
- protocols: [],
222
- defaultProtocol: 'http',
223
- HTMLAttributes: {
224
- target: '_blank',
225
- rel: 'noopener noreferrer nofollow',
226
- class: null,
227
- },
228
- validate: url => !!url,
229
- };
230
- },
231
- addAttributes() {
232
- return {
233
- href: {
234
- default: null,
235
- parseHTML(element) {
236
- return element.getAttribute('href');
237
- },
238
- },
239
- target: {
240
- default: this.options.HTMLAttributes.target,
241
- },
242
- rel: {
243
- default: this.options.HTMLAttributes.rel,
244
- },
245
- class: {
246
- default: this.options.HTMLAttributes.class,
247
- },
248
- };
249
- },
250
- parseHTML() {
251
- return [{
252
- tag: 'a[href]',
253
- getAttrs: dom => {
254
- const href = dom.getAttribute('href');
255
- // prevent XSS attacks
256
- if (!href || !isAllowedUri(href)) {
257
- return false;
258
- }
259
- return null;
260
- },
261
- }];
262
- },
263
- renderHTML({ HTMLAttributes }) {
264
- // prevent XSS attacks
265
- if (!isAllowedUri(HTMLAttributes.href)) {
266
- // strip out the href
267
- return ['a', core.mergeAttributes(this.options.HTMLAttributes, { ...HTMLAttributes, href: '' }), 0];
268
- }
269
- return ['a', core.mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
270
- },
271
- addCommands() {
272
- return {
273
- setLink: attributes => ({ chain }) => {
274
- return chain().setMark(this.name, attributes).setMeta('preventAutolink', true).run();
275
- },
276
- toggleLink: attributes => ({ chain }) => {
277
- return chain()
278
- .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
279
- .setMeta('preventAutolink', true)
280
- .run();
281
- },
282
- unsetLink: () => ({ chain }) => {
283
- return chain()
284
- .unsetMark(this.name, { extendEmptyMarkRange: true })
285
- .setMeta('preventAutolink', true)
286
- .run();
287
- },
288
- };
289
- },
290
- addPasteRules() {
291
- return [
292
- core.markPasteRule({
293
- find: text => {
294
- const foundLinks = [];
295
- if (text) {
296
- const { validate } = this.options;
297
- const links = linkifyjs.find(text).filter(item => item.isLink && validate(item.value));
298
- if (links.length) {
299
- links.forEach(link => (foundLinks.push({
300
- text: link.value,
301
- data: {
302
- href: link.href,
303
- },
304
- index: link.start,
305
- })));
306
- }
307
- }
308
- return foundLinks;
309
- },
310
- type: this.type,
311
- getAttributes: match => {
312
- var _a;
313
- return {
314
- href: (_a = match.data) === null || _a === void 0 ? void 0 : _a.href,
315
- };
316
- },
317
- }),
318
- ];
319
- },
320
- addProseMirrorPlugins() {
321
- const plugins = [];
322
- if (this.options.autolink) {
323
- plugins.push(autolink({
324
- type: this.type,
325
- defaultProtocol: this.options.defaultProtocol,
326
- validate: this.options.validate,
327
- }));
208
+ var Link = import_core3.Mark.create({
209
+ name: "link",
210
+ priority: 1e3,
211
+ keepOnSplit: false,
212
+ exitable: true,
213
+ onCreate() {
214
+ this.options.protocols.forEach((protocol) => {
215
+ if (typeof protocol === "string") {
216
+ (0, import_linkifyjs3.registerCustomProtocol)(protocol);
217
+ return;
218
+ }
219
+ (0, import_linkifyjs3.registerCustomProtocol)(protocol.scheme, protocol.optionalSlashes);
220
+ });
221
+ },
222
+ onDestroy() {
223
+ (0, import_linkifyjs3.reset)();
224
+ },
225
+ inclusive() {
226
+ return this.options.autolink;
227
+ },
228
+ addOptions() {
229
+ return {
230
+ openOnClick: true,
231
+ linkOnPaste: true,
232
+ autolink: true,
233
+ protocols: [],
234
+ defaultProtocol: "http",
235
+ HTMLAttributes: {
236
+ target: "_blank",
237
+ rel: "noopener noreferrer nofollow",
238
+ class: null
239
+ },
240
+ validate: (url) => !!url
241
+ };
242
+ },
243
+ addAttributes() {
244
+ return {
245
+ href: {
246
+ default: null,
247
+ parseHTML(element) {
248
+ return element.getAttribute("href");
328
249
  }
329
- if (this.options.openOnClick === true) {
330
- plugins.push(clickHandler({
331
- type: this.type,
332
- }));
250
+ },
251
+ target: {
252
+ default: this.options.HTMLAttributes.target
253
+ },
254
+ rel: {
255
+ default: this.options.HTMLAttributes.rel
256
+ },
257
+ class: {
258
+ default: this.options.HTMLAttributes.class
259
+ }
260
+ };
261
+ },
262
+ parseHTML() {
263
+ return [{
264
+ tag: "a[href]",
265
+ getAttrs: (dom) => {
266
+ const href = dom.getAttribute("href");
267
+ if (!href || !isAllowedUri(href)) {
268
+ return false;
333
269
  }
334
- if (this.options.linkOnPaste) {
335
- plugins.push(pasteHandler({
336
- editor: this.editor,
337
- defaultProtocol: this.options.defaultProtocol,
338
- type: this.type,
339
- }));
270
+ return null;
271
+ }
272
+ }];
273
+ },
274
+ renderHTML({ HTMLAttributes }) {
275
+ if (!isAllowedUri(HTMLAttributes.href)) {
276
+ return ["a", (0, import_core3.mergeAttributes)(this.options.HTMLAttributes, { ...HTMLAttributes, href: "" }), 0];
277
+ }
278
+ return ["a", (0, import_core3.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes), 0];
279
+ },
280
+ addCommands() {
281
+ return {
282
+ setLink: (attributes) => ({ chain }) => {
283
+ return chain().setMark(this.name, attributes).setMeta("preventAutolink", true).run();
284
+ },
285
+ toggleLink: (attributes) => ({ chain }) => {
286
+ return chain().toggleMark(this.name, attributes, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
287
+ },
288
+ unsetLink: () => ({ chain }) => {
289
+ return chain().unsetMark(this.name, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
290
+ }
291
+ };
292
+ },
293
+ addPasteRules() {
294
+ return [
295
+ (0, import_core3.markPasteRule)({
296
+ find: (text) => {
297
+ const foundLinks = [];
298
+ if (text) {
299
+ const { validate } = this.options;
300
+ const links = (0, import_linkifyjs3.find)(text).filter((item) => item.isLink && validate(item.value));
301
+ if (links.length) {
302
+ links.forEach((link) => foundLinks.push({
303
+ text: link.value,
304
+ data: {
305
+ href: link.href
306
+ },
307
+ index: link.start
308
+ }));
309
+ }
310
+ }
311
+ return foundLinks;
312
+ },
313
+ type: this.type,
314
+ getAttributes: (match) => {
315
+ var _a;
316
+ return {
317
+ href: (_a = match.data) == null ? void 0 : _a.href
318
+ };
340
319
  }
341
- return plugins;
342
- },
320
+ })
321
+ ];
322
+ },
323
+ addProseMirrorPlugins() {
324
+ const plugins = [];
325
+ if (this.options.autolink) {
326
+ plugins.push(
327
+ autolink({
328
+ type: this.type,
329
+ defaultProtocol: this.options.defaultProtocol,
330
+ validate: this.options.validate
331
+ })
332
+ );
333
+ }
334
+ if (this.options.openOnClick === true) {
335
+ plugins.push(
336
+ clickHandler({
337
+ type: this.type
338
+ })
339
+ );
340
+ }
341
+ if (this.options.linkOnPaste) {
342
+ plugins.push(
343
+ pasteHandler({
344
+ editor: this.editor,
345
+ defaultProtocol: this.options.defaultProtocol,
346
+ type: this.type
347
+ })
348
+ );
349
+ }
350
+ return plugins;
351
+ }
343
352
  });
344
353
 
345
- exports.Link = Link;
346
- exports.default = Link;
347
- exports.pasteRegex = pasteRegex;
348
- //# sourceMappingURL=index.cjs.map
354
+ // src/index.ts
355
+ var src_default = Link;
356
+ // Annotate the CommonJS export names for ESM import in node:
357
+ 0 && (module.exports = {
358
+ Link,
359
+ pasteRegex
360
+ });
361
+ //# sourceMappingURL=index.cjs.map