newechoes/src/pages/articles/[...path].astro

95 lines
2.2 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 if (['grid', 'timeline'].includes(pathParts[pathParts.length - 1])) {
// 视图路径
view = pathParts.pop() || 'grid';
path = pathParts.join('/');
} else {
// 普通目录路径
path = Astro.params.path || '';
}
// 合并属性
const mergedProps = {
...props,
path,
tag,
view
};
---
<ArticlesPage {...mergedProps} />