From fe0ddb42306f6051a9354433522a63a3b9898218 Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Tue, 7 Jan 2025 11:21:21 +0000 Subject: [PATCH] [Docs Site] Improve llms-full.txt --- src/pages/[area]/llms-full.txt.ts | 56 ++++++++++++++++++++++++++++ src/pages/[product]/llms-full.txt.ts | 44 ++++++++++++++++++++++ src/pages/llms-full.txt.ts | 13 ++++++- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/pages/[area]/llms-full.txt.ts create mode 100644 src/pages/[product]/llms-full.txt.ts diff --git a/src/pages/[area]/llms-full.txt.ts b/src/pages/[area]/llms-full.txt.ts new file mode 100644 index 00000000000000..27ba71ce2ac217 --- /dev/null +++ b/src/pages/[area]/llms-full.txt.ts @@ -0,0 +1,56 @@ +import type { APIRoute } from "astro"; +import { getCollection } from "astro:content"; +import { slug } from "github-slugger"; + +export async function getStaticPaths() { + const products = await getCollection("products"); + + const areas = new Set( + products.flatMap((p) => { + if (!p.data.product.group) return []; + + return slug(p.data.product.group.toLowerCase()); + }), + ); + + return [...areas].map((area) => { + return { + params: { + area, + }, + }; + }); +} + +export const GET: APIRoute = async ({ params }) => { + const products = await getCollection("products", (p) => { + if (!p.data.product.group) return false; + + return slug(p.data.product.group.toLowerCase()) === params.area; + }); + + const markdown = await getCollection("docs", (e) => { + if (!e.body) return false; + + return products.some((p) => + e.slug.startsWith(p.data.product.url.slice(1, -1)), + ); + }) + .then((entries) => + entries.map((entry) => { + return [ + `# ${entry.data.title}`, + `URL: https://developers.cloudflare.com/${entry.slug}/`, + `${entry.body.trim()}`, + "---", + ].join("\n\n"); + }), + ) + .then((array) => array.join("\n\n")); + + return new Response(markdown, { + headers: { + "content-type": "text/plain", + }, + }); +}; diff --git a/src/pages/[product]/llms-full.txt.ts b/src/pages/[product]/llms-full.txt.ts new file mode 100644 index 00000000000000..effc040c78ac05 --- /dev/null +++ b/src/pages/[product]/llms-full.txt.ts @@ -0,0 +1,44 @@ +import type { APIRoute } from "astro"; +import { getCollection } from "astro:content"; + +export async function getStaticPaths() { + const products = await getCollection("products", (p) => { + return p.data.product.group?.toLowerCase() === "developer platform"; + }); + + return products.map((entry) => { + return { + params: { + product: entry.id, + }, + props: { + product: entry, + }, + }; + }); +} + +export const GET: APIRoute = async ({ props }) => { + const markdown = await getCollection("docs", (e) => { + return ( + e.slug.startsWith(props.product.data.product.url.slice(1, -1)) && e.body + ); + }) + .then((entries) => + entries.map((entry) => { + return [ + `# ${entry.data.title}`, + `URL: https://developers.cloudflare.com/${entry.slug}/`, + `${entry.body.trim()}`, + "---", + ].join("\n\n"); + }), + ) + .then((array) => array.join("\n\n")); + + return new Response(markdown, { + headers: { + "content-type": "text/plain", + }, + }); +}; diff --git a/src/pages/llms-full.txt.ts b/src/pages/llms-full.txt.ts index 18dbe023a0e23b..074ab95e1d713b 100644 --- a/src/pages/llms-full.txt.ts +++ b/src/pages/llms-full.txt.ts @@ -2,8 +2,17 @@ import type { APIRoute } from "astro"; import { getCollection } from "astro:content"; export const GET: APIRoute = async () => { - const markdown = await getCollection("docs") - .then((entries) => entries.map((entry) => entry.body)) + const markdown = await getCollection("docs", (e) => e.body) + .then((entries) => + entries.map((entry) => { + return [ + `# ${entry.data.title}`, + `URL: https://developers.cloudflare.com/${entry.slug}/`, + `${entry.body.trim()}`, + "---", + ].join("\n\n"); + }), + ) .then((array) => array.join("\n\n")); return new Response(markdown, {