Skip to content

Commit 8612605

Browse files
committed
minimize llms.txt
1 parent 7bb3ebd commit 8612605

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

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

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,111 @@ export function filterDocsByPackage(
192192
return filtered;
193193
}
194194

195-
export function generateLlmContent(filteredDocs: Record<string, string>, type: Package): string {
195+
interface MinimizeOptions {
196+
removeLegacy: boolean;
197+
removeNoteBlocks: boolean;
198+
removeDetailsBlocks: boolean;
199+
removePlaygroundLinks: boolean;
200+
removePrettierIgnore: boolean;
201+
normalizeWhitespace: boolean;
202+
}
203+
204+
const defaultOptions: MinimizeOptions = {
205+
removeLegacy: false,
206+
removeNoteBlocks: false,
207+
removeDetailsBlocks: false,
208+
removePlaygroundLinks: false,
209+
removePrettierIgnore: false,
210+
normalizeWhitespace: false
211+
};
212+
213+
function removeQuoteBlocks(content: string, blockType: string): string {
214+
return content
215+
.split('\n')
216+
.reduce((acc: string[], line: string, index: number, lines: string[]) => {
217+
// If we find a block (with or without additional text), skip it and all subsequent blockquote lines
218+
if (line.trim().startsWith(`> [!${blockType}]`)) {
219+
// Skip all subsequent lines that are part of the blockquote
220+
let i = index;
221+
while (i < lines.length && (lines[i].startsWith('>') || lines[i].trim() === '')) {
222+
i++;
223+
}
224+
// Update the index to skip all these lines
225+
index = i - 1;
226+
return acc;
227+
}
228+
229+
// Only add the line if it's not being skipped
230+
acc.push(line);
231+
return acc;
232+
}, [])
233+
.join('\n');
234+
}
235+
236+
function minimizeContent(content: string, options?: Partial<MinimizeOptions>): string {
237+
// Merge with defaults, but only for properties that are defined
238+
const settings: MinimizeOptions = options ? { ...defaultOptions, ...options } : defaultOptions;
239+
240+
let minimized = content;
241+
242+
if (settings.removeLegacy) {
243+
minimized = removeQuoteBlocks(minimized, 'LEGACY');
244+
}
245+
246+
if (settings.removeNoteBlocks) {
247+
minimized = removeQuoteBlocks(minimized, 'NOTE');
248+
}
249+
250+
if (settings.removeDetailsBlocks) {
251+
minimized = removeQuoteBlocks(minimized, 'DETAILS');
252+
}
253+
254+
if (settings.removePlaygroundLinks) {
255+
// Replace playground URLs with /[link] but keep the original link text
256+
minimized = minimized.replace(/\[([^\]]+)\]\(\/playground[^)]+\)/g, '[$1](/REMOVED)');
257+
}
258+
259+
if (settings.removePrettierIgnore) {
260+
minimized = minimized
261+
.split('\n')
262+
.filter((line) => line.trim() !== '<!-- prettier-ignore -->')
263+
.join('\n');
264+
}
265+
266+
if (settings.normalizeWhitespace) {
267+
minimized = minimized.replace(/\s+/g, ' ');
268+
}
269+
270+
minimized = minimized.trim();
271+
272+
return minimized;
273+
}
274+
275+
export function generateLlmContent(
276+
filteredDocs: Record<string, string>,
277+
type: Package,
278+
minimizeOptions?: Partial<MinimizeOptions>
279+
): string {
196280
let content = `<SYSTEM>${getDocumentationTitle(type)}</SYSTEM>\n\n`;
197281

198282
const paths = sortPaths(Object.keys(filteredDocs));
199283

200284
for (const path of paths) {
201285
content += `# ${path.replace('../../../content/', '')}\n\n`;
202-
content += filteredDocs[path];
286+
const docContent = minimizeOptions
287+
? minimizeContent(filteredDocs[path], minimizeOptions)
288+
: filteredDocs[path];
289+
content += docContent;
203290
content += '\n';
204291
}
205292

206293
return content;
207294
}
208295

209-
export function generateCombinedContent(documentsContent: Record<string, string>): string {
296+
export function generateCombinedContent(
297+
documentsContent: Record<string, string>,
298+
minimizeOptions?: Partial<MinimizeOptions>
299+
): string {
210300
let content = '';
211301
let currentSection = '';
212302
const paths = sortPaths(Object.keys(documentsContent));
@@ -223,7 +313,10 @@ export function generateCombinedContent(documentsContent: Record<string, string>
223313
}
224314

225315
content += `## ${path.replace('../../../content/', '')}\n\n`;
226-
content += documentsContent[path];
316+
const docContent = minimizeOptions
317+
? minimizeContent(documentsContent[path], minimizeOptions)
318+
: documentsContent[path];
319+
content += docContent;
227320
content += '\n';
228321
}
229322

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import { documentsContent, generateCombinedContent } from '$lib/server/content';
44
const PREFIX = 'This is the abridged developer documentation for Svelte and SvelteKit.';
55

66
export const GET: RequestHandler = async () => {
7-
const content = `${PREFIX}\n\n${generateCombinedContent(documentsContent)}`;
7+
const content = `${PREFIX}\n\n${generateCombinedContent(documentsContent, {
8+
removeLegacy: true,
9+
removeNoteBlocks: true,
10+
removeDetailsBlocks: true,
11+
removePlaygroundLinks: true,
12+
removePrettierIgnore: true,
13+
normalizeWhitespace: true
14+
})}`;
815

916
return new Response(content, {
1017
status: 200,

0 commit comments

Comments
 (0)