Skip to content

Commit d212329

Browse files
committed
修改章节目录组件数据源
1 parent cf7e1c8 commit d212329

File tree

2 files changed

+48
-50
lines changed

2 files changed

+48
-50
lines changed

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

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@ import { useData } from "vitepress";
33
import {
44
ChapterItems,
55
Chapters,
6-
isChapter,
76
} from "../../../.vitepress/theme/constrants/route";
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-
}
7+
import Contents from "./Contents.vue";
188
199
const { chapter: chapter_root, root = true } = defineProps<{
2010
// 参数chapter应该是如 Chapter.xrobot_device这样的
@@ -23,16 +13,6 @@ const { chapter: chapter_root, root = true } = defineProps<{
2313
root?: boolean;
2414
}>();
2515
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-
3616
const items = ChapterItems[chapter_root];
3717
if (!items) {
3818
const { page } = useData();
@@ -44,39 +24,11 @@ if (!items) {
4424
"title:",
4525
page.value.title
4626
);
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-
});
5827
}
59-
60-
// console.log("chapter_name:", chapter_name);
61-
// console.log("tocs:", tocs);
6228
</script>
6329

6430
<template>
6531
<h2 v-if="root">目录</h2>
6632
<slot name="header"></slot>
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>
33+
<Contents :chapter_data="items" :root="true"></Contents>
8234
</template>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<script setup lang="ts">
2+
import { useData } from "vitepress";
3+
import { apply_prefix } from "../utils";
4+
import {
5+
ChapterItem,
6+
isChapter,
7+
} from "../../../.vitepress/theme/constrants/route";
8+
9+
const { chapter_data = [{ text: "", link: "" }], root = true } = defineProps<{
10+
chapter_data: ChapterItem[];
11+
// root用于控制递归生成目录
12+
root?: boolean;
13+
}>();
14+
15+
const { site } = useData();
16+
const base = site.value.base;
17+
18+
console.log("chapter_data", chapter_data);
19+
20+
// 过滤
21+
const items: ChapterItem[] = chapter_data.filter((sub) => {
22+
return !sub.goback;
23+
});
24+
</script>
25+
26+
<template>
27+
<div v-if="items.length === 0"><span>暂无内容</span></div>
28+
<div v-for="(subchapter, index) in items">
29+
<h3 v-if="isChapter(subchapter.link)">
30+
{{ chapter_data[index].text }}
31+
</h3>
32+
<a v-else :href="apply_prefix(subchapter.link, base)">{{
33+
subchapter.text
34+
}}</a>
35+
<ol>
36+
<div v-if="items.length === 0"><span>暂无内容</span></div>
37+
<div v-else :key="subchapter.link">
38+
<Contents
39+
v-if="isChapter(subchapter.link)"
40+
:root="false"
41+
:chapter_data="subchapter.items as ChapterItem[]"
42+
></Contents>
43+
</div>
44+
</ol>
45+
</div>
46+
</template>

0 commit comments

Comments
 (0)