newechoes/src/pages/articles/[...path].astro
2025-03-10 17:35:21 +08:00

102 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
import ArticlesPage, { getStaticPaths as getOriginalPaths } from './index.astro';
// 重新导出 getStaticPaths处理所有路径模式
export async function getStaticPaths() {
const paths = await getOriginalPaths();
const allPaths = paths.map(({ props }) => {
const results = [];
// 1. 如果有标签,添加标签路径
if (props.tag) {
// 标签主页
results.push({
params: { path: `tag/${props.tag}` },
props: { ...props }
});
// 标签视图页
results.push({
params: { path: `tag/${props.tag}/grid` },
props: { ...props, view: 'grid' }
});
results.push({
params: { path: `tag/${props.tag}/timeline` },
props: { ...props, view: 'timeline' }
});
}
// 2. 如果有路径,添加目录路径
if (props.path) {
// 目录主页
results.push({
params: { path: props.path },
props: { ...props }
});
// 目录视图页
results.push({
params: { path: `${props.path}/grid` },
props: { ...props, view: 'grid' }
});
results.push({
params: { path: `${props.path}/timeline` },
props: { ...props, view: 'timeline' }
});
}
return results;
}).flat();
// 添加顶级视图路径
allPaths.push(
{
params: { path: 'grid' },
props: { path: '', tag: '', view: 'grid' }
},
{
params: { path: 'timeline' },
props: { path: '', tag: '', view: 'timeline' }
}
);
return allPaths;
}
// 使用主页面组件
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 {
// 处理普通路径和视图
const lastPart = pathParts[pathParts.length - 1];
if (['grid', 'timeline'].includes(lastPart)) {
// 如果最后一部分是视图类型,则移除它并设置视图
view = lastPart;
pathParts.pop();
}
// 剩余的部分都作为路径
path = pathParts.join('/');
}
// 合并属性
const mergedProps = {
...props,
path,
tag,
view
};
---
<ArticlesPage {...mergedProps} />