generated from nisabmohd/Aria-Docs
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathllms-text.ts
More file actions
98 lines (76 loc) · 2.61 KB
/
llms-text.ts
File metadata and controls
98 lines (76 loc) · 2.61 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
import {promises as fs} from 'fs';
import matter from 'gray-matter';
import {ROUTES} from './routes-config';
import {getDocsContentPath} from './docs-utils';
import {getMarkdownForSlug} from './mdx-to-markdown';
const OUTPUT_BASE = `# Zero
> Zero is a new kind of sync engine powered by queries.
`;
function normalizeSlug(href: string) {
return href.replace(/^\//, '').replace(/^docs\//, '');
}
export async function generateLlmsTxt(baseUrl: string): Promise<string> {
let output = OUTPUT_BASE;
for (const section of ROUTES) {
output += `## ${section.title}\n\n`;
if (section.items) {
for (const item of section.items) {
if (!item.href) continue;
const slug = normalizeSlug(item.href);
const url = `${baseUrl}/docs/${slug}.md`;
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 += `- [${item.title}](${url})${descSuffix}\n`;
} catch (err) {
console.warn(`warning: could not process route ${item.href}:`, err);
}
}
}
output += '\n';
}
return output;
}
export async function generateLlmsFullTxt(baseUrl: string): Promise<string> {
let output = OUTPUT_BASE;
for (const section of ROUTES) {
if (section.items) {
for (const item of section.items) {
if (!item.href) continue;
const slug = normalizeSlug(item.href);
try {
const markdown = await getMarkdownForSlug(slug);
if (!markdown) {
console.warn(`warning: no markdown generated for ${slug}`);
continue;
}
const url = `${baseUrl}/docs/${slug}.md`;
output += '---\n\n';
if (markdown.startsWith('# ')) {
const firstNewline = markdown.indexOf('\n');
const title =
firstNewline === -1 ? markdown : markdown.slice(0, firstNewline);
const rest =
firstNewline === -1
? ''
: markdown.slice(firstNewline).trimStart();
output += `${title}\n\n`;
output += `Source: ${url}\n\n`;
if (rest) {
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;
}