import type { PageLoad } from './$types';

export const load: PageLoad = async () => {
	const postFiles = import.meta.glob<{
		metadata: { title: string; date: string; excerpt: string; image?: string };
	}>('./posts/*.md', { eager: true });

	const posts = Object.entries(postFiles).map(([path, post]) => {
		const slug = path.replace('./posts/', '').replace('.md', '');

		return {
			slug,
			title: post.metadata.title,
			date: post.metadata.date,
			excerpt: post.metadata.excerpt,
			image: post.metadata.image
		};
	});

	posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());

	return { posts };
};
