From 339e637a50d6e09eb4d499f40e0c283105f84309 Mon Sep 17 00:00:00 2001 From: lsy Date: Mon, 10 Mar 2025 17:35:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=BA=A7=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/articles/[...id].astro | 116 ++++++++++------------------- src/pages/articles/[...path].astro | 21 ++++-- 2 files changed, 53 insertions(+), 84 deletions(-) diff --git a/src/pages/articles/[...id].astro b/src/pages/articles/[...id].astro index 67c3d8c..72d01d0 100644 --- a/src/pages/articles/[...id].astro +++ b/src/pages/articles/[...id].astro @@ -12,92 +12,54 @@ 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 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 view of views) { + } + + // 为每个可能的路径生成路由 + for (const path of possiblePaths) { + // 添加基本路由 paths.push({ - params: { id: `${specialId}/${view}` }, + params: { id: path }, props: { - article, - section: article.section, - originalId: specialId !== article.id ? article.id : undefined, - view + 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; } diff --git a/src/pages/articles/[...path].astro b/src/pages/articles/[...path].astro index 5c1e944..fdb3fa6 100644 --- a/src/pages/articles/[...path].astro +++ b/src/pages/articles/[...path].astro @@ -66,21 +66,28 @@ const { props } = Astro; // 解析路径参数 const pathParts = (Astro.params.path as string | undefined)?.split('/') || []; + +// 初始化变量 let path = ''; let tag = ''; let view = 'grid'; if (pathParts[0] === 'tag' && pathParts.length >= 2) { - // 标签路径 + // 标签路径处理 tag = pathParts[1]; view = pathParts[2] || 'grid'; -} else if (['grid', 'timeline'].includes(pathParts[pathParts.length - 1])) { - // 视图路径 - view = pathParts.pop() || 'grid'; - path = pathParts.join('/'); } else { - // 普通目录路径 - path = Astro.params.path || ''; + // 处理普通路径和视图 + const lastPart = pathParts[pathParts.length - 1]; + + if (['grid', 'timeline'].includes(lastPart)) { + // 如果最后一部分是视图类型,则移除它并设置视图 + view = lastPart; + pathParts.pop(); + } + + // 剩余的部分都作为路径 + path = pathParts.join('/'); } // 合并属性