--- import { getCollection, render } from 'astro:content'; import { contentStructure, getRelativePath, getBasename, getDirPath, getSpecialPath } from '@/content.config'; import type { SectionStructure } from '@/content.config'; import Layout from '@/components/Layout.astro'; import Breadcrumb from '@/components/Breadcrumb.astro'; // 添加这一行,告诉Astro预渲染这个页面 export const prerender = true; export async function getStaticPaths() { const articles = await getCollection('articles'); const views = ['grid', 'timeline']; // 为每篇文章添加section信息 const articlesWithSections = articles.map(article => { // 查找文章所属的目录 let section = ''; const findSection = (sections: SectionStructure[], articleId: string, parentPath = ''): string | null => { for (const sec of sections) { const sectionPath = parentPath ? `${parentPath}/${sec.name}` : sec.name; // 检查文章是否在当前目录中 for (const artPath of sec.articles) { const artId = getRelativePath(artPath); const basename = getBasename(artPath); const dirPath = getDirPath(artPath); // 尝试多种可能的ID格式 const possibleIds = [ artId, // 完整相对路径 `${sectionPath}/${basename}`, // 目录路径/文件名 basename, // 仅文件名 dirPath ? `${dirPath}/${basename}` : basename, // 目录路径/文件名 `articles/${artId}`, // 添加集合名称前缀 `articles/${sectionPath}/${basename}` // 添加集合名称前缀和目录路径 ]; // 精确匹配(不区分大小写) if (possibleIds.some(id => id.toLowerCase() === articleId.toLowerCase())) { return sectionPath; } // 检查文章ID是否以某个可能的ID结尾(不区分大小写) for (const possibleId of possibleIds) { if (articleId.toLowerCase().endsWith(possibleId.toLowerCase())) { return sectionPath; } } } // 递归检查子目录 const foundInSubsection = findSection(sec.sections, articleId, sectionPath); if (foundInSubsection) { return foundInSubsection; } } return null; }; section = findSection(contentStructure.sections, article.id) || ''; return { ...article, section }; }); // 为每篇文章生成路由参数 const paths = []; for (const article of articlesWithSections) { // 检查文章ID是否需要特殊处理 const specialId = getSpecialPath(article.id); // 添加基本路由 paths.push({ params: { id: specialId }, props: { article, section: article.section, originalId: specialId !== article.id ? article.id : undefined, view: undefined } }); // 为每个视图添加路由 for (const view of views) { paths.push({ params: { id: `${specialId}/${view}` }, props: { article, section: article.section, originalId: specialId !== article.id ? article.id : undefined, view } }); } } return paths; } // 获取文章内容 const { article, section, originalId, view } = Astro.props; // 如果有原始ID,使用它来渲染内容 const articleToRender = originalId ? { ...article, id: originalId } : article; // 渲染文章内容 const { Content } = await render(articleToRender); // 获取面包屑导航 const breadcrumbs = section ? section.split('/') : []; // 获取相关文章 const allArticles = await getCollection('articles'); const relatedArticles = allArticles .filter(a => a.id !== article.id && ( (a.data.tags && article.data.tags && a.data.tags.some(tag => article.data.tags?.includes(tag))) )) .sort((a, b) => b.data.date.getTime() - a.data.date.getTime()) .slice(0, 3); // 准备文章描述 const description = article.data.summary || `${article.data.title} - 发布于 ${article.data.date.toLocaleDateString('zh-CN')}`; // 处理特殊ID的函数 function getArticleUrl(articleId: string) { return `/articles/${getSpecialPath(articleId)}${view ? `/${view}` : ''}`; } --- {/* 返回按钮 */} 返回文章列表 {article.data.title} {article.data.date.toLocaleDateString('zh-CN')} {/* 显示文章所在目录 */} {section && ( {section} )} {article.data.tags && article.data.tags.length > 0 && ( {article.data.tags.map(tag => ( #{tag} ))} )} 文章目录 {relatedArticles.length > 0 && ( 相关文章 {relatedArticles.map(relatedArticle => ( {relatedArticle.data.title} {relatedArticle.data.date.toLocaleDateString('zh-CN')} {relatedArticle.data.summary && ( {relatedArticle.data.summary} )} ))} )}
{relatedArticle.data.date.toLocaleDateString('zh-CN')}
{relatedArticle.data.summary}