From f78cf9a345d61da3815de7058cb943867cfadf11 Mon Sep 17 00:00:00 2001 From: KazooTTT Date: Sat, 30 Nov 2024 18:37:56 +0800 Subject: [PATCH] Revert "feat: add dir tree" This reverts commit 2d10f8016fe5e50aab7514513421dd7df1bf218f. --- .../categories/[category]/[...page].astro | 18 ++------- src/pages/categories/index.astro | 40 ++++++++++++------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/pages/categories/[category]/[...page].astro b/src/pages/categories/[category]/[...page].astro index b4414c3..28de28f 100644 --- a/src/pages/categories/[category]/[...page].astro +++ b/src/pages/categories/[category]/[...page].astro @@ -8,26 +8,16 @@ import Pagination from '@/components/Paginator.astro' import PostPreview from '@/components/blog/PostPreview.astro' import PageLayout from '@/layouts/BaseLayout.astro' import Button from '@/components/Button.astro' -import { getAllPosts, sortMDByDate } from '@/utils' -import { getCategoriesGroupByName } from 'src/utils/post' +import { getAllPosts, getUniqueCategories, sortMDByDate } from '@/utils' export const getStaticPaths: GetStaticPaths = async ({ paginate }) => { const allPosts = await getAllPosts() const allPostsByDate = sortMDByDate(allPosts) - const categoriesHierarchy = getCategoriesGroupByName(allPostsByDate) + const uniqueCategories = getUniqueCategories(allPostsByDate) - // Flatten the hierarchy to get all possible category paths - const allCategories = categoriesHierarchy.flatMap((category) => { - const result = [category.fullCategory] - Object.values(category.children).forEach((child) => { - result.push(child.fullCategory) - }) - return result - }) - - return allCategories.flatMap((category) => { + return uniqueCategories.flatMap((category) => { const filterPosts = allPostsByDate.filter((post) => - category === '未分类' ? !post.data?.category : post.data.category?.startsWith(category) + category === '未分类' ? !post.data.category : post.data.category === category ) return paginate(filterPosts, { pageSize: 50, diff --git a/src/pages/categories/index.astro b/src/pages/categories/index.astro index 5819965..61d37b6 100644 --- a/src/pages/categories/index.astro +++ b/src/pages/categories/index.astro @@ -1,22 +1,15 @@ --- import Button from '@/components/Button.astro' -import type { CategoryHierarchy } from 'src/types' import PageLayout from '@/layouts/BaseLayout.astro' import { getAllPosts, getUniqueCategoriesWithCount } from '@/utils' -import { getCategoriesGroupByName } from 'src/utils/post' -import CategoriesView from 'src/components/CategoriesView' const allPosts = await getAllPosts() const allCategories = getUniqueCategoriesWithCount(allPosts) -const allCategoriesHierarchy = getCategoriesGroupByName(allPosts) as CategoryHierarchy[] const meta = { description: "A list of all the topics I've written about in my posts", - title: 'All Categories' + title: 'All Categories' } - -// Default to tree view -const defaultViewMode = 'tree' --- @@ -36,11 +29,30 @@ const defaultViewMode = 'tree' - + +

Categories

+ {allCategories.length === 0 &&

No posts yet.

} + + { + allCategories.length > 0 && ( +
    + {allCategories.map(([category, val]) => ( +
  • + + #{category} + + + - {val} post{val > 1 && 's'} + +
  • + ))} +
+ ) + }