英文路径
This commit is contained in:
@@ -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)}/`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user