优化结构
This commit is contained in:
+29
-31
@@ -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'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user