From 0900354aa1c77d130f49f692dbbef92c84880c06 Mon Sep 17 00:00:00 2001 From: KazooTTT Date: Sat, 4 Jan 2025 12:29:19 +0800 Subject: [PATCH] fix: wrong category --- src/pages/rss.xml.ts | 8 ++++---- src/utils/post.ts | 28 +++++++++++++++------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index 3aedcf3..fb70a43 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -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) { 62156866798228480 `, 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), diff --git a/src/utils/post.ts b/src/utils/post.ts index 0b73a2b..05fb408 100644 --- a/src/utils/post.ts +++ b/src/utils/post.ts @@ -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[]> { 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[]> { export const getAllDiaries = async (): Promise[]> => { 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[]> return sortMDByDate(await getAllDiaries()) } - export const getSortedAllPostsAndDiaries = async (): Promise[]> => { 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>): CategoryHierarchy[] { +export function getCategoriesGroupByName( + posts: Array> +): CategoryHierarchy[] { const categories = getUniqueCategoriesWithCount(posts) const hierarchicalCategories: CategoryHierarchy[] = [] @@ -95,7 +96,7 @@ export function getCategoriesGroupByName(posts: Array>): // 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>): } } 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>): }) // 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)