feat: add category page

This commit is contained in:
KazooTTT
2024-07-21 22:29:07 +08:00
parent 3f502739d0
commit 50813b0c43
11 changed files with 320 additions and 51 deletions

View File

@ -37,3 +37,26 @@ export function getUniqueTagsWithCount(
)
].sort((a, b) => b[1] - a[1])
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getAllCategories(posts: Array<CollectionEntry<'post'>>): string[] {
return posts.map(post => post.data.category ?? "未分类")
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getUniqueCategories(posts: Array<CollectionEntry<'post'>>) {
return [...new Set(getAllCategories(posts))]
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getUniqueCategoriesWithCount(
posts: Array<CollectionEntry<'post'>>
): Array<[string, number]> {
return [
...getAllCategories(posts).reduce(
(acc, t) => acc.set(t, (acc.get(t) || 0) + 1),
new Map<string, number>()
)
].sort((a, b) => b[1] - a[1])
}