Skip to content

Commit c189d05

Browse files
committed
[Docs Site] Add llms.txt
1 parent 975c63d commit c189d05

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"astro-icon": "^1.1.2",
5353
"astro-live-code": "^0.0.4",
5454
"date-fns": "^4.1.0",
55+
"dedent": "^1.5.3",
5556
"detype": "1.0.12",
5657
"dompurify": "3.2.3",
5758
"dot-prop": "^9.0.0",

src/pages/llms.txt.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { APIRoute } from "astro";
2+
import { getCollection } from "astro:content";
3+
import dedent from "dedent";
4+
5+
export const GET: APIRoute = async () => {
6+
const products = await getCollection("products", (p) => {
7+
return p.data.product.group?.toLowerCase() === "developer platform";
8+
});
9+
10+
const docs = await getCollection("docs", (e) => {
11+
return products.some((p) =>
12+
e.slug.startsWith(p.data.product.url.slice(1, -1)),
13+
);
14+
});
15+
16+
const grouped = Object.entries(
17+
Object.groupBy(docs, (e) => {
18+
const product = products.find((p) =>
19+
e.slug.startsWith(p.data.product.url.slice(1, -1)),
20+
);
21+
22+
if (!product) return;
23+
24+
return product.data.product.title;
25+
}),
26+
);
27+
28+
const markdown = dedent(`
29+
# Cloudflare Developer Documentation
30+
31+
Easily build and deploy full-stack applications everywhere,
32+
thanks to integrated compute, storage, and networking.
33+
34+
${grouped
35+
.map(([product, entries]) => {
36+
return dedent(`
37+
## ${product}
38+
39+
${entries
40+
?.map((e) => {
41+
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.slug}/)`;
42+
43+
const description = e.data.description;
44+
45+
if (description) {
46+
return line.concat(`: ${description}`);
47+
}
48+
49+
return line;
50+
})
51+
.join("\n")}
52+
`);
53+
})
54+
.join("\n\n")}
55+
`);
56+
57+
return new Response(markdown, {
58+
headers: {
59+
"content-type": "text/plain",
60+
},
61+
});
62+
};

0 commit comments

Comments
 (0)