--- import { getCollection } from 'astro:content'; import type { CollectionEntry } from 'astro:content'; import { contentStructure } from '../../content.config'; import Layout from "@/components/Layout.astro"; import Breadcrumb from "@/components/Breadcrumb.astro"; // 启用静态预渲染 export const prerender = true; // 获取路径参数 const path = Astro.props.path || ''; const pathSegments = path ? path.split('/') : []; // 获取所有文章,并按日期排序 const articles: CollectionEntry<'articles'>[] = await getCollection('articles'); // 获取内容结构 const { sections } = contentStructure; // 根据路径获取当前目录 function getCurrentSection(pathSegments: string[]) { // 过滤掉空字符串 const filteredSegments = pathSegments.filter(segment => segment.trim() !== ''); if (filteredSegments.length === 0) { return { sections, articles: contentStructure.articles, currentPath: '' }; } let currentSections = sections; let currentPath = ''; // 遍历路径段,逐级查找 for (let i = 0; i < filteredSegments.length; i++) { const segment = filteredSegments[i]; // 查找当前段对应的目录 const foundSection = currentSections.find(s => s.name === segment); if (!foundSection) { return { sections: [], articles: [], currentPath: '' }; } // 更新当前路径 currentPath = currentPath ? `${currentPath}/${segment}` : segment; // 如果是最后一个段,返回该目录的内容 if (i === filteredSegments.length - 1) { return { sections: foundSection.sections, articles: foundSection.articles, currentPath }; } // 否则继续向下查找 currentSections = foundSection.sections; } // 默认返回空 return { sections: [], articles: [], currentPath: '' }; } // 获取当前目录内容 const { sections: currentSections, articles: currentArticles, currentPath } = getCurrentSection(pathSegments); // 设置页面标题 const pageTitle = currentPath ? currentPath : '文章列表'; ---

{pageTitle}

{/* 上一级目录卡片 - 仅在浏览目录时显示 */} {pathSegments.length > 0 && ( 1 ? pathSegments.slice(0, -1).join('/') : ''}/`} class="group flex flex-col h-full p-5 border border-gray-200 dark:border-gray-700 rounded-xl bg-white dark:bg-gray-800 hover:shadow-xl hover:-translate-y-1 shadow-lg" data-astro-prefetch="hover">
返回上级目录
返回上一级
)} {/* 目录卡片 */} {currentSections.map(section => { // 确保目录链接正确生成 const dirLink = currentPath ? `${currentPath}/${section.name}` : section.name; return (
{section.name}
{section.sections.length > 0 && ( {section.sections.length} 个子目录 )} {section.articles.length > 0 && ( {section.articles.length} 篇文章 )}
); })} {/* 文章卡片 */} {currentArticles.map(articlePath => { // 获取文章ID - 不需要移除src/content前缀,因为contentStructure中已经是相对路径 const articleId = articlePath; // 尝试匹配文章 const article = articles.find(a => a.id === articleId); if (!article) { return (

文章不存在

原始路径: {articlePath}
文章ID: {articleId}
当前目录: {currentPath}

); } return (

{article.data.title}

{article.body && (

{article.data.summary}

)}
阅读全文
); })}
{/* 空内容提示 */} {(currentSections.length === 0 && currentArticles.length === 0) && (

此目录为空

此目录下暂无内容,请浏览其他目录或返回上一级

)}