Skip to content

Commit 92a392d

Browse files
authored
Merge pull request #3 from srsng/main
修复目录生成组件缺失base路径导致跳转失败的问题
2 parents 86ccbb1 + cabf454 commit 92a392d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

docs/.vitepress/theme/components/ChapterContents.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ import {
66
isChapter,
77
} from "../../../.vitepress/theme/constrants/route";
88
9+
function apply_prefix(link: string, prefix: string) {
10+
if (!prefix) return link;
11+
if (link.startsWith("/") && prefix.endsWith("/")) {
12+
return prefix.slice(0, -1) + link;
13+
} else if (!link.startsWith("/") && !prefix.endsWith("/")) {
14+
return prefix + "/" + link;
15+
}
16+
return prefix + link;
17+
}
18+
919
const { chapter: chapter_root, root = true } = defineProps<{
1020
// 参数chapter应该是如 Chapter.xrobot_device这样的
1121
chapter: Chapters;
1222
// root用于控制递归生成目录
1323
root?: boolean;
1424
}>();
1525
26+
const { site } = useData();
27+
const base = site.value.base;
28+
1629
// console.log("contents");
1730
let chapter_name: string[] = [];
1831
let tocs: { link: string; text: string }[][] = [];
@@ -61,7 +74,7 @@ if (!items) {
6174
:chapter="item.link as Chapters"
6275
></ChapterContents>
6376
</ol>
64-
<a v-else :href="item.link">{{ item.text }}</a>
77+
<a v-else :href="apply_prefix(item.link, base)">{{ item.text }}</a>
6578
</li>
6679
</ol>
6780
</div>

0 commit comments

Comments
 (0)