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_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;
22const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
23const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;
24const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
25const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
26
27function collectMentionTokens(text: string): ComposerInlineToken[] {
28 const matches: ComposerInlineToken[] = [];
29
30 for (const match of text.matchAll(FILE_LINK_TOKEN_REGEX)) {
31 const fullMatch = match[0];
32 const prefix = match[1] ?? "";
33 const label = (match[2] ?? "").replace(/\\(.)/g, "$1");
34 const encodedPath = match[3] ?? "";
35 let path = encodedPath;
36 try {
37 path = decodeURIComponent(encodedPath);
38 } catch {
39 // Preserve malformed source rather than dropping a user-authored token.
40 }
41 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
42 const basename = separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
43 const hasExternalScheme = URI_SCHEME_REGEX.test(path) && !WINDOWS_DRIVE_PATH_REGEX.test(path);
44 if (!path || hasExternalScheme || label !== basename) {
45 continue;
46 }
47 const start = (match.index ?? 0) + prefix.length;
48 const end = start + fullMatch.length - prefix.length;
49 matches.push({
50 type: "mention",
51 value: path,
52 source: text.slice(start, end),
53 start,
54 end,
55 });
56 }
57
58 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
59 const fullMatch = match[0];
60 const prefix = match[1] ?? "";
61 const quotedPath = match[2];
62 const path = quotedPath !== undefined ? quotedPath.replace(/\\(.)/g, "$1") : (match[3] ?? "");
63 if (!path) {
64 continue;
65 }
66 const start = (match.index ?? 0) + prefix.length;
67 const end = start + fullMatch.length - prefix.length;
68 matches.push({
69 type: "mention",
70 value: path,
71 source: text.slice(start, end),
72 start,
73 end,
74 });
75 }
76
77 return matches;
78}
79
80export function collectComposerInlineTokens(
81 text: string,
82 options: CollectComposerInlineTokensOptions = {},
83): ReadonlyArray<ComposerInlineToken> {
84 const matches = collectMentionTokens(text);
85
86 for (const match of text.matchAll(SKILL_TOKEN_REGEX)) {
87 const fullMatch = match[0];
88 const prefix = match[1] ?? "";
89 const value = match[2] ?? "";
90 if (!value) {
91 continue;
92 }
93 const start = (match.index ?? 0) + prefix.length;
94 const end = start + fullMatch.length - prefix.length;
95 matches.push({
96 type: "skill",
97 value,
98 source: text.slice(start, end),
99 start,
100 end,
101 });
102 }
103
104 for (const token of options.preserveTrailingFrom ?? []) {
105 if (
106 token.end === text.length &&
107 text.slice(token.start, token.end) === token.source &&
108 !matches.some(
109 (match) =>
110 match.type === token.type && match.start === token.start && match.end === token.end,
111 )
112 ) {
113 matches.push(token);
114 }
115 }
116
117 return [...matches].sort((left, right) => left.start - right.start);
118}