From e59c84bb2f8b7a10ab4831f6315dc66ec895456b Mon Sep 17 00:00:00 2001 From: grassblock Date: Sun, 4 May 2025 11:44:26 +0800 Subject: [PATCH] feat: text version for posts --- src/pages/post/[...slug].txt.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/pages/post/[...slug].txt.js diff --git a/src/pages/post/[...slug].txt.js b/src/pages/post/[...slug].txt.js new file mode 100644 index 0000000..8d5624f --- /dev/null +++ b/src/pages/post/[...slug].txt.js @@ -0,0 +1,25 @@ +import { getCollection } from 'astro:content'; + +export const prerender = true; +export async function getStaticPaths() { + const blogEntries = await getCollection('posts'); + return blogEntries.map(entry => ({ + params: { slug: entry.slug }, props: { entry }, + })); +} +export async function GET({ props }) { + const { entry } = props; + // Format the content as plain text + const title = entry.data.title; + const date = entry.data.pubDate.toISOString().split('T')[0]; + const content = entry.body; + + // Combine the post info and body into a single text file + const textContent = `Title: ${title}\nPublished at: ${date}\n\n${content}`; + + return new Response(textContent, { + headers: { + 'Content-Type': 'text/plain', + }, + }); +} \ No newline at end of file