Skip to content

Commit 4cf5a8e

Browse files
committed
Revert "修改章节目录组件数据源"
This reverts commit d212329.
1 parent 34ba4fd commit 4cf5a8e

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import { useData } from "vitepress";
33
import {
44
ChapterItems,
55
Chapters,
6+
isChapter,
67
} from "../../../.vitepress/theme/constrants/route";
7-
import Contents from "./Contents.vue";
8+
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+
}
818
919
const { chapter: chapter_root, root = true } = defineProps<{
1020
// 参数chapter应该是如 Chapter.xrobot_device这样的
@@ -13,6 +23,16 @@ const { chapter: chapter_root, root = true } = defineProps<{
1323
root?: boolean;
1424
}>();
1525
26+
const { site } = useData();
27+
const base = site.value.base;
28+
29+
// console.log("contents");
30+
let chapter_name: string[] = [];
31+
let tocs: { link: string; text: string }[][] = [];
32+
33+
// console.log("chapter_root", chapter_root);
34+
// console.log("ChapterItems[chapter_root]", ChapterItems[chapter_root]);
35+
1636
const items = ChapterItems[chapter_root];
1737
if (!items) {
1838
const { page } = useData();
@@ -24,11 +44,39 @@ if (!items) {
2444
"title:",
2545
page.value.title
2646
);
47+
} else {
48+
items.forEach((subchapter) => {
49+
// console.log(subchapter);
50+
const t = subchapter.items?.filter((item) => {
51+
return item.link !== chapter_root && !item.goback;
52+
});
53+
if (t) {
54+
tocs.push(t);
55+
chapter_name.push(subchapter.text);
56+
}
57+
});
2758
}
59+
60+
// console.log("chapter_name:", chapter_name);
61+
// console.log("tocs:", tocs);
2862
</script>
2963

3064
<template>
3165
<h2 v-if="root">目录</h2>
3266
<slot name="header"></slot>
33-
<Contents :chapter_data="items" :root="true"></Contents>
67+
<div v-for="(subchapter, index) in tocs">
68+
<h3>{{ chapter_name[index] }}</h3>
69+
<div v-if="subchapter.length === 0"><span>暂无内容</span></div>
70+
<ol v-else>
71+
<li v-for="(item, index2) in subchapter" :key="item.link">
72+
<ol v-if="isChapter(item.link)">
73+
<ChapterContents
74+
:root="false"
75+
:chapter="item.link as Chapters"
76+
></ChapterContents>
77+
</ol>
78+
<a v-else :href="apply_prefix(item.link, base)">{{ item.text }}</a>
79+
</li>
80+
</ol>
81+
</div>
3482
</template>

docs/.vitepress/theme/components/Contents.vue

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)