import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';

export const entries = () => {
	const posts = import.meta.glob('../*.md', { eager: true });
	return Object.keys(posts).map((path) => {
		const slug = path.replace('../', '').replace('.md', '');
		return { slug };
	});
};

export const load: PageLoad = async ({ params }) => {
	try {
		const post = await import(`../${params.slug}.md`);

		return {
			content: post.default,
			metadata: post.metadata
		};
	} catch (e) {
		throw error(404, `Post not found: ${params.slug}. Error: ${e}`);
	}
};
