fix: wrong category

This commit is contained in:
KazooTTT
2025-01-04 12:29:19 +08:00
parent 4d7663c100
commit 0900354aa1
2 changed files with 19 additions and 17 deletions

View File

@ -1,11 +1,11 @@
import { siteConfig } from '@/site-config'
import rss from '@astrojs/rss'
import type { APIContext } from 'astro'
import MarkdownIt from 'markdown-it'
import sanitizeHtml from 'sanitize-html'
import MarkdownIt from 'markdown-it';
import { getSortedAllPostsAndDiaries } from 'src/utils/post';
import { getSortedAllPostsAndDiaries } from 'src/utils/post'
const parser = new MarkdownIt();
const parser = new MarkdownIt()
export async function GET(context: APIContext) {
const blog = await getSortedAllPostsAndDiaries()
return rss({
@ -17,7 +17,7 @@ export async function GET(context: APIContext) {
<userId>62156866798228480</userId>
</follow_challenge>`,
items: blog.map((post) => {
const prefix = post?.data.category?.startsWith('日记-20') ? '/diary/' : '/blog/'
const prefix = post?.data.category?.startsWith('日记') ? '/diary/' : '/blog/'
return {
title: post.data.title,
pubDate: new Date(post.data.date),

View File

@ -5,7 +5,7 @@ import { getCollection } from 'astro:content'
/** Note: this function filters out draft posts based on the environment */
export async function getAllPosts(): Promise<CollectionEntry<'post'>[]> {
return await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
return !data.draft && !data.category?.startsWith('日记-20')
return !data.draft && !data.category?.startsWith('日记')
})
}
@ -15,7 +15,7 @@ export async function getAllSortedPosts(): Promise<CollectionEntry<'post'>[]> {
export const getAllDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
return await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
return !data.draft && data.category?.startsWith('日记-20')
return !data.draft && data.category?.startsWith('日记')
})
}
@ -23,7 +23,6 @@ export const getAllDiariesSorted = async (): Promise<CollectionEntry<'post'>[]>
return sortMDByDate(await getAllDiaries())
}
export const getSortedAllPostsAndDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
const posts = await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
return !data.draft
@ -83,7 +82,9 @@ export function getUniqueCategoriesWithCount(
].sort((a, b) => b[1] - a[1])
}
export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>): CategoryHierarchy[] {
export function getCategoriesGroupByName(
posts: Array<CollectionEntry<'post'>>
): CategoryHierarchy[] {
const categories = getUniqueCategoriesWithCount(posts)
const hierarchicalCategories: CategoryHierarchy[] = []
@ -95,7 +96,7 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
// If it's the last part, add count
if (index === parts.length - 1) {
// Check if category already exists
let categoryObj = current.find(cat => cat.category === parts[0])
let categoryObj = current.find((cat) => cat.category === parts[0])
if (!categoryObj) {
categoryObj = {
@ -125,7 +126,7 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
}
} else {
// Ensure top-level category exists
let categoryObj = current.find(cat => cat.fullCategory === part)
let categoryObj = current.find((cat) => cat.fullCategory === part)
if (!categoryObj) {
categoryObj = {
category: part,
@ -140,21 +141,22 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
})
// Calculate total count for each category by summing subcategories
hierarchicalCategories.forEach(category => {
hierarchicalCategories.forEach((category) => {
if (Object.keys(category.children).length > 0) {
category.count = Object.values(category.children)
.reduce((sum, child) => sum + (child.count || 0), 0)
category.count = Object.values(category.children).reduce(
(sum, child) => sum + (child.count || 0),
0
)
}
})
// Filter out categories with zero count and sort by count
return hierarchicalCategories
.filter(category => category.count > 0)
.map(category => ({
.filter((category) => category.count > 0)
.map((category) => ({
...category,
children: Object.fromEntries(
Object.entries(category.children)
.filter(([_, child]) => child.count > 0)
Object.entries(category.children).filter(([_, child]) => child.count > 0)
)
}))
.sort((a, b) => b.count - a.count)