refactor: Add series filter on `getPosts`

This commit is contained in:
Leni Aniva 2024-10-27 20:17:42 -07:00
parent 78a414c747
commit d18c3f62c7
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
2 changed files with 7 additions and 4 deletions

View File

@ -15,7 +15,8 @@ export function transformPostMeta(metadata: PostMetadata, slug: string) : Post {
}; };
} }
export async function getPosts(tag: string | null = null) : Promise<Post[]> { // Generates a list of all posts filtering by tags and series
export async function getPosts(filter: { tag?: string, series?: string }) : Promise<Post[]> {
const allPostFiles = import.meta.glob('$content/post/*.md'); const allPostFiles = import.meta.glob('$content/post/*.md');
const iterablePostFiles = Object.entries(allPostFiles); const iterablePostFiles = Object.entries(allPostFiles);
@ -27,8 +28,10 @@ export async function getPosts(tag: string | null = null) : Promise<Post[]> {
return transformPostMeta(metadata, slug); return transformPostMeta(metadata, slug);
}) })
); );
if (tag) if (filter?.tag)
posts = posts.filter(post => post.tags.includes(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) => { posts.sort((post1, post2) => {
const date1: Date = post1.date; const date1: Date = post1.date;

View File

@ -2,7 +2,7 @@ import type { PageLoad } from './$types';
import { getPosts } from '$lib/posts'; import { getPosts } from '$lib/posts';
export const load: PageLoad = async ({ params }) => { export const load: PageLoad = async ({ params }) => {
const posts = await getPosts(params.slug); const posts = await getPosts({ tag: params.slug });
return { return {
name: params.slug, name: params.slug,
posts, posts,