手动解码中文路径

This commit is contained in:
2025-07-30 18:53:57 +08:00
parent fff9f214f2
commit 192c80537f

View File

@@ -18,9 +18,36 @@ 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(decodeURI(route.path)).first(),
() => queryCollection('docs').path(queryPath.value).first(),
{
default: () => null // 提供默认值
}