Skip to content

Commit

Permalink
[Docs Site] Add llms.txt (#19015)
Browse files Browse the repository at this point in the history
  • Loading branch information
KianNH authored Jan 6, 2025
1 parent 68162a2 commit 27db2b9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"astro-icon": "^1.1.2",
"astro-live-code": "^0.0.4",
"date-fns": "^4.1.0",
"dedent": "^1.5.3",
"detype": "1.0.12",
"dompurify": "3.2.3",
"dot-prop": "^9.0.0",
Expand Down
62 changes: 62 additions & 0 deletions src/pages/llms.txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";
import dedent from "dedent";

export const GET: APIRoute = async () => {
const products = await getCollection("products", (p) => {
return p.data.product.group?.toLowerCase() === "developer platform";
});

const docs = await getCollection("docs", (e) => {
return products.some((p) =>
e.slug.startsWith(p.data.product.url.slice(1, -1)),
);
});

const grouped = Object.entries(
Object.groupBy(docs, (e) => {
const product = products.find((p) =>
e.slug.startsWith(p.data.product.url.slice(1, -1)),
);

if (!product) throw new Error(`Unable to find product for ${e.slug}`);

return product.data.product.title;
}),
);

const markdown = dedent(`
# Cloudflare Developer Documentation
Easily build and deploy full-stack applications everywhere,
thanks to integrated compute, storage, and networking.
${grouped
.map(([product, entries]) => {
return dedent(`
## ${product}
${entries
?.map((e) => {
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.slug}/)`;
const description = e.data.description;
if (description) {
return line.concat(`: ${description}`);
}
return line;
})
.join("\n")}
`);
})
.join("\n\n")}
`);

return new Response(markdown, {
headers: {
"content-type": "text/plain",
},
});
};

0 comments on commit 27db2b9

Please sign in to comment.