mirror of
https://github.com/saicaca/fuwari.git
synced 2026-01-12 07:12:52 +01:00
feat: allow collapsing widget content, add categories widget
(cherry picked from commit 9a4ca8f6163d5e1375aa7c612e1338cce5a8c0b5)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user