mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-22 18:21:33 +08:00
@ -8,26 +8,16 @@ import Pagination from '@/components/Paginator.astro'
|
|||||||
import PostPreview from '@/components/blog/PostPreview.astro'
|
import PostPreview from '@/components/blog/PostPreview.astro'
|
||||||
import PageLayout from '@/layouts/BaseLayout.astro'
|
import PageLayout from '@/layouts/BaseLayout.astro'
|
||||||
import Button from '@/components/Button.astro'
|
import Button from '@/components/Button.astro'
|
||||||
import { getAllPosts, sortMDByDate } from '@/utils'
|
import { getAllPosts, getUniqueCategories, sortMDByDate } from '@/utils'
|
||||||
import { getCategoriesGroupByName } from 'src/utils/post'
|
|
||||||
|
|
||||||
export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
|
export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
|
||||||
const allPosts = await getAllPosts()
|
const allPosts = await getAllPosts()
|
||||||
const allPostsByDate = sortMDByDate(allPosts)
|
const allPostsByDate = sortMDByDate(allPosts)
|
||||||
const categoriesHierarchy = getCategoriesGroupByName(allPostsByDate)
|
const uniqueCategories = getUniqueCategories(allPostsByDate)
|
||||||
|
|
||||||
// Flatten the hierarchy to get all possible category paths
|
return uniqueCategories.flatMap((category) => {
|
||||||
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) => {
|
|
||||||
const filterPosts = allPostsByDate.filter((post) =>
|
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, {
|
return paginate(filterPosts, {
|
||||||
pageSize: 50,
|
pageSize: 50,
|
||||||
|
@ -1,22 +1,15 @@
|
|||||||
---
|
---
|
||||||
import Button from '@/components/Button.astro'
|
import Button from '@/components/Button.astro'
|
||||||
import type { CategoryHierarchy } from 'src/types'
|
|
||||||
import PageLayout from '@/layouts/BaseLayout.astro'
|
import PageLayout from '@/layouts/BaseLayout.astro'
|
||||||
import { getAllPosts, getUniqueCategoriesWithCount } from '@/utils'
|
import { getAllPosts, getUniqueCategoriesWithCount } from '@/utils'
|
||||||
import { getCategoriesGroupByName } from 'src/utils/post'
|
|
||||||
import CategoriesView from 'src/components/CategoriesView'
|
|
||||||
|
|
||||||
const allPosts = await getAllPosts()
|
const allPosts = await getAllPosts()
|
||||||
const allCategories = getUniqueCategoriesWithCount(allPosts)
|
const allCategories = getUniqueCategoriesWithCount(allPosts)
|
||||||
const allCategoriesHierarchy = getCategoriesGroupByName(allPosts) as CategoryHierarchy[]
|
|
||||||
|
|
||||||
const meta = {
|
const meta = {
|
||||||
description: "A list of all the topics I've written about in my posts",
|
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}>
|
<PageLayout meta={meta}>
|
||||||
@ -36,11 +29,30 @@ const defaultViewMode = 'tree'
|
|||||||
</path>
|
</path>
|
||||||
</svg>
|
</svg>
|
||||||
</Button>
|
</Button>
|
||||||
<CategoriesView
|
|
||||||
client:load
|
<h1 class='mb-6 mt-5 text-2xl font-bold'>Categories</h1>
|
||||||
allCategories={allCategories}
|
{allCategories.length === 0 && <p>No posts yet.</p>}
|
||||||
allCategoriesHierarchy={allCategoriesHierarchy}
|
|
||||||
defaultViewMode={defaultViewMode}
|
{
|
||||||
/>
|
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>
|
</div>
|
||||||
</PageLayout>
|
</PageLayout>
|
||||||
|
Reference in New Issue
Block a user