更换到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

No files matched your search

+2 -2
View File
@@ -1,5 +1,5 @@
--- ---
import type { BlogPost } from '@/lib/posts'; import { getCategoryHref, type BlogPost } from '@/lib/posts';
type TocItem = { type TocItem = {
depth: number; depth: number;
@@ -38,7 +38,7 @@ const tocSections = toc.reduce<TocSection[]>((sections, item) => {
<div class="profile-name">biss</div> <div class="profile-name">biss</div>
<div class="article-author-meta"> <div class="article-author-meta">
<span>{post.dateText}</span> <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> </div>
</section> </section>
+2 -2
View File
@@ -1,5 +1,5 @@
--- ---
import type { BlogPost } from '@/lib/posts'; import { getCategoryHref, type BlogPost } from '@/lib/posts';
interface Props { interface Props {
post: BlogPost; post: BlogPost;
@@ -15,7 +15,7 @@ const { post } = Astro.props;
{post.summary && <p>{post.summary}</p>} {post.summary && <p>{post.summary}</p>}
</a> </a>
<div class="meta-list"> <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>)} {post.tags.map((tag) => <a href={`/tags/${encodeURIComponent(tag)}/`}>#{tag}</a>)}
</div> </div>
</article> </article>
+27
View File
@@ -16,6 +16,14 @@ export type BlogPost = {
body: string; body: string;
}; };
const categorySlugMap: Record<string, string> = {
'技术': 'tech',
'建站手札': 'site-notes',
'学习': 'study',
'生活': 'life',
'杂谈': 'notes',
};
function asArray(value: unknown): string[] { function asArray(value: unknown): string[] {
if (Array.isArray(value)) return value.map(String).filter(Boolean); if (Array.isArray(value)) return value.map(String).filter(Boolean);
if (typeof value === 'string' && value.trim()) return [value.trim()]; 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[]> { export async function getAllCategories(): Promise<string[]> {
return [...new Set((await getAllPosts()).flatMap((post) => post.categories))].sort((a, b) => a.localeCompare(b, 'zh-CN')); 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)}/`;
}
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff. Show more