@blocknote/core 0.48.1 → 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
@@ -0,0 +1,941 @@
1
+ import { afterEach, describe, expect, it } from "vitest";
2
+ import { TextSelection } from "@tiptap/pm/state";
3
+ import { Slice, Fragment } from "@tiptap/pm/model";
4
+
5
+ import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
6
+ import { findLinks, tokenizeLink } from "./helpers/linkDetector.js";
7
+ import { isAllowedUri } from "./link.js";
8
+
9
+ /**
10
+ * @vitest-environment jsdom
11
+ */
12
+
13
+ // ============================================================================
14
+ // Helpers
15
+ // ============================================================================
16
+
17
+ /** Wrapper matching the old tokenize().map(t => t.toObject(defaultProtocol)) pattern */
18
+ function tokenizeToObjects(text: string, defaultProtocol = "http") {
19
+ return tokenizeLink(text, defaultProtocol);
20
+ }
21
+
22
+ /**
23
+ * Mirrors the isValidLinkStructure function from autolink.ts.
24
+ * A valid structure is either:
25
+ * - A single link token
26
+ * - A link token wrapped in () or []
27
+ */
28
+ function isValidLinkStructure(
29
+ tokens: Array<{ isLink: boolean; value: string }>
30
+ ) {
31
+ if (tokens.length === 1) {
32
+ return tokens[0].isLink;
33
+ }
34
+ if (tokens.length === 3 && tokens[1].isLink) {
35
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
36
+ }
37
+ return false;
38
+ }
39
+
40
+ function createEditor(links?: { isValidLink?: (href: string) => boolean }) {
41
+ const editor = BlockNoteEditor.create(links ? { links } : undefined);
42
+ const div = document.createElement("div");
43
+ editor.mount(div);
44
+ return editor;
45
+ }
46
+
47
+ /**
48
+ * Insert text at the end of a block, followed by a space to trigger autolink.
49
+ * Returns the link marks found in that block afterward.
50
+ */
51
+ function typeTextThenSpace(
52
+ editor: BlockNoteEditor<any, any, any>,
53
+ blockId: string,
54
+ text: string
55
+ ) {
56
+ editor.setTextCursorPosition(blockId, "end");
57
+ const view = editor._tiptapEditor.view;
58
+ const { from } = view.state.selection;
59
+
60
+ // Insert the text
61
+ view.dispatch(view.state.tr.insertText(text, from));
62
+
63
+ // Now insert a space to trigger autolink
64
+ const afterInsert = view.state.selection.from;
65
+ view.dispatch(view.state.tr.insertText(" ", afterInsert));
66
+
67
+ return getLinksInDocument(editor);
68
+ }
69
+
70
+ /**
71
+ * Walk the ProseMirror doc and collect all link marks with their text and href.
72
+ */
73
+ function getLinksInDocument(editor: BlockNoteEditor<any, any, any>) {
74
+ const links: Array<{ text: string; href: string; from: number; to: number }> =
75
+ [];
76
+ const doc = editor._tiptapEditor.state.doc;
77
+ const linkType = editor._tiptapEditor.schema.marks.link;
78
+
79
+ doc.descendants((node, pos) => {
80
+ if (node.isText && node.marks.length > 0) {
81
+ const linkMark = node.marks.find((m) => m.type === linkType);
82
+ if (linkMark) {
83
+ links.push({
84
+ text: node.text || "",
85
+ href: linkMark.attrs.href,
86
+ from: pos,
87
+ to: pos + node.nodeSize,
88
+ });
89
+ }
90
+ }
91
+ });
92
+ return links;
93
+ }
94
+
95
+ // ============================================================================
96
+ // Level 1: Unit tests for findLinks() and tokenizeLink()
97
+ // ============================================================================
98
+
99
+ describe("findLinks() baseline behavior", () => {
100
+ describe("basic URL detection", () => {
101
+ it("detects https URLs", () => {
102
+ const results = findLinks("https://example.com");
103
+ expect(results).toHaveLength(1);
104
+ expect(results[0]).toMatchObject({
105
+ isLink: true,
106
+ value: "https://example.com",
107
+ href: "https://example.com",
108
+ start: 0,
109
+ end: 19,
110
+ });
111
+ });
112
+
113
+ it("detects http URLs", () => {
114
+ const results = findLinks("http://example.com");
115
+ expect(results).toHaveLength(1);
116
+ expect(results[0]).toMatchObject({
117
+ isLink: true,
118
+ value: "http://example.com",
119
+ href: "http://example.com",
120
+ });
121
+ });
122
+
123
+ it("detects schemeless URLs and prepends default protocol", () => {
124
+ const results = findLinks("example.com");
125
+ expect(results).toHaveLength(1);
126
+ expect(results[0]).toMatchObject({
127
+ isLink: true,
128
+ value: "example.com",
129
+ href: "http://example.com",
130
+ start: 0,
131
+ end: 11,
132
+ });
133
+ });
134
+
135
+ it("respects defaultProtocol option", () => {
136
+ const results = findLinks("example.com", { defaultProtocol: "https" });
137
+ expect(results).toHaveLength(1);
138
+ expect(results[0].href).toBe("https://example.com");
139
+ });
140
+
141
+ it("detects www URLs", () => {
142
+ const results = findLinks("www.example.com");
143
+ expect(results).toHaveLength(1);
144
+ expect(results[0]).toMatchObject({
145
+ isLink: true,
146
+ value: "www.example.com",
147
+ href: "http://www.example.com",
148
+ });
149
+ });
150
+ });
151
+
152
+ describe("multiple URLs in text", () => {
153
+ it("finds multiple URLs with correct positions", () => {
154
+ const results = findLinks("Visit https://a.com and https://b.com");
155
+ expect(results).toHaveLength(2);
156
+ expect(results[0]).toMatchObject({
157
+ value: "https://a.com",
158
+ start: 6,
159
+ end: 19,
160
+ });
161
+ expect(results[1]).toMatchObject({
162
+ value: "https://b.com",
163
+ start: 24,
164
+ end: 37,
165
+ });
166
+ });
167
+
168
+ it("finds multiple schemeless URLs", () => {
169
+ const results = findLinks("Check example.com or test.org");
170
+ expect(results).toHaveLength(2);
171
+ expect(results[0].value).toBe("example.com");
172
+ expect(results[1].value).toBe("test.org");
173
+ });
174
+ });
175
+
176
+ describe("URLs with paths, queries, and fragments", () => {
177
+ it("includes full path", () => {
178
+ const results = findLinks("https://example.com/path/to/page");
179
+ expect(results).toHaveLength(1);
180
+ expect(results[0].value).toBe("https://example.com/path/to/page");
181
+ });
182
+
183
+ it("includes query string", () => {
184
+ const results = findLinks("https://example.com?q=hello&b=world");
185
+ expect(results).toHaveLength(1);
186
+ expect(results[0].value).toBe("https://example.com?q=hello&b=world");
187
+ });
188
+
189
+ it("includes fragment", () => {
190
+ const results = findLinks("https://example.com#section");
191
+ expect(results).toHaveLength(1);
192
+ expect(results[0].value).toBe("https://example.com#section");
193
+ });
194
+
195
+ it("includes path + query + fragment", () => {
196
+ const results = findLinks("https://example.com/path?q=1#frag");
197
+ expect(results).toHaveLength(1);
198
+ expect(results[0].value).toBe("https://example.com/path?q=1#frag");
199
+ });
200
+
201
+ it("includes encoded characters", () => {
202
+ const results = findLinks("https://example.com/path%20name");
203
+ expect(results).toHaveLength(1);
204
+ expect(results[0].value).toBe("https://example.com/path%20name");
205
+ });
206
+
207
+ it("includes trailing slash", () => {
208
+ const results = findLinks("https://example.com/");
209
+ expect(results).toHaveLength(1);
210
+ expect(results[0].value).toBe("https://example.com/");
211
+ });
212
+ });
213
+
214
+ describe("URLs with ports", () => {
215
+ it("detects URL with port", () => {
216
+ const results = findLinks("https://example.com:8080");
217
+ expect(results).toHaveLength(1);
218
+ expect(results[0].value).toBe("https://example.com:8080");
219
+ });
220
+
221
+ it("detects schemeless URL with port and path", () => {
222
+ const results = findLinks("example.com:3000/path");
223
+ expect(results).toHaveLength(1);
224
+ expect(results[0]).toMatchObject({
225
+ value: "example.com:3000/path",
226
+ href: "http://example.com:3000/path",
227
+ });
228
+ });
229
+ });
230
+
231
+ describe("trailing punctuation handling", () => {
232
+ it("excludes trailing period", () => {
233
+ const results = findLinks("Visit https://example.com.");
234
+ expect(results).toHaveLength(1);
235
+ expect(results[0].value).toBe("https://example.com");
236
+ });
237
+
238
+ it("excludes trailing comma", () => {
239
+ const results = findLinks("See https://example.com, and more");
240
+ expect(results).toHaveLength(1);
241
+ expect(results[0].value).toBe("https://example.com");
242
+ });
243
+
244
+ it("excludes surrounding parentheses", () => {
245
+ const results = findLinks("(https://example.com)");
246
+ expect(results).toHaveLength(1);
247
+ expect(results[0]).toMatchObject({
248
+ value: "https://example.com",
249
+ start: 1,
250
+ end: 20,
251
+ });
252
+ });
253
+
254
+ it("keeps balanced parentheses in path (Wikipedia-style)", () => {
255
+ const results = findLinks(
256
+ "https://en.wikipedia.org/wiki/Foo_(bar)"
257
+ );
258
+ expect(results).toHaveLength(1);
259
+ expect(results[0].value).toBe(
260
+ "https://en.wikipedia.org/wiki/Foo_(bar)"
261
+ );
262
+ });
263
+ });
264
+
265
+ describe("non-links", () => {
266
+ it("returns empty for plain text", () => {
267
+ expect(findLinks("not a link")).toHaveLength(0);
268
+ });
269
+
270
+ it("returns empty for single word", () => {
271
+ expect(findLinks("hello")).toHaveLength(0);
272
+ });
273
+
274
+ it("returns empty for empty string", () => {
275
+ expect(findLinks("")).toHaveLength(0);
276
+ });
277
+
278
+ it("returns empty for just a protocol", () => {
279
+ expect(findLinks("https://")).toHaveLength(0);
280
+ });
281
+
282
+ it("does not detect bare IP addresses", () => {
283
+ expect(findLinks("192.168.1.1")).toHaveLength(0);
284
+ });
285
+ });
286
+
287
+ describe("domain variations", () => {
288
+ it("detects hyphenated domains", () => {
289
+ const results = findLinks("my-site.example.com");
290
+ expect(results).toHaveLength(1);
291
+ expect(results[0].value).toBe("my-site.example.com");
292
+ });
293
+
294
+ it("detects subdomains", () => {
295
+ const results = findLinks("sub.domain.example.com");
296
+ expect(results).toHaveLength(1);
297
+ expect(results[0].value).toBe("sub.domain.example.com");
298
+ });
299
+ });
300
+
301
+ describe("URL position in text", () => {
302
+ it("detects URL at end of text", () => {
303
+ const results = findLinks("go to example.com");
304
+ expect(results).toHaveLength(1);
305
+ expect(results[0]).toMatchObject({
306
+ value: "example.com",
307
+ start: 6,
308
+ end: 17,
309
+ });
310
+ });
311
+
312
+ it("detects URL at start of text", () => {
313
+ const results = findLinks("example.com is great");
314
+ expect(results).toHaveLength(1);
315
+ expect(results[0]).toMatchObject({
316
+ value: "example.com",
317
+ start: 0,
318
+ end: 11,
319
+ });
320
+ });
321
+ });
322
+
323
+ describe("protocol variations", () => {
324
+ it("detects ftp URLs", () => {
325
+ const results = findLinks("ftp://files.example.com");
326
+ expect(results).toHaveLength(1);
327
+ expect(results[0].value).toBe("ftp://files.example.com");
328
+ });
329
+
330
+ it("detects mailto URLs", () => {
331
+ const results = findLinks("mailto:user@example.com");
332
+ expect(results).toHaveLength(1);
333
+ expect(results[0]).toMatchObject({
334
+ value: "mailto:user@example.com",
335
+ href: "mailto:user@example.com",
336
+ });
337
+ });
338
+
339
+ it("detects bare email addresses as links", () => {
340
+ const results = findLinks("user@example.com");
341
+ expect(results).toHaveLength(1);
342
+ expect(results[0]).toMatchObject({
343
+ isLink: true,
344
+ value: "user@example.com",
345
+ href: "mailto:user@example.com",
346
+ });
347
+ });
348
+ });
349
+
350
+ describe("boundary handling", () => {
351
+ it("stops at whitespace", () => {
352
+ const results = findLinks("https://example.com/path with spaces");
353
+ expect(results).toHaveLength(1);
354
+ expect(results[0].value).toBe("https://example.com/path");
355
+ });
356
+ });
357
+ });
358
+
359
+ describe("tokenizeLink() baseline behavior", () => {
360
+ describe("single valid links", () => {
361
+ it("tokenizes schemeless URL as single link token", () => {
362
+ const tokens = tokenizeToObjects("example.com");
363
+ expect(tokens).toHaveLength(1);
364
+ expect(tokens[0]).toMatchObject({
365
+ isLink: true,
366
+ value: "example.com",
367
+ href: "http://example.com",
368
+ start: 0,
369
+ end: 11,
370
+ });
371
+ });
372
+
373
+ it("tokenizes https URL as single link token", () => {
374
+ const tokens = tokenizeToObjects("https://example.com");
375
+ expect(tokens).toHaveLength(1);
376
+ expect(tokens[0]).toMatchObject({
377
+ isLink: true,
378
+ value: "https://example.com",
379
+ href: "https://example.com",
380
+ });
381
+ });
382
+
383
+ it("tokenizes URL with path as single link token", () => {
384
+ const tokens = tokenizeToObjects("example.com/path");
385
+ expect(tokens).toHaveLength(1);
386
+ expect(tokens[0]).toMatchObject({
387
+ isLink: true,
388
+ value: "example.com/path",
389
+ href: "http://example.com/path",
390
+ });
391
+ });
392
+
393
+ it("tokenizes www URL as single link token", () => {
394
+ const tokens = tokenizeToObjects("www.example.com");
395
+ expect(tokens).toHaveLength(1);
396
+ expect(tokens[0]).toMatchObject({
397
+ isLink: true,
398
+ value: "www.example.com",
399
+ href: "http://www.example.com",
400
+ });
401
+ });
402
+
403
+ it("tokenizes URL with https and path as single link token", () => {
404
+ const tokens = tokenizeToObjects("https://example.com/path");
405
+ expect(tokens).toHaveLength(1);
406
+ expect(tokens[0]).toMatchObject({
407
+ isLink: true,
408
+ value: "https://example.com/path",
409
+ href: "https://example.com/path",
410
+ });
411
+ });
412
+
413
+ it("tokenizes short TLD (2 chars) as link", () => {
414
+ const tokens = tokenizeToObjects("test.co");
415
+ expect(tokens).toHaveLength(1);
416
+ expect(tokens[0].isLink).toBe(true);
417
+ });
418
+ });
419
+
420
+ describe("bracket-wrapped links", () => {
421
+ it("tokenizes (url) as 3 tokens with link in middle", () => {
422
+ const tokens = tokenizeToObjects("(example.com)");
423
+ expect(tokens).toHaveLength(3);
424
+ expect(tokens[0]).toMatchObject({
425
+ isLink: false,
426
+ value: "(",
427
+ start: 0,
428
+ end: 1,
429
+ });
430
+ expect(tokens[1]).toMatchObject({
431
+ isLink: true,
432
+ value: "example.com",
433
+ href: "http://example.com",
434
+ start: 1,
435
+ end: 12,
436
+ });
437
+ expect(tokens[2]).toMatchObject({
438
+ isLink: false,
439
+ value: ")",
440
+ start: 12,
441
+ end: 13,
442
+ });
443
+ });
444
+
445
+ it("tokenizes [url] as 3 tokens with link in middle", () => {
446
+ const tokens = tokenizeToObjects("[example.com]");
447
+ expect(tokens).toHaveLength(3);
448
+ expect(tokens[0]).toMatchObject({ isLink: false, value: "[" });
449
+ expect(tokens[1]).toMatchObject({
450
+ isLink: true,
451
+ value: "example.com",
452
+ });
453
+ expect(tokens[2]).toMatchObject({ isLink: false, value: "]" });
454
+ });
455
+
456
+ it("tokenizes (https://url) as 3 tokens", () => {
457
+ const tokens = tokenizeToObjects("(https://example.com)");
458
+ expect(tokens).toHaveLength(3);
459
+ expect(tokens[0]).toMatchObject({ isLink: false, value: "(" });
460
+ expect(tokens[1]).toMatchObject({
461
+ isLink: true,
462
+ value: "https://example.com",
463
+ href: "https://example.com",
464
+ });
465
+ expect(tokens[2]).toMatchObject({ isLink: false, value: ")" });
466
+ });
467
+ });
468
+
469
+ describe("non-links", () => {
470
+ it("tokenizes plain word as non-link", () => {
471
+ const tokens = tokenizeToObjects("notaurl");
472
+ expect(tokens).toHaveLength(1);
473
+ expect(tokens[0].isLink).toBe(false);
474
+ });
475
+
476
+ it("tokenizes domain with trailing number as non-link", () => {
477
+ // This is a key behavior: example.com1 is NOT a valid link
478
+ // because the TLD is "com1" which is not valid
479
+ const tokens = tokenizeToObjects("example.com1");
480
+ expect(tokens).toHaveLength(1);
481
+ expect(tokens[0].isLink).toBe(false);
482
+ });
483
+
484
+ it("tokenizes single-char TLD as non-link", () => {
485
+ const tokens = tokenizeToObjects("test.x");
486
+ expect(tokens).toHaveLength(1);
487
+ expect(tokens[0].isLink).toBe(false);
488
+ });
489
+
490
+ it("tokenizes single-char hostname as non-link", () => {
491
+ const tokens = tokenizeToObjects("a.bc");
492
+ expect(tokens).toHaveLength(1);
493
+ expect(tokens[0].isLink).toBe(false);
494
+ });
495
+ });
496
+
497
+ describe("edge cases", () => {
498
+ it("tokenizes IP address as non-link", () => {
499
+ const tokens = tokenizeToObjects("192.168.1.1");
500
+ expect(tokens).toHaveLength(1);
501
+ expect(tokens[0].isLink).toBe(false);
502
+ });
503
+
504
+ it("tokenizes localhost as link (filtered downstream by shouldAutoLink)", () => {
505
+ const tokens = tokenizeToObjects("localhost");
506
+ expect(tokens).toHaveLength(1);
507
+ expect(tokens[0].isLink).toBe(true);
508
+ });
509
+
510
+ it("tokenizes url with trailing dot as url + dot tokens", () => {
511
+ const tokens = tokenizeToObjects("example.com.");
512
+ expect(tokens).toHaveLength(2);
513
+ expect(tokens[0]).toMatchObject({
514
+ isLink: true,
515
+ value: "example.com",
516
+ });
517
+ expect(tokens[1]).toMatchObject({
518
+ isLink: false,
519
+ value: ".",
520
+ });
521
+ });
522
+
523
+ it("tokenizes {url} as 3 tokens (curly braces)", () => {
524
+ const tokens = tokenizeToObjects("{example.com}");
525
+ expect(tokens).toHaveLength(3);
526
+ expect(tokens[0]).toMatchObject({ isLink: false, value: "{" });
527
+ expect(tokens[1]).toMatchObject({ isLink: true, value: "example.com" });
528
+ expect(tokens[2]).toMatchObject({ isLink: false, value: "}" });
529
+ });
530
+
531
+ it("respects defaultProtocol parameter", () => {
532
+ const tokens = tokenizeToObjects("example.com", "https");
533
+ expect(tokens[0].href).toBe("https://example.com");
534
+ });
535
+ });
536
+ });
537
+
538
+ describe("isValidLinkStructure baseline", () => {
539
+ it("accepts single link token", () => {
540
+ const tokens = tokenizeToObjects("example.com");
541
+ expect(isValidLinkStructure(tokens)).toBe(true);
542
+ });
543
+
544
+ it("accepts link wrapped in parentheses", () => {
545
+ const tokens = tokenizeToObjects("(example.com)");
546
+ expect(isValidLinkStructure(tokens)).toBe(true);
547
+ });
548
+
549
+ it("accepts link wrapped in square brackets", () => {
550
+ const tokens = tokenizeToObjects("[example.com]");
551
+ expect(isValidLinkStructure(tokens)).toBe(true);
552
+ });
553
+
554
+ it("rejects link wrapped in curly braces", () => {
555
+ // {url} tokenizes to 3 tokens but {} is not in the accepted list
556
+ const tokens = tokenizeToObjects("{example.com}");
557
+ expect(isValidLinkStructure(tokens)).toBe(false);
558
+ });
559
+
560
+ it("rejects non-link single token", () => {
561
+ const tokens = tokenizeToObjects("notaurl");
562
+ expect(isValidLinkStructure(tokens)).toBe(false);
563
+ });
564
+
565
+ it("rejects url with trailing dot (2 tokens)", () => {
566
+ const tokens = tokenizeToObjects("example.com.");
567
+ expect(isValidLinkStructure(tokens)).toBe(false);
568
+ });
569
+
570
+ it("rejects example.com1 (invalid TLD)", () => {
571
+ const tokens = tokenizeToObjects("example.com1");
572
+ expect(isValidLinkStructure(tokens)).toBe(false);
573
+ });
574
+ });
575
+
576
+ // ============================================================================
577
+ // Level 2: Integration tests through the editor
578
+ // ============================================================================
579
+
580
+ describe("Link extension autolink behavior", () => {
581
+ let editor: BlockNoteEditor<any, any, any>;
582
+
583
+ afterEach(() => {
584
+ if (editor) {
585
+ editor._tiptapEditor.destroy();
586
+ }
587
+ });
588
+
589
+ function setupEditorWithBlock(content = "") {
590
+ editor = createEditor();
591
+ editor.replaceBlocks(editor.document, [
592
+ {
593
+ id: "test-block",
594
+ type: "paragraph",
595
+ content: content || undefined,
596
+ },
597
+ ]);
598
+ return editor;
599
+ }
600
+
601
+ describe("should autolink", () => {
602
+ it("autolinks https URL when followed by space", () => {
603
+ setupEditorWithBlock();
604
+ const links = typeTextThenSpace(editor, "test-block", "https://example.com");
605
+ expect(links).toHaveLength(1);
606
+ expect(links[0].href).toBe("https://example.com");
607
+ expect(links[0].text).toBe("https://example.com");
608
+ });
609
+
610
+ it("autolinks http URL when followed by space", () => {
611
+ setupEditorWithBlock();
612
+ const links = typeTextThenSpace(editor, "test-block", "http://example.com");
613
+ expect(links).toHaveLength(1);
614
+ expect(links[0].href).toBe("http://example.com");
615
+ });
616
+
617
+ it("autolinks schemeless URL with default protocol (https in BlockNote)", () => {
618
+ setupEditorWithBlock();
619
+ const links = typeTextThenSpace(editor, "test-block", "example.com");
620
+ expect(links).toHaveLength(1);
621
+ // BlockNote overrides the tiptap default to "https" via DEFAULT_LINK_PROTOCOL
622
+ expect(links[0].href).toBe("https://example.com");
623
+ expect(links[0].text).toBe("example.com");
624
+ });
625
+
626
+ it("autolinks www URL", () => {
627
+ setupEditorWithBlock();
628
+ const links = typeTextThenSpace(editor, "test-block", "www.example.com");
629
+ expect(links).toHaveLength(1);
630
+ expect(links[0].href).toBe("https://www.example.com");
631
+ });
632
+
633
+ it("autolinks URL with path and query", () => {
634
+ setupEditorWithBlock();
635
+ const links = typeTextThenSpace(
636
+ editor,
637
+ "test-block",
638
+ "https://example.com/path?q=1"
639
+ );
640
+ expect(links).toHaveLength(1);
641
+ expect(links[0].href).toBe("https://example.com/path?q=1");
642
+ });
643
+ });
644
+
645
+ describe("should NOT autolink", () => {
646
+ it("does not autolink plain text", () => {
647
+ setupEditorWithBlock();
648
+ const links = typeTextThenSpace(editor, "test-block", "notaurl");
649
+ expect(links).toHaveLength(0);
650
+ });
651
+
652
+ it("does not autolink single word", () => {
653
+ setupEditorWithBlock();
654
+ const links = typeTextThenSpace(editor, "test-block", "hello");
655
+ expect(links).toHaveLength(0);
656
+ });
657
+
658
+ it("does not autolink IP address without protocol", () => {
659
+ setupEditorWithBlock();
660
+ const links = typeTextThenSpace(editor, "test-block", "192.168.1.1");
661
+ expect(links).toHaveLength(0);
662
+ });
663
+
664
+ it("does not autolink localhost (single-word hostname)", () => {
665
+ setupEditorWithBlock();
666
+ const links = typeTextThenSpace(editor, "test-block", "localhost");
667
+ expect(links).toHaveLength(0);
668
+ });
669
+
670
+ it("does not autolink domain with trailing number (invalid TLD)", () => {
671
+ setupEditorWithBlock();
672
+ const links = typeTextThenSpace(editor, "test-block", "example.com1");
673
+ expect(links).toHaveLength(0);
674
+ });
675
+ });
676
+
677
+ describe("bracket-wrapped URLs", () => {
678
+ it("autolinks URL in parentheses, linking only the URL", () => {
679
+ setupEditorWithBlock();
680
+ const links = typeTextThenSpace(
681
+ editor,
682
+ "test-block",
683
+ "(https://example.com)"
684
+ );
685
+ expect(links).toHaveLength(1);
686
+ expect(links[0].href).toBe("https://example.com");
687
+ expect(links[0].text).toBe("https://example.com");
688
+ });
689
+
690
+ it("autolinks URL in square brackets, linking only the URL", () => {
691
+ setupEditorWithBlock();
692
+ const links = typeTextThenSpace(
693
+ editor,
694
+ "test-block",
695
+ "[https://example.com]"
696
+ );
697
+ expect(links).toHaveLength(1);
698
+ expect(links[0].href).toBe("https://example.com");
699
+ expect(links[0].text).toBe("https://example.com");
700
+ });
701
+ });
702
+ });
703
+
704
+ describe("Link extension paste handler behavior", () => {
705
+ let editor: BlockNoteEditor<any, any, any>;
706
+
707
+ afterEach(() => {
708
+ if (editor) {
709
+ editor._tiptapEditor.destroy();
710
+ }
711
+ });
712
+
713
+ it("applies link mark when pasting URL over selected text", () => {
714
+ editor = createEditor();
715
+ editor.replaceBlocks(editor.document, [
716
+ {
717
+ id: "test-block",
718
+ type: "paragraph",
719
+ content: "click here",
720
+ },
721
+ ]);
722
+
723
+ // Select "click here"
724
+ editor.setTextCursorPosition("test-block", "start");
725
+ const view = editor._tiptapEditor.view;
726
+ const doc = view.state.doc;
727
+
728
+ // Find the text node position
729
+ let textStart = 0;
730
+ let textEnd = 0;
731
+ doc.descendants((node, pos) => {
732
+ if (node.isText && node.text === "click here") {
733
+ textStart = pos;
734
+ textEnd = pos + node.nodeSize;
735
+ }
736
+ });
737
+
738
+ // Create selection over the text
739
+ const tr = view.state.tr.setSelection(
740
+ TextSelection.create(view.state.doc, textStart, textEnd)
741
+ );
742
+ view.dispatch(tr);
743
+
744
+ // Create a minimal slice that looks like pasted URL text
745
+ const textNode = view.state.schema.text("https://example.com");
746
+ const slice = new Slice(Fragment.from(textNode), 0, 0);
747
+
748
+ // Dispatch paste through the editor view
749
+ const handled = view.someProp("handlePaste", (f) =>
750
+ f(view, new ClipboardEvent("paste"), slice)
751
+ );
752
+
753
+ expect(handled).toBeTruthy();
754
+ // Check that link mark was applied
755
+ const links = getLinksInDocument(editor);
756
+ expect(links).toHaveLength(1);
757
+ expect(links[0].href).toBe("https://example.com");
758
+ expect(links[0].text).toBe("click here");
759
+ });
760
+
761
+ it("does not apply link when pasting non-URL text over selection", () => {
762
+ editor = createEditor();
763
+ editor.replaceBlocks(editor.document, [
764
+ {
765
+ id: "test-block",
766
+ type: "paragraph",
767
+ content: "click here",
768
+ },
769
+ ]);
770
+
771
+ editor.setTextCursorPosition("test-block", "start");
772
+ const view = editor._tiptapEditor.view;
773
+ const doc = view.state.doc;
774
+
775
+ let textStart = 0;
776
+ let textEnd = 0;
777
+ doc.descendants((node, pos) => {
778
+ if (node.isText && node.text === "click here") {
779
+ textStart = pos;
780
+ textEnd = pos + node.nodeSize;
781
+ }
782
+ });
783
+
784
+ const tr = view.state.tr.setSelection(
785
+ TextSelection.create(view.state.doc, textStart, textEnd)
786
+ );
787
+ view.dispatch(tr);
788
+
789
+ const textNode = view.state.schema.text("not a url");
790
+ const slice = new Slice(Fragment.from(textNode), 0, 0);
791
+
792
+ const handled = view.someProp("handlePaste", (f) =>
793
+ f(view, new ClipboardEvent("paste"), slice)
794
+ );
795
+
796
+ // Should not be handled (not a URL)
797
+ expect(handled).toBeFalsy();
798
+
799
+ // No links should exist
800
+ const links = getLinksInDocument(editor);
801
+ expect(links).toHaveLength(0);
802
+ });
803
+
804
+ it("does not apply link when pasting URL with empty selection", () => {
805
+ editor = createEditor();
806
+ editor.replaceBlocks(editor.document, [
807
+ {
808
+ id: "test-block",
809
+ type: "paragraph",
810
+ content: "some text",
811
+ },
812
+ ]);
813
+
814
+ // Place cursor without selection
815
+ editor.setTextCursorPosition("test-block", "end");
816
+ const view = editor._tiptapEditor.view;
817
+
818
+ const textNode = view.state.schema.text("https://example.com");
819
+ const slice = new Slice(Fragment.from(textNode), 0, 0);
820
+
821
+ const handled = view.someProp("handlePaste", (f) =>
822
+ f(view, new ClipboardEvent("paste"), slice)
823
+ );
824
+
825
+ // Should not be handled because selection is empty
826
+ expect(handled).toBeFalsy();
827
+ });
828
+ });
829
+
830
+ describe("Link extension isValidLink option", () => {
831
+ let editor: BlockNoteEditor<any, any, any>;
832
+
833
+ afterEach(() => {
834
+ if (editor) {
835
+ editor._tiptapEditor.destroy();
836
+ }
837
+ });
838
+
839
+ it("autolink: restrictive override blocks normally-valid URLs on typing", () => {
840
+ editor = createEditor({ isValidLink: () => false });
841
+ editor.replaceBlocks(editor.document, [
842
+ {
843
+ id: "test-block",
844
+ type: "paragraph",
845
+ content: "",
846
+ },
847
+ ]);
848
+
849
+ const links = typeTextThenSpace(
850
+ editor,
851
+ "test-block",
852
+ "https://example.com"
853
+ );
854
+ expect(links).toHaveLength(0);
855
+ });
856
+
857
+ it("paste-rule: restrictive override blocks pasted URL text", () => {
858
+ editor = createEditor({ isValidLink: () => false });
859
+ editor.replaceBlocks(editor.document, [
860
+ {
861
+ id: "test-block",
862
+ type: "paragraph",
863
+ content: "some text here",
864
+ },
865
+ ]);
866
+
867
+ editor._tiptapEditor.commands.insertContent("https://example.com ");
868
+
869
+ const links = getLinksInDocument(editor);
870
+ expect(links).toHaveLength(0);
871
+ });
872
+
873
+ it("paste-handler: restrictive override blocks URL pasted over selection", () => {
874
+ editor = createEditor({ isValidLink: () => false });
875
+ editor.replaceBlocks(editor.document, [
876
+ {
877
+ id: "test-block",
878
+ type: "paragraph",
879
+ content: "click here",
880
+ },
881
+ ]);
882
+
883
+ editor.setTextCursorPosition("test-block", "start");
884
+ const view = editor._tiptapEditor.view;
885
+ const doc = view.state.doc;
886
+
887
+ let textStart = 0;
888
+ let textEnd = 0;
889
+ doc.descendants((node, pos) => {
890
+ if (node.isText && node.text === "click here") {
891
+ textStart = pos;
892
+ textEnd = pos + node.nodeSize;
893
+ }
894
+ });
895
+
896
+ const tr = view.state.tr.setSelection(
897
+ TextSelection.create(view.state.doc, textStart, textEnd)
898
+ );
899
+ view.dispatch(tr);
900
+
901
+ const textNode = view.state.schema.text("https://example.com");
902
+ const slice = new Slice(Fragment.from(textNode), 0, 0);
903
+
904
+ const handled = view.someProp("handlePaste", (f) =>
905
+ f(view, new ClipboardEvent("paste"), slice)
906
+ );
907
+
908
+ expect(handled).toBeFalsy();
909
+ const links = getLinksInDocument(editor);
910
+ expect(links).toHaveLength(0);
911
+ });
912
+
913
+ it("parseHTML: permissive override accepts custom-scheme links", () => {
914
+ editor = createEditor({
915
+ isValidLink: (href) => isAllowedUri(href) || href.startsWith("myapp:"),
916
+ });
917
+ editor.pasteHTML(`<p><a href="myapp://foo">click</a></p>`);
918
+
919
+ const links = getLinksInDocument(editor);
920
+ expect(links).toHaveLength(1);
921
+ expect(links[0].href).toBe("myapp://foo");
922
+ });
923
+
924
+ it("parseHTML: default rejects unknown-scheme links", () => {
925
+ editor = createEditor();
926
+ editor.pasteHTML(`<p><a href="myapp://foo">click</a></p>`);
927
+
928
+ const links = getLinksInDocument(editor);
929
+ expect(links).toHaveLength(0);
930
+ });
931
+
932
+ it("renderHTML: permissive override preserves custom-scheme href on export", () => {
933
+ editor = createEditor({
934
+ isValidLink: (href) => isAllowedUri(href) || href.startsWith("myapp:"),
935
+ });
936
+ editor.pasteHTML(`<p><a href="myapp://foo">click</a></p>`);
937
+
938
+ const html = editor.blocksToFullHTML(editor.document);
939
+ expect(html).toContain('href="myapp://foo"');
940
+ });
941
+ });