mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-21 09:41:30 +08:00
fix: wrong category
This commit is contained in:
@ -1,11 +1,11 @@
|
|||||||
import { siteConfig } from '@/site-config'
|
import { siteConfig } from '@/site-config'
|
||||||
import rss from '@astrojs/rss'
|
import rss from '@astrojs/rss'
|
||||||
import type { APIContext } from 'astro'
|
import type { APIContext } from 'astro'
|
||||||
|
import MarkdownIt from 'markdown-it'
|
||||||
import sanitizeHtml from 'sanitize-html'
|
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) {
|
export async function GET(context: APIContext) {
|
||||||
const blog = await getSortedAllPostsAndDiaries()
|
const blog = await getSortedAllPostsAndDiaries()
|
||||||
return rss({
|
return rss({
|
||||||
@ -17,7 +17,7 @@ export async function GET(context: APIContext) {
|
|||||||
<userId>62156866798228480</userId>
|
<userId>62156866798228480</userId>
|
||||||
</follow_challenge>`,
|
</follow_challenge>`,
|
||||||
items: blog.map((post) => {
|
items: blog.map((post) => {
|
||||||
const prefix = post?.data.category?.startsWith('日记-20') ? '/diary/' : '/blog/'
|
const prefix = post?.data.category?.startsWith('日记') ? '/diary/' : '/blog/'
|
||||||
return {
|
return {
|
||||||
title: post.data.title,
|
title: post.data.title,
|
||||||
pubDate: new Date(post.data.date),
|
pubDate: new Date(post.data.date),
|
||||||
|
@ -5,7 +5,7 @@ import { getCollection } from 'astro:content'
|
|||||||
/** Note: this function filters out draft posts based on the environment */
|
/** Note: this function filters out draft posts based on the environment */
|
||||||
export async function getAllPosts(): Promise<CollectionEntry<'post'>[]> {
|
export async function getAllPosts(): Promise<CollectionEntry<'post'>[]> {
|
||||||
return await getCollection('post', ({ data }: { data: 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'>[]> => {
|
export const getAllDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
|
||||||
return await getCollection('post', ({ data }: { data: 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())
|
return sortMDByDate(await getAllDiaries())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const getSortedAllPostsAndDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
|
export const getSortedAllPostsAndDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
|
||||||
const posts = await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
|
const posts = await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
|
||||||
return !data.draft
|
return !data.draft
|
||||||
@ -83,7 +82,9 @@ export function getUniqueCategoriesWithCount(
|
|||||||
].sort((a, b) => b[1] - a[1])
|
].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 categories = getUniqueCategoriesWithCount(posts)
|
||||||
const hierarchicalCategories: CategoryHierarchy[] = []
|
const hierarchicalCategories: CategoryHierarchy[] = []
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
|
|||||||
// If it's the last part, add count
|
// If it's the last part, add count
|
||||||
if (index === parts.length - 1) {
|
if (index === parts.length - 1) {
|
||||||
// Check if category already exists
|
// 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) {
|
if (!categoryObj) {
|
||||||
categoryObj = {
|
categoryObj = {
|
||||||
@ -125,7 +126,7 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Ensure top-level category exists
|
// Ensure top-level category exists
|
||||||
let categoryObj = current.find(cat => cat.fullCategory === part)
|
let categoryObj = current.find((cat) => cat.fullCategory === part)
|
||||||
if (!categoryObj) {
|
if (!categoryObj) {
|
||||||
categoryObj = {
|
categoryObj = {
|
||||||
category: part,
|
category: part,
|
||||||
@ -140,21 +141,22 @@ export function getCategoriesGroupByName(posts: Array<CollectionEntry<'post'>>):
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Calculate total count for each category by summing subcategories
|
// Calculate total count for each category by summing subcategories
|
||||||
hierarchicalCategories.forEach(category => {
|
hierarchicalCategories.forEach((category) => {
|
||||||
if (Object.keys(category.children).length > 0) {
|
if (Object.keys(category.children).length > 0) {
|
||||||
category.count = Object.values(category.children)
|
category.count = Object.values(category.children).reduce(
|
||||||
.reduce((sum, child) => sum + (child.count || 0), 0)
|
(sum, child) => sum + (child.count || 0),
|
||||||
|
0
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Filter out categories with zero count and sort by count
|
// Filter out categories with zero count and sort by count
|
||||||
return hierarchicalCategories
|
return hierarchicalCategories
|
||||||
.filter(category => category.count > 0)
|
.filter((category) => category.count > 0)
|
||||||
.map(category => ({
|
.map((category) => ({
|
||||||
...category,
|
...category,
|
||||||
children: Object.fromEntries(
|
children: Object.fromEntries(
|
||||||
Object.entries(category.children)
|
Object.entries(category.children).filter(([_, child]) => child.count > 0)
|
||||||
.filter(([_, child]) => child.count > 0)
|
|
||||||
)
|
)
|
||||||
}))
|
}))
|
||||||
.sort((a, b) => b.count - a.count)
|
.sort((a, b) => b.count - a.count)
|
||||||
|
Reference in New Issue
Block a user