--- 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 searchParams = Astro.url.searchParams; const path = Astro.props.path || ''; // 视图类型判断逻辑: // 1. 如果props中有明确指定pageType,则使用props的值 // 2. 否则: // a. 访问 /articles/ (带尾斜杠) 或其他目录路径 - 使用网格视图 // b. 访问 /articles (不带尾斜杠) - 使用筛选视图 // c. 如果有path属性(表示是目录浏览),也使用网格视图 const currentUrl = Astro.url.pathname; const isRootWithSlash = currentUrl === '/articles/'; const isGridView = isRootWithSlash || (currentUrl.startsWith('/articles/') && currentUrl !== '/articles'); const pageType = Astro.props.pageType || (isGridView || path ? 'grid' : 'filter'); const pathSegments = path ? path.split('/') : []; // 获取所有文章,并按日期排序 const articles: CollectionEntry<'articles'>[] = await getCollection('articles'); const sortedArticles = articles.sort( (a, b) => b.data.date.getTime() - a.data.date.getTime() ); // 获取所有标签 const allTags = articles.flatMap(article => article.data.tags || []); const tags = [...new Set(allTags)].sort(); // 获取内容结构 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); // 如果有标签过滤,则过滤文章 let filteredArticles = sortedArticles; let pageTitle = currentPath ? currentPath : '文章列表'; // 使用URL搜索参数中的标签进行过滤 const tagFilter = searchParams.get('tags'); if (tagFilter) { filteredArticles = sortedArticles.filter(article => article.data.tags && article.data.tags.includes(tagFilter) ); pageTitle = `标签: ${tagFilter}`; } // 处理文章链接 function getArticleUrl(articleId: string) { return searchParams.toString() ? `/articles/${articleId}?${searchParams.toString()}` : `/articles/${articleId}`; } ---

{pageTitle}

{pageType === 'grid' ? ( <>
{/* 上一级目录卡片 - 仅在浏览目录时显示 */} {!tagFilter && 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">
返回上级目录
返回上一级
)} {/* 目录卡片 - 仅在浏览目录时显示 */} {!tagFilter && 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} 篇文章 )}
); })} {/* 文章卡片 - 根据是否有标签过滤显示不同内容 */} {tagFilter ? ( // 显示标签过滤后的文章 filteredArticles.map(article => (

{article.data.title}

{article.body && (

{article.data.summary}

)}
阅读全文
{article.data.tags && article.data.tags.length > 0 && (
{article.data.tags.map(tag => ( #{tag} ))}
)}
)) ) : ( // 显示当前目录的文章 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}

可用的文章ID:
{articles.map(a => a.id).join(', ')}
); } return (

{article.data.title}

{article.body && (

{article.data.summary}

)}
阅读全文
{article.data.tags && article.data.tags.length > 0 && (
{article.data.tags.map(tag => ( #{tag} ))}
)}
); }) )}
{/* 空内容提示 */} {((tagFilter && filteredArticles.length === 0) || (!tagFilter && currentSections.length === 0 && currentArticles.length === 0)) && (

{tagFilter ? `没有找到标签为 "${tagFilter}" 的文章` : '此目录为空'}

{tagFilter ? '请尝试其他标签或返回文章列表' : '此目录下暂无内容,请浏览其他目录或返回上一级'}

)} ) : pageType === 'filter' ? (

文章筛选

已选筛选条件:
无筛选条件,显示全部文章
显示 0 篇文章(共 {sortedArticles.length} 篇)
{sortedArticles.map(article => (

{article.data.title}

{article.body && (

{article.data.summary}

)}
阅读全文
{article.data.tags && article.data.tags.length > 0 && (
{article.data.tags.map(tag => ( #{tag} ))}
)}
))}
) : ( // 默认视图(参数错误时显示)

无效的视图模式

您请求的视图模式"{pageType}"不存在,请选择其他视图。

) }