From c57e2140206a0072b93dc24dd1adab55464c366a Mon Sep 17 00:00:00 2001 From: grassblock Date: Tue, 13 May 2025 21:07:33 +0800 Subject: [PATCH] feat: add twtxt support --- src/pages/twtxt.txt.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/pages/twtxt.txt.js diff --git a/src/pages/twtxt.txt.js b/src/pages/twtxt.txt.js new file mode 100644 index 0000000..aa915c8 --- /dev/null +++ b/src/pages/twtxt.txt.js @@ -0,0 +1,17 @@ +import { getCollection } from 'astro:content'; + +export async function GET(context) { + const posts = await getCollection('posts'); + // Sort posts by date (newest first) + posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime()); + + const twtxt = posts.map(post => ( + `${new Date(post.data.pubDate).toISOString()}\t${post.data.title} ${context.site}post/${post.slug}` + )).join('\n'); + + return new Response(twtxt, { + headers: { + 'Content-Type': 'text/plain' + } + }); +} \ No newline at end of file