Skip to content

Commit c1919b4

Browse files
Add /llms.txt to docs for LLM context (#3194)
1 parent 09d19c7 commit c1919b4

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

src/pages/[...llm_slug].ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { getCollection } from 'astro:content';
2+
import type { APIRoute } from 'astro';
3+
4+
export const llmsTxtSections = [
5+
'start',
6+
'concept',
7+
'security',
8+
'develop',
9+
'distribute',
10+
'learn',
11+
'plugins',
12+
];
13+
14+
export function groupDocsByPrefix(
15+
prefixes: string[],
16+
docs: Awaited<ReturnType<typeof getCollection<'docs'>>>
17+
) {
18+
const grouped = new Map<string, typeof docs>();
19+
prefixes.forEach((prefix) => {
20+
grouped.set(
21+
prefix,
22+
docs.filter((doc) => doc.slug.startsWith(prefix))
23+
);
24+
});
25+
// sort each group by slug
26+
for (const [prefix, items] of grouped) {
27+
items.sort((a, b) => a.slug.localeCompare(b.slug));
28+
}
29+
30+
return grouped;
31+
}
32+
33+
export function toLlmsTxtPath(slug: string): string {
34+
return `${slug}/llms.txt`;
35+
}
36+
37+
export const GET: APIRoute = async ({ params, request }) => {
38+
const { llm_slug } = params;
39+
const slug = llm_slug?.replace(/\/llms\.txt$/, '');
40+
const docs = await getCollection('docs');
41+
const doc = docs.find((doc) => doc.slug === slug);
42+
if (!doc) {
43+
return new Response('Not Found', { status: 404 });
44+
}
45+
46+
const content = `# ${doc.data.title}\n\n${doc.body}\n\n`;
47+
48+
return new Response(content, {
49+
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
50+
});
51+
};
52+
53+
export async function getStaticPaths() {
54+
const docs = await getCollection('docs');
55+
const docsBySection = groupDocsByPrefix(llmsTxtSections, docs);
56+
const paths = Array.from(docsBySection.values())
57+
.map((docs) => docs.map((doc) => doc.slug))
58+
.flat()
59+
.map((slug) => ({ params: { llm_slug: toLlmsTxtPath(slug) } }));
60+
61+
return [...paths];
62+
}

src/pages/llms.toc.txt.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { getCollection } from 'astro:content';
2+
import type { APIRoute } from 'astro';
3+
4+
function getHeaderLevel(slug: string): number {
5+
return slug.split('/').length + 1;
6+
}
7+
function groupDocsByPrefix(docs: Awaited<ReturnType<typeof getCollection<'docs'>>>) {
8+
const prefixes = ['start', 'concept', 'security', 'develop', 'distribute', 'learn', 'plugins'];
9+
10+
const grouped = new Map<string, typeof docs>();
11+
prefixes.forEach((prefix) => {
12+
grouped.set(
13+
prefix,
14+
docs.filter((doc) => doc.slug.startsWith(prefix))
15+
);
16+
});
17+
18+
return grouped;
19+
}
20+
21+
const aboutBlurb = `Tauri is a framework for building tiny, fast binaries for all major desktop and mobile platforms. Developers can integrate any frontend framework that compiles to HTML, JavaScript, and CSS for building their user experience while leveraging languages such as Rust, Swift, and Kotlin for backend logic when needed.`;
22+
23+
const organizationBlur = `This index links to documentation that covers everything from getting started to advanced concepts, and distribution of Tauri applications.
24+
25+
The index is organized into key sections:
26+
- **start**: Information for getting up and running with Tauri, including prerequisites and installation instructions
27+
- **core concepts**: Topics that you should get more intimately familiar with if you want to get the most out of the framework.
28+
- **security**: High-level concepts and security features at the core of Tauri’s design and ecosystem that make you, your applications and your users more secure by default
29+
- **develop**: Topics pertaining to the development of Tauri applications, including how to use the Tauri API, communicating between the frontend and backend, configuration, state management, debugging and more.
30+
- **distribute**: Information on the tooling you need to distribute your application either to the platform app stores or as platform-specific installers.
31+
- **learn**: Tutorials intended to provided end-to-end learning experiences to guide you through specific Tauri topics and help you apply knowledge from the guides and reference documentation.
32+
- **plugins**: Information on the extensibility of Tauri from Built-in Tauri features and functionality to provided plugins and recipes built by the Tauri community
33+
- **about**: Various information about Tauri from governance, philosophy, and trademark guidelines.
34+
35+
Each section contains links to detailed markdown files that provide comprehensive information about Tauri's features and how to use them effectively.`;
36+
37+
export const GET: APIRoute = async ({ params, request }) => {
38+
const docs = await getCollection('docs');
39+
const grouped = groupDocsByPrefix(docs);
40+
let content = `# Tauri app Full Documentation\n\n${aboutBlurb}\n\n${organizationBlur}\n\n**Table of Contents**\n`;
41+
for (const [prefix, items] of grouped) {
42+
if (items.length > 0) {
43+
content += `\n## ${prefix.charAt(0).toUpperCase() + prefix.slice(1)}\n`;
44+
items.forEach((doc) => {
45+
const level = getHeaderLevel(doc.slug);
46+
const indent = ' '.repeat(level - 2);
47+
content += `${indent}- [${doc.data.title}](https://tauri.app/${doc.slug}/)\n`;
48+
});
49+
}
50+
}
51+
52+
return new Response(content, {
53+
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
54+
});
55+
};

src/pages/llms.txt.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getCollection } from 'astro:content';
2+
import type { APIRoute } from 'astro';
3+
import { llmsTxtSections, groupDocsByPrefix } from './[...llm_slug]';
4+
5+
const aboutBlurb = `Tauri is a framework for building tiny, fast binaries for all major desktop and mobile platforms. Developers can integrate any frontend framework that compiles to HTML, JavaScript, and CSS for building their user experience while leveraging languages such as Rust, Swift, and Kotlin for backend logic when needed.`;
6+
7+
function getHeaderLevel(slug: string): number {
8+
return slug.split('/').length + 1;
9+
}
10+
11+
export const GET: APIRoute = async ({ params, request }) => {
12+
const docs = await getCollection('docs');
13+
const docsBySection = groupDocsByPrefix(llmsTxtSections, docs);
14+
let content = `# Tauri app Full Documentation\n\n${aboutBlurb}\n\n**Table of Contents**\n`;
15+
16+
let n = 1;
17+
for (const [prefix, items] of docsBySection) {
18+
if (items.length > 0) {
19+
content += `\n${n++}. ${prefix.charAt(0).toUpperCase() + prefix.slice(1)}\n`;
20+
items.forEach((doc) => {
21+
const level = getHeaderLevel(doc.slug);
22+
const indent = ' '.repeat(level - 2);
23+
content += `${indent}- [${doc.data.title}](#${doc.slug})\n`;
24+
});
25+
}
26+
}
27+
for (const [prefix, docs] of docsBySection) {
28+
content += `\n# ${prefix.charAt(0).toUpperCase() + prefix.slice(1)}\n`;
29+
content += docs
30+
.map((doc) => {
31+
return `# ${doc.data.title}\n\n${doc.body}\n\n`;
32+
})
33+
.join('');
34+
}
35+
36+
return new Response(content, {
37+
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
38+
});
39+
};

0 commit comments

Comments
 (0)