diff --git a/package-lock.json b/package-lock.json index 25bb3d09e0a7b42..4d70b40a945d6a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,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", @@ -7275,6 +7276,21 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/dedent": { + "version": "1.5.3", + "resolved": "https://registry.npmjs.org/dedent/-/dedent-1.5.3.tgz", + "integrity": "sha512-NHQtfOOW68WD8lgypbLA5oT+Bt0xXJhiYvoR6SmmNXZfpzOGXwdKWmcwG8N7PwVVWV3eF/68nmD9BaJSsTBhyQ==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "babel-plugin-macros": "^3.1.0" + }, + "peerDependenciesMeta": { + "babel-plugin-macros": { + "optional": true + } + } + }, "node_modules/deep-eql": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz", diff --git a/package.json b/package.json index 12ffdd4e735e6c8..2479f7733d1667d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/pages/llms.txt.ts b/src/pages/llms.txt.ts new file mode 100644 index 000000000000000..a356baf9bac7547 --- /dev/null +++ b/src/pages/llms.txt.ts @@ -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", + }, + }); +};