-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathbuild-llms.ts
More file actions
73 lines (60 loc) 路 2.06 KB
/
Copy pathbuild-llms.ts
File metadata and controls
73 lines (60 loc) 路 2.06 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
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import matter from 'gray-matter';
import { DOCS_DIR, SITE_DESCRIPTION, SITE_TITLE } from '../site.config.js';
import type { DocsGraph } from '../src/lib/core/types';
interface LlmsSourceDocument {
content: string;
includeInLlms: boolean;
}
function parseLlmsSource(rawMarkdown: string): LlmsSourceDocument {
const parsed = matter(rawMarkdown);
const includeInLlms = parsed.data?.llms !== false;
return {
content: parsed.content.trim(),
includeInLlms,
};
}
function buildLlmsDocument(
graph: DocsGraph,
options: { title: string; description: string },
readSourceMarkdown: (sourcePath: string) => string,
): string {
const docsByRoute = new Map(graph.docs.map((doc) => [doc.routePath, doc]));
const parts: string[] = [`# ${options.title}`, '', options.description, ''];
for (const section of graph.sections) {
const sectionParts: string[] = [];
for (const routePath of section.pages) {
const doc = docsByRoute.get(routePath);
if (!doc) {
throw new Error(`Missing docs graph node for route '${routePath}'`);
}
const source = parseLlmsSource(readSourceMarkdown(doc.sourcePath));
if (!source.includeInLlms || !source.content) {
continue;
}
sectionParts.push(`### ${doc.title}`, `Source: ${doc.routePath}`, '', source.content, '');
}
if (sectionParts.length === 0) {
continue;
}
parts.push('---', `## ${section.title}`, '', ...sectionParts);
}
return `${parts.join('\n').trimEnd()}\n`;
}
const graphPath = join(process.cwd(), '.svelte-kit/generated/docs-graph.json');
const graph = JSON.parse(readFileSync(graphPath, 'utf-8')) as DocsGraph;
const llmsDocument = buildLlmsDocument(
graph,
{
title: SITE_TITLE,
description: SITE_DESCRIPTION,
},
(sourcePath) => {
const sourceAbsolutePath = join(process.cwd(), DOCS_DIR, sourcePath);
return readFileSync(sourceAbsolutePath, 'utf-8');
},
);
const outDir = join(process.cwd(), 'build');
mkdirSync(outDir, { recursive: true });
writeFileSync(join(outDir, 'llms.txt'), llmsDocument);