mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-22 02:01:32 +08:00
@ -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,
|
||||
|
@ -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'
|
||||
---
|
||||
|
||||
<PageLayout meta={meta}>
|
||||
@ -36,11 +29,30 @@ const defaultViewMode = 'tree'
|
||||
</path>
|
||||
</svg>
|
||||
</Button>
|
||||
<CategoriesView
|
||||
client:load
|
||||
allCategories={allCategories}
|
||||
allCategoriesHierarchy={allCategoriesHierarchy}
|
||||
defaultViewMode={defaultViewMode}
|
||||
/>
|
||||
|
||||
<h1 class='mb-6 mt-5 text-2xl font-bold'>Categories</h1>
|
||||
{allCategories.length === 0 && <p>No posts yet.</p>}
|
||||
|
||||
{
|
||||
allCategories.length > 0 && (
|
||||
<ul class='flex flex-col gap-y-3'>
|
||||
{allCategories.map(([category, val]) => (
|
||||
<li class='flex items-center gap-x-2 '>
|
||||
<a
|
||||
class='inline-block underline underline-offset-4 hover:text-foreground/75'
|
||||
data-astro-prefetch
|
||||
href={`/categories/${category}/`}
|
||||
title={`View posts of the Category: ${category}`}
|
||||
>
|
||||
#{category}
|
||||
</a>
|
||||
<span class='inline-block'>
|
||||
- {val} post{val > 1 && 's'}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
</div>
|
||||
</PageLayout>
|
||||
|
Reference in New Issue
Block a user