@blocknote/core 0.48.0 → 0.49.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.
Files changed (72) hide show
  1. package/dist/blocknote.cjs +1 -1
  2. package/dist/blocknote.js +3 -3
  3. package/dist/{blocks-DYl7UTS4.js → blocks-CSfJen16.js} +18 -2
  4. package/dist/blocks-CSfJen16.js.map +1 -0
  5. package/dist/{blocks-BEvIOzln.cjs → blocks-_lpGWKOa.cjs} +3 -3
  6. package/dist/blocks-_lpGWKOa.cjs.map +1 -0
  7. package/dist/blocks.cjs +1 -1
  8. package/dist/blocks.js +1 -1
  9. package/dist/comments.cjs +1 -1
  10. package/dist/comments.cjs.map +1 -1
  11. package/dist/comments.js +2 -2
  12. package/dist/comments.js.map +1 -1
  13. package/dist/extensions-BfYaEv6F.cjs +2 -0
  14. package/dist/extensions-BfYaEv6F.cjs.map +1 -0
  15. package/dist/{extensions-3nf9PPhA.js → extensions-C3nguvVr.js} +240 -248
  16. package/dist/extensions-C3nguvVr.js.map +1 -0
  17. package/dist/extensions.cjs +1 -1
  18. package/dist/extensions.js +2 -2
  19. package/dist/src-ChTwrMcE.cjs +5 -0
  20. package/dist/src-ChTwrMcE.cjs.map +1 -0
  21. package/dist/{src-Hjv1f5lc.js → src-DQfz4zLM.js} +757 -373
  22. package/dist/src-DQfz4zLM.js.map +1 -0
  23. package/dist/style.css +1 -1
  24. package/dist/webpack-stats.json +1 -1
  25. package/dist/yjs.cjs +1 -1
  26. package/dist/yjs.js +1 -1
  27. package/package.json +6 -8
  28. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +14 -1
  29. package/src/blocks/Quote/block.ts +9 -0
  30. package/src/blocks/Table/block.ts +26 -0
  31. package/src/comments/extension.ts +5 -0
  32. package/src/comments/threadstore/yjs/YjsThreadStore.test.ts +3 -2
  33. package/src/comments/threadstore/yjs/YjsThreadStore.ts +4 -4
  34. package/src/editor/BlockNoteEditor.ts +84 -5
  35. package/src/editor/editor.css +5 -0
  36. package/src/editor/managers/ExtensionManager/extensions.ts +8 -26
  37. package/src/editor/managers/StyleManager.ts +90 -10
  38. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +23 -16
  39. package/src/extensions/LinkToolbar/LinkToolbar.ts +30 -75
  40. package/src/extensions/Placeholder/Placeholder.ts +2 -2
  41. package/src/extensions/tiptap-extensions/Link/helpers/autolink.ts +179 -0
  42. package/src/extensions/tiptap-extensions/Link/helpers/clickHandler.ts +82 -0
  43. package/src/extensions/tiptap-extensions/Link/helpers/linkDetector.ts +414 -0
  44. package/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.ts +53 -0
  45. package/src/extensions/tiptap-extensions/Link/helpers/tlds.ts +7 -0
  46. package/src/extensions/tiptap-extensions/Link/helpers/whitespace.ts +13 -0
  47. package/src/extensions/tiptap-extensions/Link/index.ts +2 -0
  48. package/src/extensions/tiptap-extensions/Link/link.test.ts +941 -0
  49. package/src/extensions/tiptap-extensions/Link/link.ts +207 -0
  50. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +3 -5
  51. package/src/schema/inlineContent/types.ts +1 -1
  52. package/types/src/editor/BlockNoteEditor.d.ts +69 -3
  53. package/types/src/editor/managers/StyleManager.d.ts +22 -0
  54. package/types/src/extensions/LinkToolbar/LinkToolbar.d.ts +28 -7
  55. package/types/src/extensions/tiptap-extensions/Link/helpers/autolink.d.ts +13 -0
  56. package/types/src/extensions/tiptap-extensions/Link/helpers/clickHandler.d.ts +12 -0
  57. package/types/src/extensions/tiptap-extensions/Link/helpers/linkDetector.d.ts +32 -0
  58. package/types/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.d.ts +12 -0
  59. package/types/src/extensions/tiptap-extensions/Link/helpers/tlds.d.ts +1 -0
  60. package/types/src/extensions/tiptap-extensions/Link/helpers/whitespace.d.ts +4 -0
  61. package/types/src/extensions/tiptap-extensions/Link/index.d.ts +2 -0
  62. package/types/src/extensions/tiptap-extensions/Link/link.d.ts +13 -0
  63. package/types/src/extensions/tiptap-extensions/Link/link.test.d.ts +1 -0
  64. package/types/src/schema/inlineContent/types.d.ts +1 -2
  65. package/dist/blocks-BEvIOzln.cjs.map +0 -1
  66. package/dist/blocks-DYl7UTS4.js.map +0 -1
  67. package/dist/extensions-3nf9PPhA.js.map +0 -1
  68. package/dist/extensions-D9y6evn6.cjs +0 -2
  69. package/dist/extensions-D9y6evn6.cjs.map +0 -1
  70. package/dist/src-BKJH7Jla.cjs +0 -5
  71. package/dist/src-BKJH7Jla.cjs.map +0 -1
  72. package/dist/src-Hjv1f5lc.js.map +0 -1
@@ -5,7 +5,7 @@ import {
5
5
  Extension as TiptapExtension,
6
6
  } from "@tiptap/core";
7
7
  import { Gapcursor } from "@tiptap/extensions/gap-cursor";
8
- import { Link } from "@tiptap/extension-link";
8
+ import { Link } from "../../../extensions/tiptap-extensions/Link/link.js";
9
9
  import { Text } from "@tiptap/extension-text";
10
10
  import { createDropFileExtension } from "../../../api/clipboard/fromClipboard/fileDropExtension.js";
11
11
  import { createPasteFromClipboardExtension } from "../../../api/clipboard/fromClipboard/pasteExtension.js";
@@ -26,10 +26,6 @@ import {
26
26
  TableHandlesExtension,
27
27
  TrailingNodeExtension,
28
28
  } from "../../../extensions/index.js";
29
- import {
30
- DEFAULT_LINK_PROTOCOL,
31
- VALID_LINK_PROTOCOLS,
32
- } from "../../../extensions/LinkToolbar/protocols.js";
33
29
  import {
34
30
  BackgroundColorExtension,
35
31
  HardBreak,
@@ -49,9 +45,6 @@ import {
49
45
  import { ExtensionFactoryInstance } from "../../BlockNoteExtension.js";
50
46
  import { CollaborationExtension } from "../../../extensions/Collaboration/Collaboration.js";
51
47
 
52
- // TODO remove linkify completely by vendoring the link extension & dropping linkifyjs as a dependency
53
- let LINKIFY_INITIALIZED = false;
54
-
55
48
  /**
56
49
  * Get all the Tiptap extensions BlockNote is configured with by default
57
50
  */
@@ -80,22 +73,13 @@ export function getDefaultTiptapExtensions(
80
73
  SuggestionAddMark,
81
74
  SuggestionDeleteMark,
82
75
  SuggestionModificationMark,
83
- Link.extend({
84
- inclusive: false,
85
- })
86
- .extend({
87
- // Remove the title attribute added in newer versions of @tiptap/extension-link
88
- // to avoid unnecessary null attributes in serialized output
89
- addAttributes() {
90
- const attrs = this.parent?.() || {};
91
- delete (attrs as Record<string, unknown>).title;
92
- return attrs;
93
- },
94
- })
95
- .configure({
96
- defaultProtocol: DEFAULT_LINK_PROTOCOL,
97
- // only call this once if we have multiple editors installed. Or fix https://github.com/ueberdosis/tiptap/issues/5450
98
- protocols: LINKIFY_INITIALIZED ? [] : VALID_LINK_PROTOCOLS,
76
+ Link.configure({
77
+ HTMLAttributes: options.links?.HTMLAttributes ?? {},
78
+ editor,
79
+ onClick: options.links?.onClick,
80
+ ...(options.links?.isValidLink
81
+ ? { isValidLink: options.links.isValidLink }
82
+ : {}),
99
83
  }),
100
84
  ...(Object.values(editor.schema.styleSpecs).map((styleSpec) => {
101
85
  return styleSpec.implementation.mark.configure({
@@ -173,8 +157,6 @@ export function getDefaultTiptapExtensions(
173
157
  createDropFileExtension(editor),
174
158
  ];
175
159
 
176
- LINKIFY_INITIALIZED = true;
177
-
178
160
  return tiptapExtensions;
179
161
  }
180
162
 
@@ -1,3 +1,4 @@
1
+ import { getMarkRange } from "@tiptap/core";
1
2
  import { insertContentAt } from "../../api/blockManipulation/insertContentAt.js";
2
3
  import { inlineContentToNodes } from "../../api/nodeConversions/blockToNode.js";
3
4
  import {
@@ -12,7 +13,6 @@ import {
12
13
  DefaultInlineContentSchema,
13
14
  DefaultStyleSchema,
14
15
  } from "../../blocks/defaultBlocks.js";
15
- import { TextSelection } from "@tiptap/pm/state";
16
16
  import { UnreachableCaseError } from "../../util/typescript.js";
17
17
  import { BlockNoteEditor } from "../BlockNoteEditor.js";
18
18
 
@@ -146,13 +146,42 @@ export class StyleManager<
146
146
  });
147
147
  }
148
148
 
149
+ /**
150
+ * Find the link mark and its range at the given position.
151
+ * Returns undefined if there is no link at that position.
152
+ */
153
+ public getLinkMarkAtPos(pos: number) {
154
+ return this.editor.transact((tr) => {
155
+ const resolvedPos = tr.doc.resolve(pos);
156
+ const linkMark = resolvedPos
157
+ .marks()
158
+ .find((mark) => mark.type.name === "link");
159
+
160
+ if (!linkMark) {
161
+ return undefined;
162
+ }
163
+
164
+ const range = getMarkRange(resolvedPos, linkMark.type);
165
+ if (!range) {
166
+ return undefined;
167
+ }
168
+
169
+ return {
170
+ href: linkMark.attrs.href as string,
171
+ from: range.from,
172
+ to: range.to,
173
+ text: tr.doc.textBetween(range.from, range.to),
174
+ };
175
+ });
176
+ }
177
+
149
178
  /**
150
179
  * Gets the URL of the last link in the current selection, or `undefined` if there are no links in the selection.
151
180
  */
152
181
  public getSelectedLinkUrl() {
153
- return this.editor._tiptapEditor.getAttributes("link").href as
154
- | string
155
- | undefined;
182
+ return this.editor.transact((tr) => {
183
+ return this.getLinkMarkAtPos(tr.selection.from)?.href;
184
+ });
156
185
  }
157
186
 
158
187
  /**
@@ -164,19 +193,70 @@ export class StyleManager<
164
193
  if (url === "") {
165
194
  return;
166
195
  }
167
- const mark = this.editor.pmSchema.mark("link", { href: url });
196
+
168
197
  this.editor.transact((tr) => {
169
198
  const { from, to } = tr.selection;
199
+ const linkMark = this.editor.pmSchema.mark("link", { href: url });
170
200
 
171
201
  if (text) {
172
- tr.insertText(text, from, to).addMark(from, from + text.length, mark);
173
- } else {
174
- tr.setSelection(TextSelection.create(tr.doc, to)).addMark(
202
+ tr.insertText(text, from, to).addMark(
175
203
  from,
176
- to,
177
- mark,
204
+ from + text.length,
205
+ linkMark,
178
206
  );
207
+ } else {
208
+ tr.addMark(from, to, linkMark);
209
+ }
210
+ });
211
+ }
212
+
213
+ /**
214
+ * Updates the link at the given position with a new URL and text.
215
+ * @param url The new link URL.
216
+ * @param text The new text to display.
217
+ * @param position The position inside the link to edit. Defaults to the current selection anchor.
218
+ */
219
+ public editLink(
220
+ url: string,
221
+ text: string,
222
+ position = this.editor.transact((tr) => tr.selection.anchor),
223
+ ) {
224
+ this.editor.transact((tr) => {
225
+ const linkData = this.getLinkMarkAtPos(position + 1);
226
+ const { from, to } = linkData || {
227
+ from: tr.selection.from,
228
+ to: tr.selection.to,
229
+ };
230
+
231
+ const linkMark = this.editor.pmSchema.mark("link", { href: url });
232
+ const existingText = tr.doc.textBetween(from, to);
233
+ if (text !== existingText) {
234
+ tr.insertText(text, from, to);
179
235
  }
236
+ tr.addMark(from, from + text.length, linkMark);
237
+ });
238
+ this.editor.prosemirrorView.focus();
239
+ }
240
+
241
+ /**
242
+ * Removes the link at the given position, keeping the text.
243
+ * @param position The position inside the link to remove. Defaults to the current selection anchor.
244
+ */
245
+ public deleteLink(
246
+ position = this.editor.transact((tr) => tr.selection.anchor),
247
+ ) {
248
+ this.editor.transact((tr) => {
249
+ const linkData = this.getLinkMarkAtPos(position + 1);
250
+ const { from, to } = linkData || {
251
+ from: tr.selection.from,
252
+ to: tr.selection.to,
253
+ };
254
+
255
+ tr.removeMark(from, to, this.editor.pmSchema.marks["link"]).setMeta(
256
+ "preventAutolink",
257
+ true,
258
+ );
180
259
  });
260
+ this.editor.prosemirrorView.focus();
181
261
  }
182
262
  }
@@ -1,4 +1,4 @@
1
- import { NodeSelection, TextSelection } from "prosemirror-state";
1
+ import { TextSelection } from "prosemirror-state";
2
2
 
3
3
  import {
4
4
  createExtension,
@@ -16,15 +16,6 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
16
16
  return false;
17
17
  }
18
18
 
19
- // Don't show if a block with inline content is selected.
20
- if (
21
- tr.selection instanceof NodeSelection &&
22
- (tr.selection.node.type.spec.content === "inline*" ||
23
- tr.selection.node.firstChild?.type.spec.content === "inline*")
24
- ) {
25
- return false;
26
- }
27
-
28
19
  // Don't show if the selection is a text selection but contains no text.
29
20
  if (
30
21
  tr.selection instanceof TextSelection &&
@@ -61,16 +52,17 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
61
52
  * We want to mimic the Notion behavior of not showing the toolbar while the user is holding down the mouse button (to create a selection)
62
53
  */
63
54
  let preventShowWhileMouseDown = false;
55
+ let preventShowWhileDragging = false;
64
56
 
65
57
  const unsubscribeOnChange = editor.onChange(() => {
66
- if (preventShowWhileMouseDown) {
58
+ if (preventShowWhileMouseDown || preventShowWhileDragging) {
67
59
  return;
68
60
  }
69
61
  // re-evaluate whether the toolbar should be shown
70
62
  store.setState(shouldShow());
71
63
  });
72
64
  const unsubscribeOnSelectionChange = editor.onSelectionChange(() => {
73
- if (preventShowWhileMouseDown) {
65
+ if (preventShowWhileMouseDown || preventShowWhileDragging) {
74
66
  return;
75
67
  }
76
68
  // re-evaluate whether the toolbar should be shown
@@ -91,6 +83,7 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
91
83
  "pointerup",
92
84
  () => {
93
85
  preventShowWhileMouseDown = false;
86
+
94
87
  // We only want to re-show the toolbar if the mouse made the selection
95
88
  if (editor.isFocused()) {
96
89
  store.setState(shouldShow());
@@ -102,12 +95,26 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
102
95
  dom.addEventListener(
103
96
  "pointercancel",
104
97
  () => {
105
- preventShowWhileMouseDown = false;
98
+ preventShowWhileMouseDown = true;
106
99
  },
107
- {
108
- signal,
109
- capture: true,
100
+ { signal, capture: true },
101
+ );
102
+
103
+ editor.prosemirrorView.root.addEventListener(
104
+ "dragstart",
105
+ () => {
106
+ preventShowWhileDragging = true;
107
+ store.setState(false);
108
+ },
109
+ { signal },
110
+ );
111
+
112
+ editor.prosemirrorView.root.addEventListener(
113
+ "dragend",
114
+ () => {
115
+ preventShowWhileDragging = false;
110
116
  },
117
+ { signal },
111
118
  );
112
119
 
113
120
  signal.addEventListener("abort", () => {
@@ -1,5 +1,4 @@
1
- import { getMarkRange, posToDOMRect } from "@tiptap/core";
2
- import { getPmSchema } from "../../api/pmUtil.js";
1
+ import { posToDOMRect } from "@tiptap/core";
3
2
  import { createExtension } from "../../editor/BlockNoteExtension.js";
4
3
 
5
4
  export const LinkToolbarExtension = createExtension(({ editor }) => {
@@ -14,47 +13,35 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
14
13
  return null;
15
14
  }
16
15
 
17
- function getMarkAtPos(pos: number, markType: string) {
18
- return editor.transact((tr) => {
19
- const resolvedPos = tr.doc.resolve(pos);
20
- const mark = resolvedPos
21
- .marks()
22
- .find((mark) => mark.type.name === markType);
23
-
24
- if (!mark) {
25
- return;
26
- }
27
-
28
- const markRange = getMarkRange(resolvedPos, mark.type);
29
- if (!markRange) {
30
- return;
31
- }
16
+ function getLinkAtPos(pos: number) {
17
+ const linkData = editor.getLinkMarkAtPos(pos);
18
+ if (!linkData) {
19
+ return undefined;
20
+ }
32
21
 
33
- return {
34
- range: markRange,
35
- mark,
36
- get text() {
37
- return tr.doc.textBetween(markRange.from, markRange.to);
38
- },
39
- get position() {
40
- // to minimize re-renders, we convert to JSON, which is the same shape anyway
41
- return posToDOMRect(
42
- editor.prosemirrorView,
43
- markRange.from,
44
- markRange.to,
45
- ).toJSON() as DOMRect;
46
- },
47
- };
48
- });
22
+ return {
23
+ range: { from: linkData.from, to: linkData.to },
24
+ // Expose mark-like attrs for backward compat with React LinkToolbarController
25
+ mark: { attrs: { href: linkData.href } },
26
+ get text() {
27
+ return linkData.text;
28
+ },
29
+ get position() {
30
+ return posToDOMRect(
31
+ editor.prosemirrorView,
32
+ linkData.from,
33
+ linkData.to,
34
+ ).toJSON() as DOMRect;
35
+ },
36
+ };
49
37
  }
50
38
 
51
39
  function getLinkAtSelection() {
52
40
  return editor.transact((tr) => {
53
- const selection = tr.selection;
54
- if (!selection.empty) {
41
+ if (!tr.selection.empty) {
55
42
  return undefined;
56
43
  }
57
- return getMarkAtPos(selection.anchor, "link");
44
+ return getLinkAtPos(tr.selection.anchor);
58
45
  });
59
46
  }
60
47
 
@@ -63,12 +50,14 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
63
50
 
64
51
  getLinkAtSelection,
65
52
  getLinkElementAtPos,
66
- getMarkAtPos,
53
+ getMarkAtPos(pos: number, _markType: string) {
54
+ return getLinkAtPos(pos);
55
+ },
67
56
 
68
57
  getLinkAtElement(element: HTMLElement) {
69
58
  return editor.transact(() => {
70
59
  const posAtElement = editor.prosemirrorView.posAtDOM(element, 0) + 1;
71
- return getMarkAtPos(posAtElement, "link");
60
+ return getLinkAtPos(posAtElement);
72
61
  });
73
62
  },
74
63
 
@@ -77,45 +66,11 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
77
66
  text: string,
78
67
  position = editor.transact((tr) => tr.selection.anchor),
79
68
  ) {
80
- editor.transact((tr) => {
81
- const pmSchema = getPmSchema(tr);
82
- const { range } = getMarkAtPos(position + 1, "link") || {
83
- range: {
84
- from: tr.selection.from,
85
- to: tr.selection.to,
86
- },
87
- };
88
- if (!range) {
89
- return;
90
- }
91
- tr.insertText(text, range.from, range.to);
92
- tr.addMark(
93
- range.from,
94
- range.from + text.length,
95
- pmSchema.mark("link", { href: url }),
96
- );
97
- });
98
- editor.prosemirrorView.focus();
69
+ editor.editLink(url, text, position);
99
70
  },
100
- deleteLink(position = editor.transact((tr) => tr.selection.anchor)) {
101
- editor.transact((tr) => {
102
- const pmSchema = getPmSchema(tr);
103
- const { range } = getMarkAtPos(position + 1, "link") || {
104
- range: {
105
- from: tr.selection.from,
106
- to: tr.selection.to,
107
- },
108
- };
109
- if (!range) {
110
- return;
111
- }
112
71
 
113
- tr.removeMark(range.from, range.to, pmSchema.marks["link"]).setMeta(
114
- "preventAutolink",
115
- true,
116
- );
117
- });
118
- editor.prosemirrorView.focus();
72
+ deleteLink(position = editor.transact((tr) => tr.selection.anchor)) {
73
+ editor.deleteLink(position);
119
74
  },
120
75
  } as const;
121
76
  });
@@ -1,6 +1,6 @@
1
1
  import { Plugin, PluginKey } from "prosemirror-state";
2
2
  import { Decoration, DecorationSet } from "prosemirror-view";
3
- import { v4 } from "uuid";
3
+ import { uuidv4 } from "lib0/random";
4
4
  import {
5
5
  createExtension,
6
6
  ExtensionOptions,
@@ -23,7 +23,7 @@ export const PlaceholderExtension = createExtension(
23
23
  new Plugin({
24
24
  key: PLUGIN_KEY,
25
25
  view: (view) => {
26
- const uniqueEditorSelector = `placeholder-selector-${v4()}`;
26
+ const uniqueEditorSelector = `placeholder-selector-${uuidv4()}`;
27
27
  view.dom.classList.add(uniqueEditorSelector);
28
28
  const styleEl = document.createElement("style");
29
29
 
@@ -0,0 +1,179 @@
1
+ import type { NodeWithPos } from "@tiptap/core";
2
+ import {
3
+ combineTransactionSteps,
4
+ findChildrenInRange,
5
+ getChangedRanges,
6
+ getMarksBetween,
7
+ } from "@tiptap/core";
8
+ import type { MarkType } from "@tiptap/pm/model";
9
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
10
+ import type { LinkMatch } from "./linkDetector.js";
11
+ import { tokenizeLink } from "./linkDetector.js";
12
+
13
+ import {
14
+ UNICODE_WHITESPACE_REGEX,
15
+ UNICODE_WHITESPACE_REGEX_END,
16
+ } from "./whitespace.js";
17
+
18
+ /**
19
+ * Check if the provided tokens form a valid link structure, which can either be a single link token
20
+ * or a link token surrounded by parentheses or square brackets.
21
+ */
22
+ function isValidLinkStructure(tokens: LinkMatch[]) {
23
+ if (tokens.length === 1) {
24
+ return tokens[0].isLink;
25
+ }
26
+
27
+ if (tokens.length === 3 && tokens[1].isLink) {
28
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
29
+ }
30
+
31
+ return false;
32
+ }
33
+
34
+ type AutolinkOptions = {
35
+ type: MarkType;
36
+ defaultProtocol: string;
37
+ validate: (url: string) => boolean;
38
+ shouldAutoLink: (url: string) => boolean;
39
+ };
40
+
41
+ /**
42
+ * Plugin that automatically adds link marks when typing URLs.
43
+ */
44
+ export function autolink(options: AutolinkOptions): Plugin {
45
+ return new Plugin({
46
+ key: new PluginKey("autolink"),
47
+ appendTransaction: (transactions, oldState, newState) => {
48
+ const docChanges =
49
+ transactions.some((transaction) => transaction.docChanged) &&
50
+ !oldState.doc.eq(newState.doc);
51
+
52
+ const preventAutolink = transactions.some((transaction) =>
53
+ transaction.getMeta("preventAutolink")
54
+ );
55
+
56
+ if (!docChanges || preventAutolink) {
57
+ return;
58
+ }
59
+
60
+ const { tr } = newState;
61
+ const transform = combineTransactionSteps(oldState.doc, [
62
+ ...transactions,
63
+ ]);
64
+ const changes = getChangedRanges(transform);
65
+
66
+ changes.forEach(({ newRange }) => {
67
+ const nodesInChangedRanges = findChildrenInRange(
68
+ newState.doc,
69
+ newRange,
70
+ (node) => node.isTextblock
71
+ );
72
+
73
+ let textBlock: NodeWithPos | undefined;
74
+ let textBeforeWhitespace: string | undefined;
75
+
76
+ if (nodesInChangedRanges.length > 1) {
77
+ textBlock = nodesInChangedRanges[0];
78
+ textBeforeWhitespace = newState.doc.textBetween(
79
+ textBlock.pos,
80
+ textBlock.pos + textBlock.node.nodeSize,
81
+ undefined,
82
+ " "
83
+ );
84
+ } else if (nodesInChangedRanges.length) {
85
+ const endText = newState.doc.textBetween(
86
+ newRange.from,
87
+ newRange.to,
88
+ " ",
89
+ " "
90
+ );
91
+ if (!UNICODE_WHITESPACE_REGEX_END.test(endText)) {
92
+ return;
93
+ }
94
+ textBlock = nodesInChangedRanges[0];
95
+ textBeforeWhitespace = newState.doc.textBetween(
96
+ textBlock.pos,
97
+ newRange.to,
98
+ undefined,
99
+ " "
100
+ );
101
+ }
102
+
103
+ if (textBlock && textBeforeWhitespace) {
104
+ const wordsBeforeWhitespace = textBeforeWhitespace
105
+ .split(UNICODE_WHITESPACE_REGEX)
106
+ .filter(Boolean);
107
+
108
+ if (wordsBeforeWhitespace.length <= 0) {
109
+ return;
110
+ }
111
+
112
+ const lastWordBeforeSpace =
113
+ wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
114
+ const lastWordAndBlockOffset =
115
+ textBlock.pos +
116
+ textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
117
+
118
+ if (!lastWordBeforeSpace) {
119
+ return;
120
+ }
121
+
122
+ const linksBeforeSpace = tokenizeLink(
123
+ lastWordBeforeSpace,
124
+ options.defaultProtocol
125
+ );
126
+
127
+ if (!isValidLinkStructure(linksBeforeSpace)) {
128
+ return;
129
+ }
130
+
131
+ linksBeforeSpace
132
+ .filter((link) => link.isLink)
133
+ .map((link) => ({
134
+ ...link,
135
+ from: lastWordAndBlockOffset + link.start + 1,
136
+ to: lastWordAndBlockOffset + link.end + 1,
137
+ }))
138
+ // ignore link inside code mark
139
+ .filter((link) => {
140
+ if (!newState.schema.marks.code) {
141
+ return true;
142
+ }
143
+
144
+ return !newState.doc.rangeHasMark(
145
+ link.from,
146
+ link.to,
147
+ newState.schema.marks.code
148
+ );
149
+ })
150
+ .filter((link) => options.validate(link.value))
151
+ .filter((link) => options.shouldAutoLink(link.value))
152
+ .forEach((link) => {
153
+ if (
154
+ getMarksBetween(link.from, link.to, newState.doc).some(
155
+ (item) => item.mark.type === options.type
156
+ )
157
+ ) {
158
+ return;
159
+ }
160
+
161
+ tr.addMark(
162
+ link.from,
163
+ link.to,
164
+ options.type.create({
165
+ href: link.href,
166
+ })
167
+ );
168
+ });
169
+ }
170
+ });
171
+
172
+ if (!tr.steps.length) {
173
+ return;
174
+ }
175
+
176
+ return tr;
177
+ },
178
+ });
179
+ }