forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.ts
More file actions
339 lines (287 loc) · 9.19 KB
/
Copy pathextract.ts
File metadata and controls
339 lines (287 loc) · 9.19 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as fs from "node:fs";
import * as path from "node:path";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkMdx from "remark-mdx";
import { visit } from "unist-util-visit";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface CodeBlock {
lang: string;
title: string;
doctest: string;
code: string;
line: number;
sourceFile: string;
}
interface ManifestEntry {
id: string;
file: string;
lang: string;
category: string;
source: string;
}
interface DoctestConfig {
python?: { deps: string[] };
typescript?: { deps: string[] };
node?: { deps: string[] };
}
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
const DOCS_DIR = path.resolve(__dirname, "../../docs");
const OUTPUT_DIR = path.resolve(__dirname, "../../.doctest-output");
// ---------------------------------------------------------------------------
// AST Extraction
// ---------------------------------------------------------------------------
const parser = unified().use(remarkParse).use(remarkMdx);
/**
* Strip common leading whitespace from all lines of a code block.
* Handles indented code blocks inside JSX (Tabs, If, etc.) that
* preserve the JSX indentation in the extracted code.
*/
function stripCommonIndent(code: string): string {
const lines = code.split("\n");
const nonEmptyLines = lines.filter((l) => l.trim().length > 0);
if (nonEmptyLines.length === 0) return code;
const minIndent = Math.min(
...nonEmptyLines.map((l) => l.match(/^(\s*)/)![1].length),
);
if (minIndent === 0) return code;
return lines.map((l) => l.slice(minIndent)).join("\n");
}
/**
* Parse the meta string from a code fence to extract key-value attributes.
*
* Handles formats like:
* python title="main.py" doctest="server"
* typescript title="server.ts" doctest="component"
*/
export function parseMeta(meta: string): Record<string, string> {
const attrs: Record<string, string> = {};
// Match key="value" or key='value'
const regex = /(\w+)=["']([^"']+)["']/g;
let match: RegExpExecArray | null;
while ((match = regex.exec(meta)) !== null) {
attrs[match[1]] = match[2];
}
return attrs;
}
/**
* Extract all code blocks with a doctest attribute from an MDX file.
*/
export function extractFromMdx(
content: string,
sourceFile: string,
): CodeBlock[] {
const blocks: CodeBlock[] = [];
let tree: ReturnType<typeof parser.parse>;
try {
tree = parser.parse(content);
} catch {
// Some MDX files have JSX constructs that trip the parser.
// Fall back to a regex-based extraction for resilience.
return extractFromMdxFallback(content, sourceFile);
}
visit(tree, "code", (node: any) => {
const lang = node.lang || "";
const meta = node.meta || "";
const attrs = parseMeta(meta);
if (!attrs.doctest) return;
const line =
node.position && node.position.start ? node.position.start.line : 0;
blocks.push({
lang,
title: attrs.title || `snippet.${langToExt(lang)}`,
doctest: attrs.doctest,
code: stripCommonIndent(node.value),
line,
sourceFile,
});
});
return blocks;
}
/**
* Regex-based fallback for MDX files that trip the remark-mdx parser.
* Only extracts code blocks with doctest attributes — less precise on
* position, but sufficient for our purposes.
*/
function extractFromMdxFallback(
content: string,
sourceFile: string,
): CodeBlock[] {
const blocks: CodeBlock[] = [];
const lines = content.split("\n");
let inBlock = false;
let blockLang = "";
let blockMeta = "";
let blockLines: string[] = [];
let blockStart = 0;
for (let i = 0; i < lines.length; i++) {
const trimmed = lines[i].trimStart();
if (!inBlock && /^```(\w+)(.*)$/.test(trimmed)) {
const match = trimmed.match(/^```(\w+)(.*)$/);
if (match) {
blockLang = match[1];
blockMeta = match[2];
blockLines = [];
blockStart = i + 1;
inBlock = true;
}
} else if (inBlock && /^```\s*$/.test(trimmed)) {
const attrs = parseMeta(blockMeta);
if (attrs.doctest) {
blocks.push({
lang: blockLang,
title: attrs.title || `snippet.${langToExt(blockLang)}`,
doctest: attrs.doctest,
code: stripCommonIndent(blockLines.join("\n")),
line: blockStart,
sourceFile,
});
}
inBlock = false;
} else if (inBlock) {
blockLines.push(lines[i]);
}
}
return blocks;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function langToExt(lang: string): string {
switch (lang) {
case "python":
return "py";
case "typescript":
case "tsx":
return "ts";
case "javascript":
case "jsx":
return "js";
default:
return lang || "txt";
}
}
function slugify(filePath: string): string {
return filePath
.replace(/\.mdx$/, "")
.replace(/[/\\]/g, "-")
.replace(/[^a-zA-Z0-9-]/g, "");
}
/**
* Walk a directory tree and return all .mdx files.
*/
function findMdxFiles(dir: string): string[] {
const results: string[] = [];
function walk(current: string) {
const entries = fs.readdirSync(current, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(current, entry.name);
if (entry.isDirectory()) {
if (entry.name.startsWith(".") || entry.name === "node_modules")
continue;
walk(full);
} else if (entry.name.endsWith(".mdx")) {
results.push(full);
}
}
}
walk(dir);
return results.sort();
}
// ---------------------------------------------------------------------------
// Output generation
// ---------------------------------------------------------------------------
/**
* Group extracted blocks by page slug and title, then write to output dir.
* Blocks sharing the same title within a page are concatenated into one file.
*/
export function writeExtractedBlocks(
blocks: CodeBlock[],
outputDir: string,
docsDir: string,
): ManifestEntry[] {
const manifest: ManifestEntry[] = [];
// Group by (page slug, title)
const grouped = new Map<string, CodeBlock[]>();
for (const block of blocks) {
const rel = path.relative(docsDir, block.sourceFile);
const slug = slugify(rel);
const key = `${slug}/${block.title}`;
const existing = grouped.get(key) || [];
existing.push(block);
grouped.set(key, existing);
}
for (const [key, groupBlocks] of grouped) {
const slug = key.split("/")[0];
const title = groupBlocks[0].title;
const dir = path.join(outputDir, slug);
fs.mkdirSync(dir, { recursive: true });
// Concatenate code from all blocks sharing this title
const code = groupBlocks.map((b) => b.code).join("\n\n");
const filePath = path.join(dir, title);
fs.writeFileSync(filePath, code, "utf-8");
// Copy doctest.json sidecar if it exists
const sidecarPath = path.join(
path.dirname(groupBlocks[0].sourceFile),
"doctest.json",
);
const destSidecar = path.join(dir, "doctest.json");
if (fs.existsSync(sidecarPath) && !fs.existsSync(destSidecar)) {
fs.copyFileSync(sidecarPath, destSidecar);
}
const firstBlock = groupBlocks[0];
const relSource = path.relative(
path.resolve(docsDir, ".."),
firstBlock.sourceFile,
);
const id = `${slug}-${title.replace(/[^a-zA-Z0-9]/g, "-")}`;
manifest.push({
id,
file: `${slug}/${title}`,
lang: firstBlock.lang,
category: firstBlock.doctest,
source: `${relSource}:${firstBlock.line}`,
});
}
return manifest;
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
export function extract(
docsDir: string = DOCS_DIR,
outputDir: string = OUTPUT_DIR,
): ManifestEntry[] {
// Clean output dir
if (fs.existsSync(outputDir)) {
fs.rmSync(outputDir, { recursive: true });
}
fs.mkdirSync(outputDir, { recursive: true });
const files = findMdxFiles(docsDir);
const allBlocks: CodeBlock[] = [];
for (const file of files) {
const content = fs.readFileSync(file, "utf-8");
const blocks = extractFromMdx(content, file);
allBlocks.push(...blocks);
}
const manifest = writeExtractedBlocks(allBlocks, outputDir, docsDir);
// Write manifest
const manifestPath = path.join(outputDir, "manifest.json");
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), "utf-8");
console.log(`Extracted ${manifest.length} doctest snippet(s):`);
for (const entry of manifest) {
console.log(` ${entry.id} [${entry.category}] ${entry.source}`);
}
return manifest;
}
// ---------------------------------------------------------------------------
// CLI entry point
// ---------------------------------------------------------------------------
const isDirectRun = typeof require !== "undefined" && require.main === module;
if (isDirectRun) {
extract();
}