--- import { getCollection, render } from 'astro:content'; import { getSpecialPath } from '@/content.config'; import Layout from '@/components/Layout.astro'; import Breadcrumb from '@/components/Breadcrumb.astro'; import { ARTICLE_EXPIRY_CONFIG } from '@/consts'; // 添加这一行,告诉Astro预渲染这个页面 export const prerender = true; export async function getStaticPaths() { const articles = await getCollection('articles'); const views = ['grid', 'timeline']; // 为每篇文章生成路由参数 const paths = []; for (const article of articles) { // 获取所有可能的路径形式 const possiblePaths = new Set([ article.id // 只保留原始路径 ]); // 如果是多级目录,检查是否需要特殊处理 if (article.id.includes('/')) { const parts = article.id.split('/'); const fileName = parts[parts.length - 1]; const dirName = parts[parts.length - 2]; // 只有当文件名与其父目录名相同时才添加特殊路径 if (fileName === dirName) { possiblePaths.add(getSpecialPath(article.id)); } } // 为每个可能的路径生成路由 for (const path of possiblePaths) { // 添加基本路由 paths.push({ params: { id: path }, props: { article, section: article.id.includes('/') ? article.id.split('/').slice(0, -1).join('/') : '', originalId: path !== article.id ? article.id : undefined, view: undefined } }); // 为每个视图添加路由 for (const view of views) { paths.push({ params: { id: `${path}/${view}` }, props: { article, section: article.id.includes('/') ? article.id.split('/').slice(0, -1).join('/') : '', originalId: path !== 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}

{/* 显示文章所在目录 */} {section && ( {section} )}
{article.data.tags && article.data.tags.length > 0 && (
{article.data.tags.map(tag => ( #{tag} ))}
)}
{(() => { const publishDate = article.data.date; const currentDate = new Date(); const daysDiff = Math.floor((currentDate.getTime() - publishDate.getTime()) / (1000 * 60 * 60 * 24)); if (ARTICLE_EXPIRY_CONFIG.enabled && daysDiff > ARTICLE_EXPIRY_CONFIG.expiryDays) { return (

{ARTICLE_EXPIRY_CONFIG.warningMessage}

); } return null; })()}
{relatedArticles.length > 0 && ( )}