|
| 1 | +import { conceptPagePath, startPagePath } from "../lib/utils"; |
| 2 | +import { site } from "../site"; |
| 3 | +import type { APIRoute } from "astro"; |
| 4 | +import { getCollection } from "astro:content"; |
| 5 | +import Handlebars from "handlebars"; |
| 6 | +import fs from "node:fs"; |
| 7 | +import path from "node:path"; |
| 8 | + |
| 9 | +const { url: root, title, description: tagline, llms } = site; |
| 10 | + |
| 11 | +const templateFile = fs.readFileSync( |
| 12 | + path.join(process.cwd(), "src/templates/llms.txt.hbs"), |
| 13 | + "utf-8", |
| 14 | +); |
| 15 | + |
| 16 | +const template = Handlebars.compile(templateFile); |
| 17 | + |
| 18 | +export const GET: APIRoute = async () => { |
| 19 | + const startPages = await getCollection("start"); |
| 20 | + const conceptPages = await getCollection("concepts"); |
| 21 | + |
| 22 | + const content = template({ |
| 23 | + root, |
| 24 | + title, |
| 25 | + tagline, |
| 26 | + description: llms.description, |
| 27 | + startPages: startPages.map(({ data: { title }, slug }) => ({ |
| 28 | + title, |
| 29 | + href: `${root}/${startPagePath(slug)}`, |
| 30 | + })), |
| 31 | + conceptPages: conceptPages.map(({ data: { title }, slug }) => ({ |
| 32 | + title, |
| 33 | + href: `${root}/${conceptPagePath(slug)}`, |
| 34 | + })), |
| 35 | + otherFormats: [ |
| 36 | + { |
| 37 | + root, |
| 38 | + file: "llms-small.txt", |
| 39 | + description: "compact structure-only version", |
| 40 | + }, |
| 41 | + { |
| 42 | + root, |
| 43 | + file: "llms-full.txt", |
| 44 | + description: "complete content in one file", |
| 45 | + }, |
| 46 | + ], |
| 47 | + }); |
| 48 | + |
| 49 | + return new Response(content, { |
| 50 | + headers: { |
| 51 | + "Content-Type": "text/plain; charset=utf-8", |
| 52 | + }, |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +export const prerender = true; |
0 commit comments