英文路径

This commit is contained in:
2026-06-18 21:50:53 +08:00 Unverified
parent cb244eb983
commit fc76f9949f
6 changed files with 38 additions and 11 deletions
+27
View File
@@ -16,6 +16,14 @@ export type BlogPost = {
body: string;
};
const categorySlugMap: Record<string, string> = {
'技术': 'tech',
'建站手札': 'site-notes',
'学习': 'study',
'生活': 'life',
'杂谈': 'notes',
};
function asArray(value: unknown): string[] {
if (Array.isArray(value)) return value.map(String).filter(Boolean);
if (typeof value === 'string' && value.trim()) return [value.trim()];
@@ -94,3 +102,22 @@ export async function getAllTags(): Promise<string[]> {
export async function getAllCategories(): Promise<string[]> {
return [...new Set((await getAllPosts()).flatMap((post) => post.categories))].sort((a, b) => a.localeCompare(b, 'zh-CN'));
}
export function getCategorySlug(category: string): string {
const mapped = categorySlugMap[category];
if (mapped) return mapped;
const slug = category
.trim()
.toLowerCase()
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
return slug || 'category';
}
export function getCategoryHref(category: string): string {
return `/categories/${getCategorySlug(category)}/`;
}