@tiptap/extension-link 3.0.0-next.0 → 3.0.0-next.2

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