From bd0b55b050a22864ccb0368b3007bd2a62c11622 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 8 Nov 2023 11:46:15 +0000 Subject: [PATCH] Readd Blog API --- src/routes/api/blog/+server.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/routes/api/blog/+server.ts diff --git a/src/routes/api/blog/+server.ts b/src/routes/api/blog/+server.ts new file mode 100644 index 0000000000..b9ae18bcda --- /dev/null +++ b/src/routes/api/blog/+server.ts @@ -0,0 +1,28 @@ +const isVercel = process.env.VERCEL === '1'; + +export const prerender = isVercel; + +const parsePosts = (posts: Record) => { + return Object.fromEntries( + Object.entries(posts).map(([key, value]) => { + return [ + key.replace('../../blog/post/', '').replace('/+page.markdoc', ''), + value.frontmatter + ]; + }) + ); +}; + +const posts = parsePosts( + import.meta.glob('../../blog/post/**/*.markdoc', { + eager: true + }) +); + +export const GET = async () => { + return new Response(JSON.stringify({ posts, total: Object.keys(posts).length }), { + headers: { + 'content-type': 'application/json;charset=UTF-8' + } + }); +}; \ No newline at end of file