New API for posts ! best way to do this.
This commit is contained in:
parent
dda41c8414
commit
74987b8408
|
@ -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<Post & MdsvexImport>('$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<Post, 'slug'>;
|
||||||
|
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);
|
||||||
|
};
|
Loading…
Reference in New Issue