diff --git a/src/pages/llms.txt.js b/src/pages/llms.txt.js new file mode 100644 index 0000000..b917536 --- /dev/null +++ b/src/pages/llms.txt.js @@ -0,0 +1,42 @@ +import { getCollection } from "astro:content"; +import { siteConfig } from "../config"; + +// Modified version that outputs markdown content +export async function GET(context) { + let baseUrl = context.site?.href || "https://terminal-blog.example.com"; + if (baseUrl.at(-1) === "/") baseUrl = baseUrl.slice(0, -1); + const markdownText = `# ${siteConfig.title} + +> ${siteConfig.description} + +${await generateMarkdownSection("posts", "Post", baseUrl)} + +${await generateMarkdownSection("pages", "Page", baseUrl)}`; + + return new Response(markdownText); +} + +async function generateMarkdownSection(collectionName, sectionTitle, baseUrl) { + const items = await getCollection(collectionName); + + // Sort posts by pubDate if available + const sortedItems = + collectionName === "posts" + ? items.sort((a, b) => (a.data.pubDate > b.data.pubDate ? -1 : 1)) + : items; + + let markdown = `## ${sectionTitle}\n`; + + for (const item of sortedItems) { + const { title, description, slug } = getMetaData(item); + markdown += `\n- [${title}](${baseUrl}/post/${slug}): ${description}`; + } + + return markdown; +} + +function getMetaData(collection) { + const { title, description } = collection.data; + const { slug } = collection; + return { title, description, slug }; +}