From e0b93eb63ce01cb83fb7f50a345a5be50b101070 Mon Sep 17 00:00:00 2001 From: saicaca Date: Sun, 21 Jan 2024 20:21:04 +0800 Subject: [PATCH] feat: make 'uncategorized' a category --- src/components/PostMetadata.astro | 13 +++++-------- src/constants/constants.ts | 1 + src/utils/content-utils.ts | 10 ++++++---- src/utils/url-utils.ts | 8 +++++++- tsconfig.json | 1 + 5 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/constants/constants.ts diff --git a/src/components/PostMetadata.astro b/src/components/PostMetadata.astro index d09b51b..7b5919d 100644 --- a/src/components/PostMetadata.astro +++ b/src/components/PostMetadata.astro @@ -31,14 +31,11 @@ const className = Astro.props.class;
- {category && -
- {category} -
- } - {!category &&
{i18n(I18nKey.uncategorized)}
} +
+ {category || i18n(I18nKey.uncategorized)} +
diff --git a/src/constants/constants.ts b/src/constants/constants.ts new file mode 100644 index 0000000..6cd1631 --- /dev/null +++ b/src/constants/constants.ts @@ -0,0 +1 @@ +export const UNCATEGORIZED = '__uncategorized__' diff --git a/src/utils/content-utils.ts b/src/utils/content-utils.ts index 92e3a65..db03eda 100644 --- a/src/utils/content-utils.ts +++ b/src/utils/content-utils.ts @@ -1,4 +1,7 @@ import { getCollection } from 'astro:content' +import {UNCATEGORIZED} from "@constants/constants.ts"; +import {i18n} from "@i18n/translation.ts"; +import I18nKey from "@i18n/i18nKey.ts"; export async function getSortedPosts() { const allBlogPosts = await getCollection('posts', ({ data }) => { @@ -60,12 +63,11 @@ export async function getCategoryList(): Promise { const count: { [key: string]: number } = {} allBlogPosts.map(post => { if (!post.data.category) { + const ucKey = i18n(I18nKey.uncategorized); + count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1 return } - if (!count[post.data.category]) { - count[post.data.category] = 0 - } - count[post.data.category]++ + count[post.data.category] = count[post.data.category] ? count[post.data.category] + 1 : 1 }) const lst = Object.keys(count).sort((a, b) => { diff --git a/src/utils/url-utils.ts b/src/utils/url-utils.ts index 9fc27c2..38bd200 100644 --- a/src/utils/url-utils.ts +++ b/src/utils/url-utils.ts @@ -1,3 +1,6 @@ +import {i18n} from "@i18n/translation.ts"; +import i18nKey from "@i18n/i18nKey.ts"; + export function pathsEqual(path1: string, path2: string) { const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase() const normalizedPath2 = path2.replace(/^\/|\/$/g, '').toLowerCase() @@ -15,7 +18,10 @@ export function getPostUrlBySlug(slug: string): string | null { } export function getCategoryUrl(category: string): string | null { - if (!category) return null + if (!category) + return null + if (category === i18n(i18nKey.uncategorized)) + return '/archive/category/uncategorized' return `/archive/category/${category}` } diff --git a/tsconfig.json b/tsconfig.json index 237efad..86513ab 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "paths": { "@components/*": ["src/components/*"], "@assets/*": ["src/assets/*"], + "@constants/*": ["src/constants/*"], "@utils/*": ["src/utils/*"], "@i18n/*": ["src/i18n/*"], "@layouts/*": ["src/layouts/*"],