feat: allow collapsing widget content, add categories widget

(cherry picked from commit 9a4ca8f6163d5e1375aa7c612e1338cce5a8c0b5)
This commit is contained in:
saicaca
2023-10-11 22:29:23 +08:00
parent 83b765a398
commit f4dc88e982
16 changed files with 174 additions and 33 deletions

View File

@@ -20,12 +20,6 @@ export async function getSortedPosts() {
return sorted;
}
export function getPostUrlBySlug(slug: string): string | null {
if (!slug)
return null;
return `/posts/${slug}`;
}
export async function getTagList(): Promise<{ name: string; count: number }[]> {
const allBlogPosts = await getCollection("posts");
@@ -52,3 +46,27 @@ export async function getTagList(): Promise<{ name: string; count: number }[]> {
return keys.map((key) => ({name: key, count: countMap[key]}));
}
type Category = {
name: string;
count: number;
children: CategoryMap;
}
export type CategoryMap = { [key: string]: Category };
export async function getCategoryMap(): Promise<CategoryMap> {
const allBlogPosts = await getCollection("posts");
let root: CategoryMap = {};
allBlogPosts.map((post) => {
let current = root;
if (!post.data.categories)
return;
for (const c of post.data.categories) {
if (!current[c]) current[c] = {name: c, count: 0, children: {}};
current[c].count++;
current = current[c].children;
}
});
return root;
}

View File

@@ -3,3 +3,16 @@ export function pathsEqual(path1: string, path2: string) {
const normalizedPath2 = path2.replace(/^\/|\/$/g, '').toLowerCase();
return normalizedPath1 === normalizedPath2;
}
export function getPostUrlBySlug(slug: string): string | null {
if (!slug)
return null;
return `/posts/${slug}`;
}
export function getCategoryUrl(category: string): string | null {
if (!category)
return null;
return `/archive/category/${category}`;
}