From e70d243e450bbbceba9d35c49354b9d66d8b6d83 Mon Sep 17 00:00:00 2001 From: conglinyizhi Date: Sun, 18 May 2025 11:37:18 +0800 Subject: [PATCH] feat(llms): Add Markdown format content export feature --- src/pages/llms.txt.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/pages/llms.txt.js 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 }; +}