Open History to see the last commit
1export type ComposerInlineToken =
2 | {
3 readonly type: "mention";
4 readonly value: string;
5 readonly source: string;
6 readonly start: number;
7 readonly end: number;
8 }
9 | {
10 readonly type: "skill";
11 readonly value: string;
12 readonly source: string;
13 readonly start: number;
14 readonly end: number;
15 };
16
17export interface CollectComposerInlineTokensOptions {
18 readonly preserveTrailingFrom?: ReadonlyArray<ComposerInlineToken>;
19}
20
21const SKILL_MENTION_SOURCE =
22 /(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)/u
23 .source;
24const SKILL_TOKEN_REGEX = new RegExp(`${SKILL_MENTION_SOURCE}(?=\\s)`, "gu");
25export const SKILL_MENTION_PATTERN = new RegExp(`${SKILL_MENTION_SOURCE}(?=\\s|$)`, "gu");
26const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
27/**
28 * Unbounded, every whitespace in the composer is a candidate start: the engine scans the rest
29 * of the text for a closing `]`, fails, and rescans from the next whitespace — quadratic on
30 * input like " [[[[[…". Only a basename ever survives the `label !== basename` check below, so
31 * this cannot reject a link a user could meaningfully write; the longest filename any common
32 * filesystem allows is 255.
33 */
34const MAX_FILE_LINK_LABEL_LENGTH = 512;
35const FILE_LINK_TOKEN_REGEX = new RegExp(
36 `(^|\\s)\\[((?:\\\\.|[^\\]\\\\]){0,${MAX_FILE_LINK_LABEL_LENGTH}})\\]\\(([^)\\s]+)\\)(?=\\s)`,
37 "g",
38);
39const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
40const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
41const SCOPED_PACKAGE_REFERENCE_REGEX =
42 /^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*(?:\/[^\s@"]+)*$/;
43const DECORATOR_CALL_REFERENCE_REGEX = /^[\p{ID_Start}_$][\p{ID_Continue}.$\u200c\u200d]*\(/u;
44
45function collectMentionTokens(text: string): ComposerInlineToken[] {
46 const matches: ComposerInlineToken[] = [];
47
48 for (const match of text.matchAll(FILE_LINK_TOKEN_REGEX)) {
49 const fullMatch = match[0];
50 const prefix = match[1] ?? "";
51 const label = (match[2] ?? "").replace(/\\(.)/g, "$1");
52 const encodedPath = match[3] ?? "";
53 let path = encodedPath;
54 try {
55 path = decodeURIComponent(encodedPath);
56 } catch {}
57 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
58 const basename = separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
59 const hasExternalScheme = URI_SCHEME_REGEX.test(path) && !WINDOWS_DRIVE_PATH_REGEX.test(path);
60 if (!path || hasExternalScheme || label !== basename) {
61 continue;
62 }
63 const start = (match.index ?? 0) + prefix.length;
64 const end = start + fullMatch.length - prefix.length;
65 matches.push({
66 type: "mention",
67 value: path,
68 source: text.slice(start, end),
69 start,
70 end,
71 });
72 }
73
74 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
75 const fullMatch = match[0];
76 const prefix = match[1] ?? "";
77 const quotedPath = match[2];
78 const path = quotedPath !== undefined ? quotedPath.replace(/\\(.)/g, "$1") : (match[3] ?? "");
79 if (
80 !path ||
81 (quotedPath === undefined &&
82 (SCOPED_PACKAGE_REFERENCE_REGEX.test(path) || DECORATOR_CALL_REFERENCE_REGEX.test(path)))
83 ) {
84 continue;
85 }
86 const start = (match.index ?? 0) + prefix.length;
87 const end = start + fullMatch.length - prefix.length;
88 matches.push({
89 type: "mention",
90 value: path,
91 source: text.slice(start, end),
92 start,
93 end,
94 });
95 }
96
97 return matches;
98}
99
100export function collectComposerInlineTokens(
101 text: string,
102 options: CollectComposerInlineTokensOptions = {},
103): ReadonlyArray<ComposerInlineToken> {
104 const matches = collectMentionTokens(text);
105
106 for (const match of text.matchAll(SKILL_TOKEN_REGEX)) {
107 const fullMatch = match[0];
108 const prefix = match[1] ?? "";
109 const value = match[2] ?? "";
110 if (!value) {
111 continue;
112 }
113 const start = (match.index ?? 0) + prefix.length;
114 const end = start + fullMatch.length - prefix.length;
115 matches.push({
116 type: "skill",
117 value,
118 source: text.slice(start, end),
119 start,
120 end,
121 });
122 }
123
124 for (const token of options.preserveTrailingFrom ?? []) {
125 if (
126 token.end === text.length &&
127 text.slice(token.start, token.end) === token.source &&
128 !matches.some(
129 (match) =>
130 match.type === token.type && match.start === token.start && match.end === token.end,
131 )
132 ) {
133 matches.push(token);
134 }
135 }
136
137 return [...matches].sort((left, right) => left.start - right.start);
138}