修改slug处理首页网址的逻辑

This commit is contained in:
2025-07-28 21:30:54 +08:00
parent 0e2120ea0c
commit 12d7765e10
3 changed files with 2 additions and 199 deletions

View File

@@ -18,37 +18,9 @@ const pageFontSizeClass = computed(() => {
return `text-${selectedFontSize.value}`
})
// 根据路由参数构建内容路径
const path = computed(() => {
const slug = route.params.slug;
// 处理 slug 参数
if (!slug) {
return '/'; // 如果没有 slug返回根路径
}
const pathValue = Array.isArray(slug) ? slug.join("/") : slug;
// 确保路径以 / 开头,不以 / 结尾
const normalizedPath = `/${pathValue}`.replace(/\/+$/, ""); // 使用 /+ 匹配多个连续的斜杠
return normalizedPath;
});
// URL 解码并验证路径
const queryPath = computed(() => {
try {
return decodeURIComponent(path.value);
} catch (error) {
console.error('URL decode error:', error);
return path.value; // 如果解码失败,返回原始路径
}
});
const { data: page } = await useAsyncData(
`page-${route.path}`, // 使用更具体的 key
() => queryCollection('docs').path(queryPath.value).first(),
() => queryCollection('docs').path(decodeURI(route.path)).first(),
{
default: () => null // 提供默认值
}