feat: 新增分类

This commit is contained in:
KazooTTT
2025-02-05 21:30:41 +08:00
parent 1f239d3205
commit fc91e1e390
12 changed files with 323 additions and 108 deletions

View File

@ -55,3 +55,25 @@ export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[]): [strin
),
].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">>): string[] {
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]);
}