import type { Post } from '$lib/types' type PostMetadata = { title: string , description?: string, tags: [string], series?: [string], date: Date, } export function transformPostMeta(metadata: PostMetadata, slug: string) : Post { return { ...metadata, date: new Date(metadata.date), slug, }; } export async function getPosts(tag: string | null = null) : Promise { const allPostFiles = import.meta.glob('$content/post/*.md'); const iterablePostFiles = Object.entries(allPostFiles); let posts: Post[] = await Promise.all( iterablePostFiles.map(async ([pathMarkdown, resolver]) => { const { metadata } = await resolver(); const slug = pathMarkdown.slice(pathMarkdown.lastIndexOf("/") + 1, -".md".length); return transformPostMeta(metadata, slug); }) ); if (tag) posts = posts.filter(post => post.tags.includes(tag)) posts.sort((post1, post2) => { const date1: Date = post1.date; const date2: Date = post2.date; return date2.getTime() - date1.getTime(); }); return posts; } export async function getTags() : Promise> { const allPostFiles = import.meta.glob('$content/post/*.md'); const iterablePostFiles = Object.entries(allPostFiles); const allPosts: string[][] = await Promise.all( iterablePostFiles.map(async ([_, resolver]) => { const { metadata } = await resolver(); return metadata.tags; }) ); return allPosts.flat().reduce((acc: Map, curr: string) => { return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc }, new Map()); }