diff --git a/src/components/ArticleSidebar.astro b/src/components/ArticleSidebar.astro index e281dc8..33ccf2b 100644 --- a/src/components/ArticleSidebar.astro +++ b/src/components/ArticleSidebar.astro @@ -1,5 +1,5 @@ --- -import type { BlogPost } from '@/lib/posts'; +import { getCategoryHref, type BlogPost } from '@/lib/posts'; type TocItem = { depth: number; @@ -38,7 +38,7 @@ const tocSections = toc.reduce((sections, item) => {
biss
{post.dateText} - {post.categories[0] && {post.categories[0]}} + {post.categories[0] && {post.categories[0]}}
diff --git a/src/components/PostCard.astro b/src/components/PostCard.astro index 20ac5a2..3493493 100644 --- a/src/components/PostCard.astro +++ b/src/components/PostCard.astro @@ -1,5 +1,5 @@ --- -import type { BlogPost } from '@/lib/posts'; +import { getCategoryHref, type BlogPost } from '@/lib/posts'; interface Props { post: BlogPost; @@ -15,7 +15,7 @@ const { post } = Astro.props; {post.summary &&

{post.summary}

}
- {post.categories.map((category) => {category})} + {post.categories.map((category) => {category})} {post.tags.map((tag) => #{tag})}
diff --git a/src/lib/posts.ts b/src/lib/posts.ts index 29d086d..0e34590 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -16,6 +16,14 @@ export type BlogPost = { body: string; }; +const categorySlugMap: Record = { + '技术': '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 { export async function getAllCategories(): Promise { 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)}/`; +} diff --git a/src/pages/categories/[category].astro b/src/pages/categories/[category].astro index 7e146f1..374e53d 100644 --- a/src/pages/categories/[category].astro +++ b/src/pages/categories/[category].astro @@ -2,11 +2,11 @@ import BaseLayout from '@/layouts/BaseLayout.astro'; import PageHeaderCard from '@/components/PageHeaderCard.astro'; import PostCard from '@/components/PostCard.astro'; -import { getAllCategories, getAllPosts } from '@/lib/posts'; +import { getAllCategories, getAllPosts, getCategorySlug } from '@/lib/posts'; export async function getStaticPaths() { return (await getAllCategories()).map((category) => ({ - params: { category }, + params: { category: getCategorySlug(category) }, props: { category }, })); } diff --git a/src/pages/categories/index.astro b/src/pages/categories/index.astro index f1cdffc..3e8e0ed 100644 --- a/src/pages/categories/index.astro +++ b/src/pages/categories/index.astro @@ -1,7 +1,7 @@ --- import BaseLayout from '@/layouts/BaseLayout.astro'; import PageHeaderCard from '@/components/PageHeaderCard.astro'; -import { getAllCategories, getAllPosts } from '@/lib/posts'; +import { getAllCategories, getAllPosts, getCategoryHref } from '@/lib/posts'; const posts = await getAllPosts(); const categories = await getAllCategories(); @@ -12,7 +12,7 @@ const categories = await getAllCategories();
{categories.map((category) => ( - + {category} {posts.filter((post) => post.categories.includes(category)).length} diff --git a/src/pages/posts/[slug].astro b/src/pages/posts/[slug].astro index 4e32b07..f8eba9e 100644 --- a/src/pages/posts/[slug].astro +++ b/src/pages/posts/[slug].astro @@ -4,7 +4,7 @@ import PostCopyrightCard from '@/components/PostCopyrightCard.astro'; import TwikooComments from '@/components/TwikooComments.astro'; import BaseLayout from '@/layouts/BaseLayout.astro'; import { render } from 'astro:content'; -import { getAllPosts } from '@/lib/posts'; +import { getAllPosts, getCategoryHref } from '@/lib/posts'; export async function getStaticPaths() { return (await getAllPosts()).map((post) => ({ @@ -89,13 +89,13 @@ const twikooEnvId = 'https://comment.biss.click'; {post.cover ? (
发布于 {post.dateText} - {post.categories[0] && {post.categories[0]}} + {post.categories[0] && {post.categories[0]}} 总字数:{wordCountText} 阅读时长:{readingMinutes} 分钟
) : (
- {post.categories.map((category) => {category})} + {post.categories.map((category) => {category})} {post.tags.map((tag) => #{tag})}
)}