fix: unable to list uncategorized posts on the archive page (fix #471)

This commit is contained in:
saicaca
2025-05-30 19:42:45 +08:00
parent 4b30ff2b7b
commit dde7835dd7
7 changed files with 17 additions and 130 deletions

View File

@@ -1,6 +1,7 @@
import { getCollection } from "astro:content";
import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
import { getCategoryUrl } from "@utils/url-utils.ts";
export async function getSortedPosts() {
const allBlogPosts = await getCollection("posts", ({ data }) => {
@@ -54,6 +55,7 @@ export async function getTagList(): Promise<Tag[]> {
export type Category = {
name: string;
count: number;
url: string;
};
export async function getCategoryList(): Promise<Category[]> {
@@ -61,7 +63,7 @@ export async function getCategoryList(): Promise<Category[]> {
return import.meta.env.PROD ? data.draft !== true : true;
});
const count: { [key: string]: number } = {};
allBlogPosts.map((post: { data: { category: string | number } }) => {
allBlogPosts.map((post: { data: { category: string | null } }) => {
if (!post.data.category) {
const ucKey = i18n(I18nKey.uncategorized);
count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1;
@@ -82,7 +84,11 @@ export async function getCategoryList(): Promise<Category[]> {
const ret: Category[] = [];
for (const c of lst) {
ret.push({ name: c, count: count[c] });
ret.push({
name: c,
count: count[c],
url: getCategoryUrl(c),
});
}
return ret;
}

View File

@@ -27,7 +27,7 @@ export function getCategoryUrl(category: string): string {
category.trim() === "" ||
category.trim().toLowerCase() === i18n(I18nKey.uncategorized).toLowerCase()
)
return url("/archive/");
return url("/archive/?uncategorized=true");
return url(`/archive/?category=${encodeURIComponent(category.trim())}`);
}