增加多级目录

This commit is contained in:
lsy 2025-03-10 17:35:21 +08:00
parent f110beb5dc
commit 339e637a50
2 changed files with 53 additions and 84 deletions

View File

@ -12,74 +12,35 @@ 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);
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: specialId },
params: { id: path },
props: {
article,
section: article.section,
originalId: specialId !== article.id ? article.id : undefined,
section: article.id.includes('/') ? article.id.split('/').slice(0, -1).join('/') : '',
originalId: path !== article.id ? article.id : undefined,
view: undefined
}
});
@ -87,16 +48,17 @@ export async function getStaticPaths() {
// 为每个视图添加路由
for (const view of views) {
paths.push({
params: { id: `${specialId}/${view}` },
params: { id: `${path}/${view}` },
props: {
article,
section: article.section,
originalId: specialId !== article.id ? article.id : undefined,
section: article.id.includes('/') ? article.id.split('/').slice(0, -1).join('/') : '',
originalId: path !== article.id ? article.id : undefined,
view
}
});
}
}
}
return paths;
}

View File

@ -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('/');
}
// 合并属性