@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.
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +3 -3
- package/dist/{blocks-DYl7UTS4.js → blocks-CSfJen16.js} +18 -2
- package/dist/blocks-CSfJen16.js.map +1 -0
- package/dist/{blocks-BEvIOzln.cjs → blocks-_lpGWKOa.cjs} +3 -3
- package/dist/blocks-_lpGWKOa.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +1 -1
- package/dist/comments.cjs +1 -1
- package/dist/comments.cjs.map +1 -1
- package/dist/comments.js +2 -2
- package/dist/comments.js.map +1 -1
- package/dist/extensions-BfYaEv6F.cjs +2 -0
- package/dist/extensions-BfYaEv6F.cjs.map +1 -0
- package/dist/{extensions-3nf9PPhA.js → extensions-C3nguvVr.js} +240 -248
- package/dist/extensions-C3nguvVr.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +2 -2
- package/dist/src-ChTwrMcE.cjs +5 -0
- package/dist/src-ChTwrMcE.cjs.map +1 -0
- package/dist/{src-Hjv1f5lc.js → src-DQfz4zLM.js} +757 -373
- package/dist/src-DQfz4zLM.js.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +1 -1
- package/package.json +6 -8
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +14 -1
- package/src/blocks/Quote/block.ts +9 -0
- package/src/blocks/Table/block.ts +26 -0
- package/src/comments/extension.ts +5 -0
- package/src/comments/threadstore/yjs/YjsThreadStore.test.ts +3 -2
- package/src/comments/threadstore/yjs/YjsThreadStore.ts +4 -4
- package/src/editor/BlockNoteEditor.ts +84 -5
- package/src/editor/editor.css +5 -0
- package/src/editor/managers/ExtensionManager/extensions.ts +8 -26
- package/src/editor/managers/StyleManager.ts +90 -10
- package/src/extensions/FormattingToolbar/FormattingToolbar.ts +23 -16
- package/src/extensions/LinkToolbar/LinkToolbar.ts +30 -75
- package/src/extensions/Placeholder/Placeholder.ts +2 -2
- package/src/extensions/tiptap-extensions/Link/helpers/autolink.ts +179 -0
- package/src/extensions/tiptap-extensions/Link/helpers/clickHandler.ts +82 -0
- package/src/extensions/tiptap-extensions/Link/helpers/linkDetector.ts +414 -0
- package/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.ts +53 -0
- package/src/extensions/tiptap-extensions/Link/helpers/tlds.ts +7 -0
- package/src/extensions/tiptap-extensions/Link/helpers/whitespace.ts +13 -0
- package/src/extensions/tiptap-extensions/Link/index.ts +2 -0
- package/src/extensions/tiptap-extensions/Link/link.test.ts +941 -0
- package/src/extensions/tiptap-extensions/Link/link.ts +207 -0
- package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +3 -5
- package/src/schema/inlineContent/types.ts +1 -1
- package/types/src/editor/BlockNoteEditor.d.ts +69 -3
- package/types/src/editor/managers/StyleManager.d.ts +22 -0
- package/types/src/extensions/LinkToolbar/LinkToolbar.d.ts +28 -7
- package/types/src/extensions/tiptap-extensions/Link/helpers/autolink.d.ts +13 -0
- package/types/src/extensions/tiptap-extensions/Link/helpers/clickHandler.d.ts +12 -0
- package/types/src/extensions/tiptap-extensions/Link/helpers/linkDetector.d.ts +32 -0
- package/types/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.d.ts +12 -0
- package/types/src/extensions/tiptap-extensions/Link/helpers/tlds.d.ts +1 -0
- package/types/src/extensions/tiptap-extensions/Link/helpers/whitespace.d.ts +4 -0
- package/types/src/extensions/tiptap-extensions/Link/index.d.ts +2 -0
- package/types/src/extensions/tiptap-extensions/Link/link.d.ts +13 -0
- package/types/src/extensions/tiptap-extensions/Link/link.test.d.ts +1 -0
- package/types/src/schema/inlineContent/types.d.ts +1 -2
- package/dist/blocks-BEvIOzln.cjs.map +0 -1
- package/dist/blocks-DYl7UTS4.js.map +0 -1
- package/dist/extensions-3nf9PPhA.js.map +0 -1
- package/dist/extensions-D9y6evn6.cjs +0 -2
- package/dist/extensions-D9y6evn6.cjs.map +0 -1
- package/dist/src-BKJH7Jla.cjs +0 -5
- package/dist/src-BKJH7Jla.cjs.map +0 -1
- package/dist/src-Hjv1f5lc.js.map +0 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Editor } from "@tiptap/core";
|
|
2
|
+
import { getAttributes } from "@tiptap/core";
|
|
3
|
+
import type { MarkType } from "@tiptap/pm/model";
|
|
4
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
5
|
+
import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
|
|
6
|
+
|
|
7
|
+
type ClickHandlerOptions = {
|
|
8
|
+
type: MarkType;
|
|
9
|
+
tiptapEditor: Editor;
|
|
10
|
+
editor?: BlockNoteEditor<any, any, any>;
|
|
11
|
+
onClick?: (
|
|
12
|
+
event: MouseEvent,
|
|
13
|
+
editor: BlockNoteEditor<any, any, any>,
|
|
14
|
+
) => boolean | void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function clickHandler(options: ClickHandlerOptions): Plugin {
|
|
18
|
+
return new Plugin({
|
|
19
|
+
key: new PluginKey("handleClickLink"),
|
|
20
|
+
props: {
|
|
21
|
+
handleClick: (view, _pos, event) => {
|
|
22
|
+
if (event.button !== 0) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!view.editable) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let link: HTMLAnchorElement | null = null;
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
event.target instanceof HTMLAnchorElement &&
|
|
34
|
+
// Differentiate between link inline content and read-only links.
|
|
35
|
+
event.target.getAttribute("data-inline-content-type") === "link"
|
|
36
|
+
) {
|
|
37
|
+
link = event.target;
|
|
38
|
+
} else {
|
|
39
|
+
const target = event.target as HTMLElement | null;
|
|
40
|
+
if (!target) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const root = options.tiptapEditor.view.dom;
|
|
45
|
+
|
|
46
|
+
// Intentionally limit the lookup to the editor root.
|
|
47
|
+
// Using tag names like DIV as boundaries breaks with custom NodeViews,
|
|
48
|
+
link = target.closest<HTMLAnchorElement>(
|
|
49
|
+
'a[data-inline-content-type="link"]',
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
if (link && !root.contains(link)) {
|
|
53
|
+
link = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!link) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (options.onClick) {
|
|
62
|
+
if (!options.editor) {
|
|
63
|
+
throw new Error("BlockNoteEditor not found in Link click handler");
|
|
64
|
+
}
|
|
65
|
+
const result = options.onClick(event, options.editor);
|
|
66
|
+
return result ?? true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const attrs = getAttributes(view.state, options.type.name);
|
|
70
|
+
const href = link.href ?? attrs.href;
|
|
71
|
+
const target = link.target ?? attrs.target;
|
|
72
|
+
|
|
73
|
+
if (href) {
|
|
74
|
+
window.open(href, target);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return false;
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight URL detection module replacing linkifyjs.
|
|
3
|
+
*
|
|
4
|
+
* Provides two functions:
|
|
5
|
+
* - findLinks(): find all URLs/emails in arbitrary text (replaces linkifyjs find())
|
|
6
|
+
* - tokenizeLink(): tokenize a single word for autolink validation (replaces linkifyjs tokenize())
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ENCODED_TLDS } from "./tlds.js";
|
|
10
|
+
|
|
11
|
+
export interface LinkMatch {
|
|
12
|
+
type: string;
|
|
13
|
+
value: string;
|
|
14
|
+
isLink: boolean;
|
|
15
|
+
href: string;
|
|
16
|
+
start: number;
|
|
17
|
+
end: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// TLD set – used only for schemeless URL validation.
|
|
22
|
+
// Protocol URLs (http://, https://, etc.) skip TLD checks.
|
|
23
|
+
// Decoded once at module load from the trie-encoded IANA list in tlds.ts.
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
function decodeTlds(encoded: string): string[] {
|
|
27
|
+
const words: string[] = [];
|
|
28
|
+
const stack: string[] = [];
|
|
29
|
+
let i = 0;
|
|
30
|
+
while (i < encoded.length) {
|
|
31
|
+
let popDigitCount = 0;
|
|
32
|
+
while (
|
|
33
|
+
i + popDigitCount < encoded.length &&
|
|
34
|
+
encoded.charCodeAt(i + popDigitCount) >= 48 &&
|
|
35
|
+
encoded.charCodeAt(i + popDigitCount) <= 57
|
|
36
|
+
) {
|
|
37
|
+
popDigitCount++;
|
|
38
|
+
}
|
|
39
|
+
if (popDigitCount > 0) {
|
|
40
|
+
words.push(stack.join(""));
|
|
41
|
+
let popCount = parseInt(encoded.substring(i, i + popDigitCount), 10);
|
|
42
|
+
while (popCount-- > 0) {
|
|
43
|
+
stack.pop();
|
|
44
|
+
}
|
|
45
|
+
i += popDigitCount;
|
|
46
|
+
} else {
|
|
47
|
+
stack.push(encoded[i]);
|
|
48
|
+
i++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return words;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const TLD_SET = new Set(decodeTlds(ENCODED_TLDS));
|
|
55
|
+
|
|
56
|
+
// Special hostnames recognized without a TLD
|
|
57
|
+
const SPECIAL_HOSTS = new Set(["localhost"]);
|
|
58
|
+
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
// Regex building blocks
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
// Characters that are unlikely to be part of a URL when they appear at the end
|
|
64
|
+
const TRAILING_PUNCT = /[.,;:!?"']+$/;
|
|
65
|
+
|
|
66
|
+
// Protocol URLs: http:// https:// ftp:// ftps://
|
|
67
|
+
const PROTOCOL_RE =
|
|
68
|
+
/(?:https?|ftp|ftps):\/\/[^\s]+/g;
|
|
69
|
+
|
|
70
|
+
// Mailto URLs: mailto:...
|
|
71
|
+
const MAILTO_RE = /mailto:[^\s]+/g;
|
|
72
|
+
|
|
73
|
+
// Bare email addresses: user@domain.tld
|
|
74
|
+
const EMAIL_RE =
|
|
75
|
+
/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}/g;
|
|
76
|
+
|
|
77
|
+
// Schemeless URLs: domain.tld with optional port and path
|
|
78
|
+
// Hostname: one or more labels separated by dots, TLD is alpha-only 2+ chars
|
|
79
|
+
const SCHEMELESS_RE =
|
|
80
|
+
/(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(?::\d{1,5})?(?:[/?#][^\s]*)?/g;
|
|
81
|
+
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
// Post-processing helpers
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Trim trailing punctuation and unbalanced closing brackets from a URL match.
|
|
88
|
+
*/
|
|
89
|
+
function trimTrailing(value: string): string {
|
|
90
|
+
let v = value;
|
|
91
|
+
|
|
92
|
+
// Iteratively trim trailing punctuation and unbalanced brackets
|
|
93
|
+
let changed = true;
|
|
94
|
+
while (changed) {
|
|
95
|
+
changed = false;
|
|
96
|
+
|
|
97
|
+
// Trim trailing punctuation chars
|
|
98
|
+
const before = v;
|
|
99
|
+
v = v.replace(TRAILING_PUNCT, "");
|
|
100
|
+
if (v !== before) {
|
|
101
|
+
changed = true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Trim unbalanced closing brackets from the end
|
|
105
|
+
for (const [open, close] of [
|
|
106
|
+
["(", ")"],
|
|
107
|
+
["[", "]"],
|
|
108
|
+
] as const) {
|
|
109
|
+
while (v.endsWith(close)) {
|
|
110
|
+
const openCount = countChar(v, open);
|
|
111
|
+
const closeCount = countChar(v, close);
|
|
112
|
+
if (closeCount > openCount) {
|
|
113
|
+
v = v.slice(0, -1);
|
|
114
|
+
changed = true;
|
|
115
|
+
} else {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return v;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function countChar(str: string, ch: string): number {
|
|
126
|
+
let count = 0;
|
|
127
|
+
for (let i = 0; i < str.length; i++) {
|
|
128
|
+
if (str[i] === ch) {
|
|
129
|
+
count++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return count;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extract the TLD from a hostname string.
|
|
137
|
+
* Returns the last dot-separated segment.
|
|
138
|
+
*/
|
|
139
|
+
function extractTld(hostname: string): string {
|
|
140
|
+
const parts = hostname.split(".");
|
|
141
|
+
return parts[parts.length - 1].toLowerCase();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function isValidTld(hostname: string): boolean {
|
|
145
|
+
const tld = extractTld(hostname);
|
|
146
|
+
return TLD_SET.has(tld);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Build the href for a URL value, prepending the default protocol if needed.
|
|
151
|
+
*/
|
|
152
|
+
function buildHref(
|
|
153
|
+
value: string,
|
|
154
|
+
type: string,
|
|
155
|
+
defaultProtocol: string
|
|
156
|
+
): string {
|
|
157
|
+
if (type === "email") {
|
|
158
|
+
return "mailto:" + value;
|
|
159
|
+
}
|
|
160
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(value) || /^mailto:/i.test(value)) {
|
|
161
|
+
// Already has a protocol
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
return defaultProtocol + "://" + value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
// findLinks()
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
export interface FindOptions {
|
|
172
|
+
defaultProtocol?: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface RawMatch {
|
|
176
|
+
type: string;
|
|
177
|
+
value: string;
|
|
178
|
+
start: number;
|
|
179
|
+
end: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Find all URLs and email addresses in the given text.
|
|
184
|
+
* Drop-in replacement for linkifyjs find().
|
|
185
|
+
*/
|
|
186
|
+
export function findLinks(
|
|
187
|
+
text: string,
|
|
188
|
+
options?: FindOptions
|
|
189
|
+
): LinkMatch[] {
|
|
190
|
+
if (!text) {
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const defaultProtocol = options?.defaultProtocol || "http";
|
|
195
|
+
const rawMatches: RawMatch[] = [];
|
|
196
|
+
|
|
197
|
+
// 1. Protocol URLs
|
|
198
|
+
for (const m of text.matchAll(PROTOCOL_RE)) {
|
|
199
|
+
rawMatches.push({
|
|
200
|
+
type: "url",
|
|
201
|
+
value: m[0],
|
|
202
|
+
start: m.index!,
|
|
203
|
+
end: m.index! + m[0].length,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 2. Mailto URLs
|
|
208
|
+
for (const m of text.matchAll(MAILTO_RE)) {
|
|
209
|
+
rawMatches.push({
|
|
210
|
+
type: "url",
|
|
211
|
+
value: m[0],
|
|
212
|
+
start: m.index!,
|
|
213
|
+
end: m.index! + m[0].length,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 3. Bare email addresses
|
|
218
|
+
for (const m of text.matchAll(EMAIL_RE)) {
|
|
219
|
+
rawMatches.push({
|
|
220
|
+
type: "email",
|
|
221
|
+
value: m[0],
|
|
222
|
+
start: m.index!,
|
|
223
|
+
end: m.index! + m[0].length,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 4. Schemeless URLs
|
|
228
|
+
for (const m of text.matchAll(SCHEMELESS_RE)) {
|
|
229
|
+
rawMatches.push({
|
|
230
|
+
type: "url",
|
|
231
|
+
value: m[0],
|
|
232
|
+
start: m.index!,
|
|
233
|
+
end: m.index! + m[0].length,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Sort by start position
|
|
238
|
+
rawMatches.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
239
|
+
|
|
240
|
+
// Deduplicate overlapping matches (prefer earlier & longer)
|
|
241
|
+
const deduped: RawMatch[] = [];
|
|
242
|
+
let lastEnd = -1;
|
|
243
|
+
for (const match of rawMatches) {
|
|
244
|
+
if (match.start >= lastEnd) {
|
|
245
|
+
deduped.push(match);
|
|
246
|
+
lastEnd = match.end;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Post-process each match
|
|
251
|
+
const results: LinkMatch[] = [];
|
|
252
|
+
for (const raw of deduped) {
|
|
253
|
+
const value = trimTrailing(raw.value);
|
|
254
|
+
if (!value) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const start = raw.start;
|
|
259
|
+
const end = start + value.length;
|
|
260
|
+
|
|
261
|
+
// For schemeless URLs, validate TLD
|
|
262
|
+
if (raw.type === "url" && !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(value)) {
|
|
263
|
+
const hostname = new URL("http://" + value).hostname;
|
|
264
|
+
if (!isValidTld(hostname)) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// For emails, validate TLD
|
|
270
|
+
if (raw.type === "email") {
|
|
271
|
+
const hostname = value.split("@")[1];
|
|
272
|
+
if (!isValidTld(hostname)) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const href = buildHref(value, raw.type, defaultProtocol);
|
|
278
|
+
|
|
279
|
+
results.push({
|
|
280
|
+
type: raw.type,
|
|
281
|
+
value,
|
|
282
|
+
isLink: true,
|
|
283
|
+
href,
|
|
284
|
+
start,
|
|
285
|
+
end,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return results;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
// tokenizeLink()
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Tokenize a single word for autolink validation.
|
|
298
|
+
* Drop-in replacement for: tokenize(word).map(t => t.toObject(defaultProtocol))
|
|
299
|
+
*
|
|
300
|
+
* Returns an array of LinkMatch tokens. The autolink code checks:
|
|
301
|
+
* - 1 token with isLink=true → valid single link
|
|
302
|
+
* - 3 tokens with middle isLink=true and outer brackets → valid wrapped link
|
|
303
|
+
*/
|
|
304
|
+
export function tokenizeLink(
|
|
305
|
+
text: string,
|
|
306
|
+
defaultProtocol = "http"
|
|
307
|
+
): LinkMatch[] {
|
|
308
|
+
if (!text) {
|
|
309
|
+
return [nonLinkToken(text, 0, 0)];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Check for bracket wrapping: (url), [url], {url}
|
|
313
|
+
const brackets: Array<[string, string]> = [
|
|
314
|
+
["(", ")"],
|
|
315
|
+
["[", "]"],
|
|
316
|
+
["{", "}"],
|
|
317
|
+
];
|
|
318
|
+
for (const [open, close] of brackets) {
|
|
319
|
+
if (text.startsWith(open) && text.endsWith(close) && text.length > 2) {
|
|
320
|
+
const inner = text.slice(1, -1);
|
|
321
|
+
if (isSingleUrl(inner)) {
|
|
322
|
+
return [
|
|
323
|
+
nonLinkToken(open, 0, 1),
|
|
324
|
+
linkToken(inner, 1, 1 + inner.length, defaultProtocol),
|
|
325
|
+
nonLinkToken(close, 1 + inner.length, text.length),
|
|
326
|
+
];
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Check for trailing punctuation (e.g., "example.com." → link + dot)
|
|
332
|
+
if (text.endsWith(".") && text.length > 1) {
|
|
333
|
+
const withoutDot = text.slice(0, -1);
|
|
334
|
+
if (isSingleUrl(withoutDot)) {
|
|
335
|
+
return [
|
|
336
|
+
linkToken(withoutDot, 0, withoutDot.length, defaultProtocol),
|
|
337
|
+
nonLinkToken(".", withoutDot.length, text.length),
|
|
338
|
+
];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Check if the whole text is a single URL
|
|
343
|
+
if (isSingleUrl(text)) {
|
|
344
|
+
return [linkToken(text, 0, text.length, defaultProtocol)];
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Not a link
|
|
348
|
+
return [nonLinkToken(text, 0, text.length)];
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Check if a string is a single complete URL (no extra chars).
|
|
353
|
+
*/
|
|
354
|
+
function isSingleUrl(text: string): boolean {
|
|
355
|
+
// Protocol URLs
|
|
356
|
+
if (/^(?:https?|ftp|ftps):\/\/[^\s]+$/.test(text)) {
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Mailto URLs
|
|
361
|
+
if (/^mailto:[^\s]+$/.test(text)) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Special hosts (e.g., localhost)
|
|
366
|
+
if (SPECIAL_HOSTS.has(text.toLowerCase())) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Schemeless URLs: hostname.tld with optional port and path
|
|
371
|
+
const schemelessFull =
|
|
372
|
+
/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+([a-zA-Z]{2,})(?::\d{1,5})?(?:[/?#][^\s]*)?$/;
|
|
373
|
+
const match = text.match(schemelessFull);
|
|
374
|
+
if (match) {
|
|
375
|
+
const tld = match[1].toLowerCase();
|
|
376
|
+
// TLD must be a-z only (no numbers) and recognized
|
|
377
|
+
if (TLD_SET.has(tld)) {
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function linkToken(
|
|
386
|
+
value: string,
|
|
387
|
+
start: number,
|
|
388
|
+
end: number,
|
|
389
|
+
defaultProtocol: string
|
|
390
|
+
): LinkMatch {
|
|
391
|
+
const type =
|
|
392
|
+
value.includes("@") && !value.includes("://") && !value.startsWith("mailto:")
|
|
393
|
+
? "email"
|
|
394
|
+
: "url";
|
|
395
|
+
return {
|
|
396
|
+
type,
|
|
397
|
+
value,
|
|
398
|
+
isLink: true,
|
|
399
|
+
href: buildHref(value, type, defaultProtocol),
|
|
400
|
+
start,
|
|
401
|
+
end,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function nonLinkToken(value: string, start: number, end: number): LinkMatch {
|
|
406
|
+
return {
|
|
407
|
+
type: "text",
|
|
408
|
+
value,
|
|
409
|
+
isLink: false,
|
|
410
|
+
href: value,
|
|
411
|
+
start,
|
|
412
|
+
end,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Editor } from "@tiptap/core";
|
|
2
|
+
import type { MarkType } from "@tiptap/pm/model";
|
|
3
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
4
|
+
import { findLinks } from "./linkDetector.js";
|
|
5
|
+
|
|
6
|
+
type PasteHandlerOptions = {
|
|
7
|
+
editor: Editor;
|
|
8
|
+
defaultProtocol: string;
|
|
9
|
+
type: MarkType;
|
|
10
|
+
shouldAutoLink?: (url: string) => boolean;
|
|
11
|
+
isValidLink: (href: string) => boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function pasteHandler(options: PasteHandlerOptions): Plugin {
|
|
15
|
+
return new Plugin({
|
|
16
|
+
key: new PluginKey("handlePasteLink"),
|
|
17
|
+
props: {
|
|
18
|
+
handlePaste: (view, _event, slice) => {
|
|
19
|
+
const { shouldAutoLink, isValidLink } = options;
|
|
20
|
+
const { state } = view;
|
|
21
|
+
const { selection } = state;
|
|
22
|
+
const { empty } = selection;
|
|
23
|
+
|
|
24
|
+
if (empty) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let textContent = "";
|
|
29
|
+
|
|
30
|
+
slice.content.forEach((node) => {
|
|
31
|
+
textContent += node.textContent;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const link = findLinks(textContent, {
|
|
35
|
+
defaultProtocol: options.defaultProtocol,
|
|
36
|
+
}).find((item) => item.isLink && item.value === textContent);
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
!textContent ||
|
|
40
|
+
!link ||
|
|
41
|
+
!isValidLink(link.value) ||
|
|
42
|
+
(shouldAutoLink !== undefined && !shouldAutoLink(link.value))
|
|
43
|
+
) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return options.editor.commands.setMark(options.type, {
|
|
48
|
+
href: link.href,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// THIS FILE IS AUTO-GENERATED. DO NOT EDIT DIRECTLY.
|
|
2
|
+
// Source: https://data.iana.org/TLD/tlds-alpha-by-domain.txt
|
|
3
|
+
// Regenerate with: pnpm --filter @blocknote/core update-tlds
|
|
4
|
+
// Encoding format ported from linkifyjs (MIT) — trie collapsed into ASCII.
|
|
5
|
+
|
|
6
|
+
export const ENCODED_TLDS =
|
|
7
|
+
"aaa1rp3bb0ott3vie4c1le2ogado5udhabi7c0ademy5centure6ountant0s9o1tor4d0s1ult4e0g1ro2tna4f0l1rica5g0akhan5ency5i0g1rbus3force5tel5kdn3l0ibaba4pay4lfinanz6state5y2sace3tom5m0azon4ericanexpress7family11x2fam3ica3sterdam8nalytics7droid5quan4z2o0l2partments8p0le4q0uarelle8r0ab1mco4chi3my2pa2t0e3s0da2ia2sociates9t0hleta5torney7u0ction5di0ble3o3spost5thor3o0s4w0s2x0a2z0ure5ba0by2idu3namex4d1k2r0celona5laycard4s5efoot5gains6seball5ketball8uhaus5yern5b0c1t1va3cg1n2d1e0ats2uty4er2rlin4st0buy5t2f1g1h0arti5i0ble3d1ke2ng0o3o1z2j1lack0friday9ockbuster8g1omberg7ue3m0s1w2n0pparibas9o0ats3ehringer8fa2m1nd2o0k0ing5sch2tik2on4t1utique6x2r0adesco6idgestone9oadway5ker3ther5ussels7s1t1uild0ers6siness6y1zz3v1w1y1z0h3ca0b1fe2l0l1vinklein9m0era3p2non3petown5ital0one8r0avan4ds2e0er0s4s2sa1e1h1ino4t0ering5holic7ba1n1re3c1d1enter4o1rn3f0a1d2g1h0anel2nel4rity4se2t2eap3intai5ristmas6ome4urch5i0priani6rcle4sco3tadel4i0c2y3k1l0aims4eaning6ick2nic1que6othing5ud3ub0med6m1n1o0ach3des3ffee4llege4ogne5m0mbank4unity6pany2re3uter5sec4ndos3struction8ulting7tact3ractors9oking4l1p2rsica5untry4pon0s4rses6pa2r0edit0card4union9icket5own3s1uise0s6u0isinella9v1w1x1y0mru3ou3z2dad1nce3ta1e1ing3sun4y2clk3ds2e0al0er2s3gree4livery5l1oitte5ta3mocrat6ntal2ist5si0gn4v2hl2iamonds6et2gital5rect0ory7scount3ver5h2y2j1k1m1np2o0cs1tor4g1mains5t1wnload7rive4tv2ubai3pont4rban5vag2r2z2earth3t2c0o2deka3u0cation8e1g1mail3erck5nergy4gineer0ing9terprises10pson4quipment8r0icsson6ni3s0q1tate5t1u0rovision8s2vents5xchange6pert3osed4ress5traspace10fage2il1rwinds6th3mily4n0s2rm0ers5shion4t3edex3edback6rrari3ero6i0delity5o2lm2nal1nce1ial7re0stone6mdale6sh0ing5t0ness6j1k1lickr3ghts4r2orist4wers5y2m1o0o0d1tball6rd1ex2sale4um3undation8x2r0ee1senius7l1ogans4ntier7tr2ujitsu5n0d2rniture7tbol5yi3ga0l0lery3o1up4me0s3p1rden4y2b0iz3d0n2e0a1nt0ing5orge5f1g0ee3h1i0ft0s3ves2ing5l0ass3e1obal2o4m0ail3bh2o1x2n1odaddy5ld0point6f2odyear5g0le4p1t1v2p1q1r0ainger5phics5tis4een3ipe3ocery4up4s1t1u0cci3ge2ide2tars5ru3w1y2hair2mburg5ngout5us3bo2dfc0bank7ealth0care8lp1sinki6re1mes5iphop4samitsu7tachi5v2k0t2m1n1ockey4ldings5iday5medepot5goods5s0ense7nda3rse3spital5t0ing5t0els3mail5use3w2r1sbc3t1u0ghes5yatt3undai7ibm2cbc2e1u2d1e0ee3fm2kano4l1m0amat4db2mo0bilien9n0c1dustries8finiti5o2g1k1stitute6urance4e4t0ernational10uit4vestments10o1piranga7q1r0ish4s0maili5t0anbul7t0au2v3jaguar4va3cb2e0ep2tzt3welry6io2ll2m0p2nj2o0bs1urg4t1y2p0morgan6rs3uegos4niper7kaufen5ddi3e0rryhotels6properties14fh2g1h1i0a1ds2m1ndle4tchen5wi3m1n1oeln3matsu5sher5p0mg2n2r0d1ed3uokgroup8w1y0oto4z2la0caixa5mborghini8er3nd0rover6xess5salle5t0ino3robe5w0yer5b1c1ds2ease3clerc5frak4gal2o2xus4gbt3i0dl2fe0insurance9style7ghting6ke2lly3mited4o2ncoln4k2ve1ing5k1lc1p2oan0s3cker3us3l1ndon4tte1o3ve3pl0financial11r1s1t0d0a3u0ndbeck6xe1ury5v1y2ma0drid4if1son4keup4n0agement7go3p1rket0ing3s4riott5shalls7ttel5ba2c0kinsey7d1e0d0ia3et2lbourne7me1orial6n0u2rck0msd7g1h1iami3crosoft7l1ni1t2t0subishi9k1l0b1s2m0a2n1o0bi0le4da2e1i1m1nash3ey2ster5rmon3tgage6scow4to0rcycles9v0ie4p1q1r1s0d2t0n1r2u0seum3ic4v1w1x1y1z2na0b1goya4me2vy3ba2c1e0c1t0bank4flix4work5ustar5w0s2xt0direct7us4f0l2g0o2hk2i0co2ke1on3nja3ssan1y5l1o0kia3rton4w0ruz3tv4p1r0a1w2tt2u1yc2z2obi1server7ffice5kinawa6layan0group9lo3m0ega4ne1g1l0ine5oo2pen3racle3nge4g0anic5igins6saka4tsuka4t2vh3pa0ge2nasonic7ris2s1tners4s1y3y2ccw3e0t2f0izer5g1h0armacy6d1ilips5one2to0graphy6s4ysio5ics1tet2ures6d1n0g1k2oneer5zza4k1l0ace2y0station9umbing5s3m1n0c2ohl2ker3litie5rn2st3r0axi3ess3ime3o0d0uctions8f1gressive8mo2perties3y5tection8u0dential9s1t1ub2w0c2y2qa1pon3uebec3st5racing4dio4e0ad1lestate6tor2y4cipes5d0umbrella9hab3ise0n3t2liance6n0t0als5pair3ort3ublican8st0aurant8view0s5xroth6ich0ardli6oh3l1o1p2o0cks3deo3gers4om3s0vp3u0gby3hr2n2w0e2yukyu6sa0arland6fe0ty4kura4le1on3msclub4ung5ndvik0coromant12ofi4p1rl2s1ve2xo3b0i1s2c0b1haeffler7midt4olarships8ol3ule3warz5ience5ot3d1e0arch3t2cure1ity6ek2lect4ner3rvices6ven3w1x0y3fr2g1h0angrila6rp3ell3ia1ksha5oes2p0ping5uji3w3i0lk2na1gles5te3j1k0i0n2y0pe4l0ing4m0art3ile4n0cf3o0ccer3ial4ftbank4ware6hu2lar2utions7ng1y2y2pa0ce3ort2t3r0l2s1t0ada2ples4r1tebank4farm7c0group6ockholm6rage3e3ream4udio2y3yle4u0cks3pplies3y2ort5rf1gery5zuki5v1watch4iss4x1y0dney4stems6z2tab1ipei4lk2obao4rget4tamotors6r2too4x0i3c0i2d0k2eam2ch0nology8l1masek5nnis4va3f1g1h0d1eater2re6iaa2ckets5enda4ps2res2ol4j0maxx4x2k0maxx5l1m0all4n1o0day3kyo3ols3p1ray3shiba5tal3urs3wn2yota3s3r0ade1ing4ining5vel0ers0insurance16ust3v2t1ube2i1nes3shu4v0s2w1z2ua1bank3s2g1k1nicom3versity8o2ol2ps2s1y1z2va0cations7na1guard7c1e0gas3ntures6risign5sicherung10t2g1i0ajes4deo3g1king4llas4n1p1rgin4sa1ion4va1o3laanderen9n1odka3lvo3te1ing3o2yage5u2wales2mart4ter4ng0gou5tch0es6eather0channel12bcam3er2site5d0ding5ibo2r3f1hoswho6ien2ki2lliamhill9n0dows4e1ners6me2oodside6rk0s2ld3w2s1tc1f3xbox3erox4ihuan4n2xx2yz3yachts4hoo3maxun5ndex5e1odobashi7ga2kohama6u0tube6t1un3za0ppos4ra3ero3ip2m1one3uerich6w2";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// From DOMPurify
|
|
2
|
+
// https://github.com/cure53/DOMPurify/blob/main/src/regexp.ts
|
|
3
|
+
export const UNICODE_WHITESPACE_PATTERN =
|
|
4
|
+
"[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]";
|
|
5
|
+
|
|
6
|
+
export const UNICODE_WHITESPACE_REGEX = new RegExp(UNICODE_WHITESPACE_PATTERN);
|
|
7
|
+
export const UNICODE_WHITESPACE_REGEX_END = new RegExp(
|
|
8
|
+
`${UNICODE_WHITESPACE_PATTERN}$`
|
|
9
|
+
);
|
|
10
|
+
export const UNICODE_WHITESPACE_REGEX_GLOBAL = new RegExp(
|
|
11
|
+
UNICODE_WHITESPACE_PATTERN,
|
|
12
|
+
"g"
|
|
13
|
+
);
|