From d18c3f62c787fd3b308b2b4627c49c82e99779ce Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Sun, 27 Oct 2024 20:17:42 -0700 Subject: [PATCH] refactor: Add series filter on `getPosts` --- src/lib/posts.ts | 9 ++++++--- src/routes/tag/[slug]/+page.ts | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/posts.ts b/src/lib/posts.ts index d0fe91f..9984e6d 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -15,7 +15,8 @@ export function transformPostMeta(metadata: PostMetadata, slug: string) : Post { }; } -export async function getPosts(tag: string | null = null) : Promise { +// Generates a list of all posts filtering by tags and series +export async function getPosts(filter: { tag?: string, series?: string }) : Promise { const allPostFiles = import.meta.glob('$content/post/*.md'); const iterablePostFiles = Object.entries(allPostFiles); @@ -27,8 +28,10 @@ export async function getPosts(tag: string | null = null) : Promise { return transformPostMeta(metadata, slug); }) ); - if (tag) - posts = posts.filter(post => post.tags.includes(tag)) + if (filter?.tag) + posts = posts.filter(post => post.tags.includes(filter.tag!)) + if (filter?.series) + posts = posts.filter(post => post.series?.includes(filter.series!) ?? false) posts.sort((post1, post2) => { const date1: Date = post1.date; diff --git a/src/routes/tag/[slug]/+page.ts b/src/routes/tag/[slug]/+page.ts index f3a0fe0..6d2fafd 100644 --- a/src/routes/tag/[slug]/+page.ts +++ b/src/routes/tag/[slug]/+page.ts @@ -2,7 +2,7 @@ import type { PageLoad } from './$types'; import { getPosts } from '$lib/posts'; export const load: PageLoad = async ({ params }) => { - const posts = await getPosts(params.slug); + const posts = await getPosts({ tag: params.slug }); return { name: params.slug, posts,