Skip to content

Commit bdf0381

Browse files
committed
simplify
1 parent edc5d43 commit bdf0381

File tree

5 files changed

+47
-85
lines changed

5 files changed

+47
-85
lines changed

apps/svelte.dev/src/lib/server/llms.ts

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { minimatch } from 'minimatch';
22
import { dev } from '$app/environment';
3-
import { docs, index } from './content';
3+
import { index } from './content';
44

55
interface GenerateLlmContentOptions {
66
prefix?: string;
77
ignore?: string[];
88
minimize?: Partial<MinimizeOptions>;
9-
package?: string;
9+
sections: Section[];
1010
}
1111

1212
interface MinimizeOptions {
@@ -18,6 +18,11 @@ interface MinimizeOptions {
1818
normalize_whitespace: boolean;
1919
}
2020

21+
interface Section {
22+
slug: string;
23+
title: string;
24+
}
25+
2126
const defaults: MinimizeOptions = {
2227
remove_legacy: false,
2328
remove_note_blocks: false,
@@ -27,62 +32,48 @@ const defaults: MinimizeOptions = {
2732
normalize_whitespace: false
2833
};
2934

30-
export function generate_llm_content(options: GenerateLlmContentOptions = {}): string {
35+
export function generate_llm_content(options: GenerateLlmContentOptions): string {
3136
let content = '';
3237

3338
if (options.prefix) {
3439
content = `${options.prefix}\n\n`;
3540
}
3641

37-
let current_section = '';
38-
const paths = sort_documentation_paths();
39-
40-
for (const path of paths) {
41-
if (!should_include_file_llm_docs(path, options.ignore)) continue;
42-
43-
// If a specific package is provided, only include its docs
44-
if (options.package) {
45-
if (!path.includes(`docs/${options.package}/`)) continue;
46-
} else {
47-
// For combined content, only include paths that match any package
48-
const doc_type = packages.find((p) => path.includes(`docs/${p}/`));
49-
if (!doc_type) continue;
50-
51-
const section = get_documentation_start_title(doc_type);
52-
if (section !== current_section) {
53-
if (current_section) content += '\n';
54-
content += `${section}\n\n`;
55-
current_section = section;
56-
}
42+
for (const section of options.sections) {
43+
if (options.sections.length > 1) {
44+
content += `${get_documentation_start_title(section)}\n\n`;
5745
}
5846

59-
const doc_content = options.minimize
60-
? minimize_content(index[path].body, options.minimize)
61-
: index[path].body;
62-
if (doc_content.trim() === '') continue;
47+
for (const [path, document] of Object.entries(index)) {
48+
if (!path.startsWith(`docs/${section.slug}`)) continue;
49+
if (!should_include_file_llm_docs(path, options.ignore)) continue;
50+
51+
const doc_content = options.minimize
52+
? minimize_content(document.body, options.minimize)
53+
: document.body;
54+
if (doc_content.trim() === '') continue;
6355

64-
content += `\n# ${index[path].metadata.title}\n\n`;
65-
content += doc_content;
66-
content += '\n';
56+
content += `\n# ${document.metadata.title}\n\n`;
57+
content += doc_content;
58+
content += '\n';
59+
}
6760
}
6861

6962
return content;
7063
}
7164

72-
export const packages = Object.keys(docs.topics).map((topic) => topic.split('/')[1]);
73-
74-
export const DOCUMENTATION_NAMES: Record<string, string> = {
75-
svelte: 'Svelte',
76-
kit: 'SvelteKit',
77-
cli: 'Svelte CLI'
78-
};
65+
export const sections: Section[] = [
66+
{ slug: 'svelte', title: 'Svelte' },
67+
{ slug: 'kit', title: 'SvelteKit' },
68+
{ slug: 'cli', title: 'the Svelte CLI' }
69+
];
7970

80-
export function get_documentation_title(type: string): string {
81-
return `This is the developer documentation for ${DOCUMENTATION_NAMES[type]}.`;
71+
export function get_documentation_title(section: Section): string {
72+
return `This is the developer documentation for ${section.title}.`;
8273
}
8374

84-
export function get_documentation_start_title(type: string): string {
85-
return `# Start of ${DOCUMENTATION_NAMES[type]} documentation`;
75+
export function get_documentation_start_title(section: Section): string {
76+
return `# Start of ${section.title} documentation`;
8677
}
8778

8879
function minimize_content(content: string, options?: Partial<MinimizeOptions>): string {
@@ -133,38 +124,6 @@ function should_include_file_llm_docs(path: string, ignore: string[] = []): bool
133124
return true;
134125
}
135126

136-
function get_documentation_section_priority(path: string): number {
137-
if (path.includes('docs/svelte/')) return 0;
138-
if (path.includes('docs/kit/')) return 1;
139-
if (path.includes('docs/cli/')) return 2;
140-
return 3;
141-
}
142-
143-
function sort_documentation_paths(): string[] {
144-
return Object.keys(index).sort((a, b) => {
145-
a = index[a].file;
146-
b = index[b].file;
147-
// First compare by section priority
148-
const priorityA = get_documentation_section_priority(a);
149-
const priorityB = get_documentation_section_priority(b);
150-
if (priorityA !== priorityB) return priorityA - priorityB;
151-
152-
// Get directory paths
153-
const dirA = a.split('/').slice(0, -1).join('/');
154-
const dirB = b.split('/').slice(0, -1).join('/');
155-
156-
// If in the same directory, prioritize index.md
157-
if (dirA === dirB) {
158-
if (a.endsWith('index.md')) return -1;
159-
if (b.endsWith('index.md')) return 1;
160-
return a.localeCompare(b);
161-
}
162-
163-
// Otherwise sort by directory path
164-
return dirA.localeCompare(dirB);
165-
});
166-
}
167-
168127
function remove_quote_blocks(content: string, blockType: string): string {
169128
return content
170129
.split('\n')

apps/svelte.dev/src/routes/docs/[...path]/llms.txt/+server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { error } from '@sveltejs/kit';
2-
import { generate_llm_content, get_documentation_title, packages } from '$lib/server/llms';
2+
import { generate_llm_content, get_documentation_title, sections } from '$lib/server/llms';
33

44
export const prerender = true;
55

66
export function entries() {
7-
return packages.map((type) => ({ path: type }));
7+
return sections.map((section) => ({ path: section.slug }));
88
}
99

1010
export function GET({ params }) {
1111
const pkg = params.path;
1212

13-
if (!packages.includes(pkg)) {
13+
const section = sections.find((s) => s.slug === pkg);
14+
15+
if (!section) {
1416
error(404, 'Not Found');
1517
}
1618

17-
const prefix = `<SYSTEM>${get_documentation_title(pkg)}</SYSTEM>`;
18-
const content = `${prefix}\n\n${generate_llm_content({ package: pkg })}`;
19+
const prefix = `<SYSTEM>${get_documentation_title(section)}</SYSTEM>`;
20+
const content = `${prefix}\n\n${generate_llm_content({ sections: [section] })}`;
1921

2022
return new Response(content, {
2123
status: 200,

apps/svelte.dev/src/routes/llms-full.txt/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { generate_llm_content } from '$lib/server/llms';
1+
import { generate_llm_content, sections } from '$lib/server/llms';
22

33
export const prerender = true;
44

55
export function GET() {
6-
const content = `<SYSTEM>This is the full developer documentation for Svelte and SvelteKit.</SYSTEM>\n\n${generate_llm_content()}`;
6+
const content = `<SYSTEM>This is the full developer documentation for Svelte and SvelteKit.</SYSTEM>\n\n${generate_llm_content({ sections })}`;
77

88
return new Response(content, {
99
status: 200,

apps/svelte.dev/src/routes/llms-small.txt/+server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { generate_llm_content } from '$lib/server/llms';
1+
import { generate_llm_content, sections } from '$lib/server/llms';
22

33
export function GET() {
44
const main_content = generate_llm_content({
5+
sections,
56
ignore: [
67
// Svelte ignores
78
'docs/svelte/legacy/**/*',

apps/svelte.dev/src/routes/llms.txt/+server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { get_documentation_title, packages, DOCUMENTATION_NAMES } from '$lib/server/llms';
1+
import { get_documentation_title, sections } from '$lib/server/llms';
22

33
const DOMAIN = `https://svelte.dev`;
44

55
export const prerender = true;
66

77
export function GET() {
8-
const package_docs = packages
8+
const package_docs = sections
99
.map(
10-
(pkg) =>
11-
`- [${DOCUMENTATION_NAMES[pkg]} documentation](${DOMAIN}/docs/${pkg}/llms.txt): ${get_documentation_title(pkg)}`
10+
(section) =>
11+
`- [${section.title} documentation](${DOMAIN}/docs/${section.slug}/llms.txt): ${get_documentation_title(section)}`
1212
)
1313
.join('\n');
1414

0 commit comments

Comments
 (0)