From 74987b8408aff1409a422eb7c8da931187519dd2 Mon Sep 17 00:00:00 2001 From: matthieu42morin Date: Sun, 28 Apr 2024 01:21:34 +0200 Subject: [PATCH] New API for posts ! best way to do this. --- src/routes/api/posts/+server.ts | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/routes/api/posts/+server.ts diff --git a/src/routes/api/posts/+server.ts b/src/routes/api/posts/+server.ts new file mode 100644 index 0000000..0b7ac9b --- /dev/null +++ b/src/routes/api/posts/+server.ts @@ -0,0 +1,95 @@ +import { json } from '@sveltejs/kit'; +import type { Post } from '$lib/types/post'; +import type { MdsvexImport } from '$lib/types/post'; +import { error } from '@sveltejs/kit'; +import readingTime from 'reading-time'; +import { getImageUrl } from '$lib/utils/images'; +import type { RequestHandler } from './$types'; + +const getPosts = async () => { + let posts: Post[] = []; + + const paths = import.meta.glob('$content/blog/*.md', { + eager: true + }); + + for (const path in paths) { + const file = paths[path]; + const slug = path.split('/').at(-1)?.replace('.md', ''); + + if (file && typeof file === 'object' && 'metadata' in file && slug) { + const metadata = file.metadata as Omit; + const { imagePublicId } = metadata; + + const post = { + ...metadata, + slug, + readingTime, + featuredImage: getImageUrl(imagePublicId, { width: 1200, height: 630 }), + ogImage: getImageUrl(imagePublicId, { width: 1200, height: 630 }), + ogSquareImage: getImageUrl(imagePublicId, { width: 400, height: 400 }), + twitterImage: getImageUrl(imagePublicId, { width: 1200, height: 630 }) + } satisfies Post; + post.datePublished && posts.push(post); + } + } + posts = posts.sort( + (first, second) => + new Date(second.datePublished).getTime() - new Date(first.datePublished).getTime() + ); + + // Add SEO metadata + // posts = posts.map((post) => ({ + // ...post, + // seoTitle: `${post.title} | ${defaultMeta.siteTitle}`, + // seoDescription: post.description || defaultMeta.defaultDescription, + // seoImage: post.ogImage || defaultMeta.ogImage, + // seoSquareImage: post.ogSquareImage || defaultMeta.ogSquareImage, + // seoTwitterImage: post.twitterImage || defaultMeta.twitterImage + // })); + + // Add SEO metadata + // posts = posts.map((post) => ({ + // ...post, + // seo: { + // title: post.postTitle, + // description: post.seoMetaDescription, + // openGraph: { + // title: post.postTitle, + // description: post.seoMetaDescription, + // type: post.type, + // url: `https://mattmor.in/blog/${post.slug}`, + // images: [ + // { + // url: post.ogImage, + // width: 1200, + // height: 630, + // alt: post.featuredImageAlt + // }, + // { + // url: post.ogSquareImage, + // width: 400, + // height: 400, + // alt: post.featuredImageAlt + // } + // ] + // }, + // twitter: { + // card: 'summary_large_image', + // site: '@yourtwitterhandle', + // title: post.postTitle, + // description: post.seoMetaDescription, + // image: post.twitterImage, + // imageAlt: post.featuredImageAlt + // } + // } + // }, + + return posts; +}; + +export const GET: RequestHandler = async () => { + const posts = await getPosts(); + const projects = await getProjects(); + return json(posts); +};