优化结构

This commit is contained in:
2026-06-18 21:44:37 +08:00 Unverified
parent 3051fe2d6a
commit cb244eb983
74 changed files with 94 additions and 144 deletions
-37
View File
@@ -1,37 +0,0 @@
type PageModule = {
frontmatter: Record<string, unknown>;
Content: unknown;
rawContent?: () => string;
};
export type SitePage = PageModule & {
title: string;
slug: string;
};
const modules = import.meta.glob<PageModule>('/source/**/index.md', { eager: true });
export function getSitePages(): SitePage[] {
return Object.entries(modules)
.filter(([path]) => {
if (path.includes('/_posts/') || path.includes('/_drafts/')) return false;
return ![
'/source/about/index.md',
'/source/cc/index.md',
'/source/cookie/index.md',
'/source/link/index.md',
'/source/privacy/index.md',
'/source/categories/index.md',
'/source/tags/index.md',
'/source/shuoshuo/index.md',
].includes(path);
})
.map(([path, module]) => {
const slug = path.replace('/source/', '').replace('/index.md', '');
return {
...module,
title: String(module.frontmatter.title ?? slug),
slug,
};
});
}
+29 -31
View File
@@ -1,11 +1,8 @@
type MarkdownModule = {
frontmatter: Record<string, unknown>;
Content: unknown;
rawContent?: () => string;
file?: string;
};
import type { CollectionEntry } from 'astro:content';
import { getCollection } from 'astro:content';
export type BlogPost = MarkdownModule & {
export type BlogPost = {
entry: CollectionEntry<'posts'>;
title: string;
slug: string;
date: Date;
@@ -16,10 +13,9 @@ export type BlogPost = MarkdownModule & {
comments: boolean;
cover?: string;
href: string;
body: string;
};
const modules = import.meta.glob<MarkdownModule>('/posts/**/*.md', { eager: true });
function asArray(value: unknown): string[] {
if (Array.isArray(value)) return value.map(String).filter(Boolean);
if (typeof value === 'string' && value.trim()) return [value.trim()];
@@ -43,20 +39,19 @@ function formatDate(date: Date): string {
}).format(date);
}
function fallbackSlug(path: string): string {
return path
function fallbackSlug(id: string): string {
return id
.split('/')
.pop()
?.replace(/\.md$/, '')
.toLowerCase() ?? 'post';
}
function makeSummary(module: MarkdownModule): string {
const explicit = module.frontmatter.summary ?? module.frontmatter.description;
function makeSummary(entry: CollectionEntry<'posts'>): string {
const explicit = entry.data.summary ?? entry.data.description;
if (typeof explicit === 'string' && explicit.trim()) return explicit.trim();
const raw = module.rawContent?.() ?? '';
return raw
return entry.body
.replace(/```[\s\S]*?```/g, '')
.replace(/<[^>]+>/g, '')
.replace(/[#>*_`~\-\[\]\(\)]/g, '')
@@ -65,34 +60,37 @@ function makeSummary(module: MarkdownModule): string {
.slice(0, 120);
}
export function getAllPosts(): BlogPost[] {
return Object.entries(modules)
.map(([path, module]) => {
const date = toDate(module.frontmatter.date);
const slug = String(module.frontmatter.abbrlink ?? fallbackSlug(path));
const title = String(module.frontmatter.title ?? slug);
export async function getAllPosts(): Promise<BlogPost[]> {
const entries = await getCollection('posts');
return entries
.map((entry) => {
const date = toDate(entry.data.date);
const slug = String(entry.data.abbrlink ?? fallbackSlug(entry.id));
const title = String(entry.data.title ?? slug);
return {
...module,
entry,
title,
slug,
date,
dateText: formatDate(date),
tags: asArray(module.frontmatter.tags),
categories: asArray(module.frontmatter.categories),
summary: makeSummary(module),
comments: module.frontmatter.comments !== false,
cover: typeof module.frontmatter.cover === 'string' ? module.frontmatter.cover : undefined,
tags: asArray(entry.data.tags),
categories: asArray(entry.data.categories),
summary: makeSummary(entry),
comments: entry.data.comments !== false,
cover: typeof entry.data.cover === 'string' ? entry.data.cover : undefined,
href: `/posts/${slug}/`,
body: entry.body,
};
})
.sort((a, b) => b.date.valueOf() - a.date.valueOf());
}
export function getAllTags(): string[] {
return [...new Set(getAllPosts().flatMap((post) => post.tags))].sort((a, b) => a.localeCompare(b, 'zh-CN'));
export async function getAllTags(): Promise<string[]> {
return [...new Set((await getAllPosts()).flatMap((post) => post.tags))].sort((a, b) => a.localeCompare(b, 'zh-CN'));
}
export function getAllCategories(): string[] {
return [...new Set(getAllPosts().flatMap((post) => post.categories))].sort((a, b) => a.localeCompare(b, 'zh-CN'));
export async function getAllCategories(): Promise<string[]> {
return [...new Set((await getAllPosts()).flatMap((post) => post.categories))].sort((a, b) => a.localeCompare(b, 'zh-CN'));
}