|
| 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