feat: make 'uncategorized' a category

This commit is contained in:
saicaca 2024-01-21 20:21:04 +08:00
parent 23533e9ac9
commit e0b93eb63c
5 changed files with 20 additions and 13 deletions

View File

@ -31,14 +31,11 @@ const className = Astro.props.class;
<Icon name="material-symbols:menu-rounded" class="text-xl"></Icon> <Icon name="material-symbols:menu-rounded" class="text-xl"></Icon>
</div> </div>
<div class="flex flex-row flex-nowrap"> <div class="flex flex-row flex-nowrap">
{category && <div><a href=`/archive/category/${category || 'uncategorized'}` aria-label=`View all posts in the ${category} category`
<div><a href=`/archive/category/${category}` aria-label=`View all posts in the ${category} category`
class="link-lg transition text-black/50 dark:text-white/50 text-sm font-medium class="link-lg transition text-black/50 dark:text-white/50 text-sm font-medium
hover:text-[var(--primary)] dark:hover:text-[var(--primary)] whitespace-nowrap"> hover:text-[var(--primary)] dark:hover:text-[var(--primary)] whitespace-nowrap">
{category} {category || i18n(I18nKey.uncategorized)}
</a></div> </a></div>
}
{!category && <div class="transition text-black/50 dark:text-white/50 text-sm font-medium">{i18n(I18nKey.uncategorized)}</div>}
</div> </div>
</div> </div>

View File

@ -0,0 +1 @@
export const UNCATEGORIZED = '__uncategorized__'

View File

@ -1,4 +1,7 @@
import { getCollection } from 'astro:content' 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() { export async function getSortedPosts() {
const allBlogPosts = await getCollection('posts', ({ data }) => { const allBlogPosts = await getCollection('posts', ({ data }) => {
@ -60,12 +63,11 @@ export async function getCategoryList(): Promise<Category[]> {
const count: { [key: string]: number } = {} const count: { [key: string]: number } = {}
allBlogPosts.map(post => { allBlogPosts.map(post => {
if (!post.data.category) { if (!post.data.category) {
const ucKey = i18n(I18nKey.uncategorized);
count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1
return return
} }
if (!count[post.data.category]) { count[post.data.category] = count[post.data.category] ? count[post.data.category] + 1 : 1
count[post.data.category] = 0
}
count[post.data.category]++
}) })
const lst = Object.keys(count).sort((a, b) => { const lst = Object.keys(count).sort((a, b) => {

View File

@ -1,3 +1,6 @@
import {i18n} from "@i18n/translation.ts";
import i18nKey from "@i18n/i18nKey.ts";
export function pathsEqual(path1: string, path2: string) { export function pathsEqual(path1: string, path2: string) {
const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase() const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase()
const normalizedPath2 = path2.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 { 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}` return `/archive/category/${category}`
} }

View File

@ -12,6 +12,7 @@
"paths": { "paths": {
"@components/*": ["src/components/*"], "@components/*": ["src/components/*"],
"@assets/*": ["src/assets/*"], "@assets/*": ["src/assets/*"],
"@constants/*": ["src/constants/*"],
"@utils/*": ["src/utils/*"], "@utils/*": ["src/utils/*"],
"@i18n/*": ["src/i18n/*"], "@i18n/*": ["src/i18n/*"],
"@layouts/*": ["src/layouts/*"], "@layouts/*": ["src/layouts/*"],