From 22787eceda1700c9bdc5a517b582666deeff0fd6 Mon Sep 17 00:00:00 2001
From: Leni Aniva
{date}
diff --git a/src/lib/posts.ts b/src/lib/posts.ts index ff2065e..0c31b6e 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -1,8 +1,8 @@ -export async function getPosts() { +export async function getPosts(tag: string | null = null) { const allPostFiles = import.meta.glob('$content/post/*.md'); const iterablePostFiles = Object.entries(allPostFiles); - const allPosts = await Promise.all( + let posts = await Promise.all( iterablePostFiles.map(async ([pathMarkdown, resolver]) => { const { metadata } = await resolver(); const pathPost = "/post/" + pathMarkdown.slice(pathMarkdown.lastIndexOf("/") + 1, -".md".length); @@ -16,11 +16,26 @@ export async function getPosts() { }; }) ); + if (tag) + posts = posts.filter(obj => obj.meta.tags.includes(tag)) - allPosts.sort((post1, post2) => { + posts.sort((post1, post2) => { const date1: Date = post1.meta.date; const date2: Date = post2.meta.date; return date2.getTime() - date1.getTime(); }); - return allPosts; + return posts; +} + +export async function getTags() { + 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 new Set(allPosts.flat()); } diff --git a/src/lib/sitemap.ts b/src/lib/sitemap.ts index 97f9051..3b312b9 100644 --- a/src/lib/sitemap.ts +++ b/src/lib/sitemap.ts @@ -1,7 +1,7 @@ export const routes: { route: string, name: string, disabled?: boolean }[] = [ { route: "/", name: "Home" }, { route: "/post", name: "Blog" }, - { route: "/tag", name: "Tags", disabled: true }, + { route: "/tag", name: "Tags" }, { route: "/gallery", name: "Gallery", disabled: true }, { route: "/archives", name: "Archives" }, ]; diff --git a/src/routes/tag/+page.server.ts b/src/routes/tag/+page.server.ts new file mode 100644 index 0000000..a5bf330 --- /dev/null +++ b/src/routes/tag/+page.server.ts @@ -0,0 +1,7 @@ +import type { PageLoad } from './$types'; +import { getTags } from '$lib/posts'; + +export const load: PageLoad = async (_) => { + const allTags = await getTags(); + return { allTags }; +}; diff --git a/src/routes/tag/+page.svelte b/src/routes/tag/+page.svelte index e69de29..2386ccf 100644 --- a/src/routes/tag/+page.svelte +++ b/src/routes/tag/+page.svelte @@ -0,0 +1,28 @@ + + +