forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx.ts
More file actions
68 lines (59 loc) · 1.82 KB
/
Copy pathmdx.ts
File metadata and controls
68 lines (59 loc) · 1.82 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
import { promises as fs } from "fs";
import { join, resolve } from "path";
import { glob } from "glob";
type AnnotationType = "hook" | "component" | "class";
export interface AnnotatedDoc {
path: string;
comment: string;
type: AnnotationType;
name: string;
sourcePath: string;
}
export async function getAnnotatedMdxDocs(directory: string): Promise<AnnotatedDoc[]> {
const fullPath = resolve(directory);
const pattern = join(fullPath, "**/*.mdx");
const files = await glob(pattern);
const annotations: AnnotatedDoc[] = [];
for (const file of files) {
const content = await fs.readFile(file, "utf8");
// Regular expression to find the specific comment format
const commentRegex = /{\/\*\s*GENERATE-DOCS\s*(.*?)\s*\*\/}/g;
let match;
while ((match = commentRegex.exec(content)) !== null) {
const details = parseKeyValuePairs(match[1].trim());
const sourcePath = details["path"];
let type: AnnotationType | undefined;
let name: string | undefined;
for (const key of ["hook", "component", "class"]) {
if (details[key]) {
type = key as AnnotationType;
name = details[key];
break;
}
}
if (type && name && sourcePath) {
const annotatedDoc: AnnotatedDoc = {
path: file,
comment: match[1].trim(),
type,
name,
sourcePath,
};
annotations.push(annotatedDoc);
}
}
}
return annotations;
}
function parseKeyValuePairs(input: string): Record<string, string> {
return input.split(/\s+/).reduce(
(acc, current) => {
const [key, value] = current.split("=");
if (key && value) {
acc[key] = value.replace(/^['"]|['"]$/g, ""); // Remove quotes around the value
}
return acc;
},
{} as Record<string, string>,
);
}