generated from nisabmohd/Aria-Docs
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgenerate-llms.ts
More file actions
101 lines (78 loc) · 2.91 KB
/
generate-llms.ts
File metadata and controls
101 lines (78 loc) · 2.91 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
import path from 'path';
import {promises as fs} from 'fs';
import matter from 'gray-matter';
import {getMarkdownForSlug} from './mdx-to-markdown';
import {page_routes as pageRoutes} from './routes-config';
import {getBaseUrl, getDocsContentPath} from './docs-utils';
async function generateLlmsTxt(baseUrl: string): Promise<string> {
let output = OUTPUT_BASE;
for (const route of pageRoutes) {
if (!route.href) continue;
const slug = route.href.replace(/^\//, '').replace(/^docs\//, '');
const url = `${baseUrl}/docs/${slug}`;
try {
const contentPath = await getDocsContentPath(slug);
const rawMdx = await fs.readFile(contentPath, 'utf-8');
const {data} = matter(rawMdx);
const description = data.description ?? '';
const descSuffix = description ? `: ${description}` : '';
output += `- [${route.title}](${url})${descSuffix}\n`;
} catch (err) {
console.warn(`Warning: Could not process route ${route.href}:`, err);
}
}
return output;
}
async function generateLlmsFullTxt(baseUrl: string): Promise<string> {
let output = OUTPUT_BASE;
for (const route of pageRoutes) {
if (!route.href) continue;
const slug = route.href.replace(/^\//, '').replace(/^docs\//, '');
try {
const markdown = await getMarkdownForSlug(slug);
if (!markdown) {
console.warn(`Warning: No markdown generated for ${slug}`);
continue;
}
const url = `${baseUrl}/docs/${slug}`;
output += '---\n\n';
if (markdown.startsWith('# ')) {
const firstNewline = markdown.indexOf('\n');
const title = markdown.slice(0, firstNewline);
const rest = markdown.slice(firstNewline).trimStart();
output += `${title}\n\n`;
output += `Source: ${url}\n\n`;
output += `${rest}\n\n`;
} else {
output += `Source: ${url}\n\n`;
output += `${markdown}\n\n`;
}
} catch (err) {
console.warn(`Warning: Error processing ${slug}:`, err);
}
}
return output;
}
const OUTPUT_BASE = `# Zero\n\n
> Zero is a new kind of sync engine powered by queries.\n\n
## Documentation\n\n`;
async function main() {
console.log('Generating llms.txt files...');
const baseUrl = getBaseUrl();
console.log(`Using base URL: ${baseUrl}`);
try {
const llmsTxt = await generateLlmsTxt(baseUrl);
const llmsTxtPath = path.join(process.cwd(), 'public', 'llms.txt');
await fs.writeFile(llmsTxtPath, llmsTxt.trim());
console.log(`✓ Generated ${llmsTxtPath}`);
const llmsFullTxt = await generateLlmsFullTxt(baseUrl);
const llmsFullTxtPath = path.join(process.cwd(), 'public', 'llms-full.txt');
await fs.writeFile(llmsFullTxtPath, llmsFullTxt.trim());
console.log(`✓ Generated ${llmsFullTxtPath}`);
console.log('Successfully generated llms.txt files');
} catch (err) {
console.error('Error generating llms.txt files:', err);
process.exit(1);
}
}
main();