更换到Astro #9

Merged
biss merged 30 commits from switch-to-astro into master 2026-06-19 13:23:31 +08:00
6 changed files with 38 additions and 11 deletions
Showing only changes of commit fc76f9949f - Show all commits
+2 -2
View File
@@ -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<TocSection[]>((sections, item) => {
<div class="profile-name">biss</div>
<div class="article-author-meta">
<span>{post.dateText}</span>
{post.categories[0] && <a href={`/categories/${encodeURIComponent(post.categories[0])}/`}>{post.categories[0]}</a>}
{post.categories[0] && <a href={getCategoryHref(post.categories[0])}>{post.categories[0]}</a>}
</div>
</section>
+2 -2
View File
@@ -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 && <p>{post.summary}</p>}
</a>
<div class="meta-list">
{post.categories.map((category) => <a href={`/categories/${encodeURIComponent(category)}/`}>{category}</a>)}
{post.categories.map((category) => <a href={getCategoryHref(category)}>{category}</a>)}
{post.tags.map((tag) => <a href={`/tags/${encodeURIComponent(tag)}/`}>#{tag}</a>)}
</div>
</article>
+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)}/`;
}
+2 -2
View File
@@ -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 },
}));
}
+2 -2
View File
@@ -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();
<PageHeaderCard title="分类" subtitle={`共 ${categories.length} 个分类`} />
<div class="term-grid">
{categories.map((category) => (
<a href={`/categories/${encodeURIComponent(category)}/`}>
<a href={getCategoryHref(category)}>
<span>{category}</span>
<strong>{posts.filter((post) => post.categories.includes(category)).length}</strong>
</a>
+3 -3
View File
@@ -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 ? (
<div class="article-hero-meta" aria-label="文章信息">
<span>发布于 {post.dateText}</span>
{post.categories[0] && <a href={`/categories/${encodeURIComponent(post.categories[0])}/`}>{post.categories[0]}</a>}
{post.categories[0] && <a href={getCategoryHref(post.categories[0])}>{post.categories[0]}</a>}
<span>总字数:{wordCountText}</span>
<span>阅读时长:{readingMinutes} 分钟</span>
</div>
) : (
<div class="meta-list">
{post.categories.map((category) => <a href={`/categories/${encodeURIComponent(category)}/`}>{category}</a>)}
{post.categories.map((category) => <a href={getCategoryHref(category)}>{category}</a>)}
{post.tags.map((tag) => <a href={`/tags/${encodeURIComponent(tag)}/`}>#{tag}</a>)}
</div>
)}