-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcontent.ts
More file actions
245 lines (220 loc) · 7.13 KB
/
Copy pathcontent.ts
File metadata and controls
245 lines (220 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { escapeXmlAttr, type UploadableSkillSource } from "@posthog/shared";
import { isUploadableSkillSource, parseXmlAttrs } from "./skillTags";
export interface MentionChip {
type:
| "file"
| "folder"
| "command"
| "error"
| "experiment"
| "insight"
| "feature_flag"
| "github_issue"
| "github_pr";
id: string;
label: string;
pastedText?: boolean;
chipId?: string;
skillPath?: string;
skillSource?: UploadableSkillSource;
skillName?: string;
}
/** Chip title shown while a pasted GitHub ref's real title is being fetched. */
export const GITHUB_REF_PLACEHOLDER_TITLE = "Loading...";
export interface FileAttachment {
id: string;
label: string;
}
export interface EditorContent {
segments: Array<
{ type: "text"; text: string } | { type: "chip"; chip: MentionChip }
>;
attachments?: FileAttachment[];
}
export function contentToPlainText(content: EditorContent): string {
return content.segments
.map((seg) => {
if (seg.type === "text") return seg.text;
const chip = seg.chip;
if (chip.type === "file" || chip.type === "folder")
return `@${chip.label}`;
if (chip.type === "command") return `/${chip.label}`;
return `@${chip.label}`;
})
.join("");
}
function isAbsolutePathLike(p: string): boolean {
return p.startsWith("/") || p.startsWith("~") || /^[A-Za-z]:[\\/]/.test(p);
}
export function contentToXml(content: EditorContent): string {
const inlineFilePaths = new Set<string>();
const parts = content.segments.map((seg) => {
if (seg.type === "text") return seg.text;
const chip = seg.chip;
const escapedId = escapeXmlAttr(chip.id);
switch (chip.type) {
case "file":
inlineFilePaths.add(chip.id);
return `<file path="${escapedId}" />`;
case "folder":
inlineFilePaths.add(chip.id);
return `<folder path="${escapedId}" />`;
case "command":
if (chip.skillPath && chip.skillSource) {
return `<skill name="${escapeXmlAttr(chip.skillName ?? chip.label)}" source="${escapeXmlAttr(chip.skillSource)}" path="${escapeXmlAttr(chip.skillPath)}" />`;
}
if (chip.id && chip.id !== chip.label && isAbsolutePathLike(chip.id)) {
return `<folder path="${escapedId}" />`;
}
return `/${chip.label}`;
case "error":
return `<error id="${escapedId}" />`;
case "experiment":
return `<experiment id="${escapedId}" />`;
case "insight":
return `<insight id="${escapedId}" />`;
case "feature_flag":
return `<feature_flag id="${escapedId}" />`;
case "github_issue":
case "github_pr": {
const labelMatch = chip.label.match(/^#(\d+)(?:\s*-\s*(.*))?$/);
const number = labelMatch?.[1] ?? "";
const rawTitle = labelMatch?.[2] ?? "";
// A chip sent before its title fetch resolves must not leak the placeholder
const title = rawTitle === GITHUB_REF_PLACEHOLDER_TITLE ? "" : rawTitle;
return `<${chip.type} number="${escapeXmlAttr(number)}" title="${escapeXmlAttr(title)}" url="${escapedId}" />`;
}
default:
return `@${chip.label}`;
}
});
// Append file tags for attachments not already referenced inline
if (content.attachments) {
for (const att of content.attachments) {
if (!inlineFilePaths.has(att.id)) {
parts.push(`<file path="${escapeXmlAttr(att.id)}" />`);
}
}
}
return parts.join("");
}
const CHIP_TAG_REGEX =
/<(file|folder|skill|error|experiment|insight|feature_flag|github_issue|github_pr)\b([^>]*?)\s*\/>/g;
export function deriveFileLabel(filePath: string): string {
const segments = filePath.split("/").filter(Boolean);
const fileName = segments.pop() ?? filePath;
const parentDir = segments.pop();
return parentDir ? `${parentDir}/${fileName}` : fileName;
}
function chipFromTag(tag: string, rawAttrs: string): MentionChip | null {
const attrs = parseXmlAttrs(rawAttrs);
switch (tag) {
case "file": {
const path = attrs.path;
if (!path) return null;
return { type: "file", id: path, label: deriveFileLabel(path) };
}
case "folder": {
const path = attrs.path;
if (!path) return null;
return { type: "folder", id: path, label: deriveFileLabel(path) };
}
case "skill": {
const path = attrs.path;
const name = attrs.name;
const source = attrs.source;
if (!path || !name || !isUploadableSkillSource(source)) {
return null;
}
return {
type: "command",
id: path,
label: name,
skillPath: path,
skillSource: source,
skillName: name,
};
}
case "error":
case "experiment":
case "insight":
case "feature_flag": {
const id = attrs.id;
if (!id) return null;
return { type: tag, id, label: id };
}
case "github_issue":
case "github_pr": {
const number = attrs.number ?? "";
const title = attrs.title ?? "";
const url = attrs.url ?? "";
if (!number && !url) return null;
const label = title ? `#${number} - ${title}` : `#${number}`;
return { type: tag, id: url, label };
}
default:
return null;
}
}
export function xmlToContent(xml: string): EditorContent {
const segments: EditorContent["segments"] = [];
let lastIndex = 0;
for (const match of xml.matchAll(CHIP_TAG_REGEX)) {
const matchIndex = match.index ?? 0;
const chip = chipFromTag(match[1], match[2] ?? "");
if (!chip) continue;
if (matchIndex > lastIndex) {
segments.push({ type: "text", text: xml.slice(lastIndex, matchIndex) });
}
segments.push({ type: "chip", chip });
lastIndex = matchIndex + match[0].length;
}
if (lastIndex < xml.length) {
segments.push({ type: "text", text: xml.slice(lastIndex) });
}
if (segments.length === 0) {
segments.push({ type: "text", text: xml });
}
return { segments };
}
export function xmlToPlainText(xml: string): string {
return contentToPlainText(xmlToContent(xml));
}
/** Wrap plain text in editor content as a single text segment, no chip parsing. */
export function textToContent(text: string): EditorContent {
return { segments: [{ type: "text", text }] };
}
export function isContentEmpty(
content: EditorContent | null | string,
): boolean {
if (!content) return true;
if (typeof content === "string") return !content.trim();
if (content.attachments && content.attachments.length > 0) return false;
if (!content.segments) return true;
return content.segments.every(
(seg) => seg.type === "text" && !seg.text.trim(),
);
}
export function extractFilePaths(content: EditorContent): string[] {
const filePaths: string[] = [];
const seen = new Set<string>();
for (const seg of content.segments) {
if (
seg.type === "chip" &&
(seg.chip.type === "file" || seg.chip.type === "folder") &&
!seen.has(seg.chip.id)
) {
seen.add(seg.chip.id);
filePaths.push(seg.chip.id);
}
}
if (content.attachments) {
for (const att of content.attachments) {
if (!seen.has(att.id)) {
seen.add(att.id);
filePaths.push(att.id);
}
}
}
return filePaths;
}