Skip to content

Commit 3a53a8e

Browse files
committed
限制sidebar最多深入两层章节
1 parent 40920da commit 3a53a8e

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

docs/.vitepress/config.mts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { defineConfig } from "vitepress";
2-
import { ChapterItems, Chapters } from "./theme/constrants/route";
2+
import { Chapters, getChapterItems } from "./theme/constrants/route";
33
import autoH1 from "./plugins/autoH1";
44

5+
// 限制sidebar最多深入两层章节
6+
const sidebarItems = getChapterItems(1, 2);
7+
58
// https://vitepress.dev/reference/site-config
69
export default defineConfig({
710
title: "灵矽文档中心",
@@ -27,18 +30,7 @@ export default defineConfig({
2730
{ text: "API参考", link: Chapters.xrobot_api },
2831
{ text: "FAQ", link: Chapters.xrobot_faq },
2932
],
30-
sidebar: {
31-
"/": [
32-
{
33-
text: "Examples",
34-
items: [
35-
{ text: "Markdown Examples", link: "/markdown-examples" },
36-
{ text: "Runtime API Examples", link: "/api-examples" },
37-
],
38-
},
39-
],
40-
...ChapterItems,
41-
},
33+
sidebar: sidebarItems,
4234
socialLinks: [
4335
{
4436
icon: "github",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const { chapter: chapter_root, root = true } = defineProps<{
1616
let chapter_name: string[] = [];
1717
let tocs: { link: string; text: string }[][] = [];
1818
19-
console.log("chapter_root", chapter_root);
20-
console.log("ChapterItems[chapter_root]", ChapterItems[chapter_root]);
19+
// console.log("chapter_root", chapter_root);
20+
// console.log("ChapterItems[chapter_root]", ChapterItems[chapter_root]);
2121
2222
ChapterItems[chapter_root]?.forEach((subchapter) => {
2323
const t = subchapter.items?.filter((item) => {
@@ -29,8 +29,8 @@ ChapterItems[chapter_root]?.forEach((subchapter) => {
2929
}
3030
});
3131
32-
console.log("chapter_name:", chapter_name);
33-
console.log("tocs:", tocs);
32+
// console.log("chapter_name:", chapter_name);
33+
// console.log("tocs:", tocs);
3434
</script>
3535

3636
<template>

docs/.vitepress/theme/constrants/route.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ export enum Chapters {
1919
// xrobot 主章节
2020
xrobot = "/xrobot/",
2121
// guide
22-
xrobot_guide = "/xrobot/guide",
23-
xrobot_guide_mp = "/xrobot/guide/mini-program",
22+
xrobot_guide = "/xrobot/guide/",
23+
xrobot_guide_mp = "/xrobot/guide/mini-program/",
2424
xrobot_guide_device = "/xrobot/guide/device/",
25-
xrobot_guide_console = "/xrobot/guide/console",
26-
xrobot_guide_net_config = "/xrobot/guide/net-config",
25+
xrobot_guide_console = "/xrobot/guide/console/",
26+
xrobot_guide_net_config = "/xrobot/guide/net-config/",
2727
// api
2828
xrobot_api = "/xrobot/api/",
2929
xrobot_api_server = "/xrobot/api/server/",
3030
xrobot_api_client = "/xrobot/api/client/",
3131
// platform
32-
xrobot_platform = "/xrobot/platform",
33-
xrobot_platform_esp32 = "/xrobot/platform/esp32",
32+
xrobot_platform = "/xrobot/platform/",
33+
xrobot_platform_esp32 = "/xrobot/platform/esp32/",
3434
// faq
3535
xrobot_faq = "/xrobot/faq/",
3636
}
@@ -42,6 +42,11 @@ export function isChapter<T extends Record<string, string>>(
4242
return Object.values(Chapters).includes(link as Chapters);
4343
}
4444

45+
export function isSubChapter(link: string) {
46+
const t = link.split("/").filter((p) => p !== "");
47+
return t.length > 1 && isChapter(link);
48+
}
49+
4550
// // 给 ChapterItem 的 link 字段追加当前章节的 link 前缀
4651
// // 通过是否包含子items来判断
4752
// export function apply_prefix2(item: ChapterItem, prefix: Chapters) {
@@ -264,3 +269,23 @@ export const ChapterItems: Record<Chapters, ChapterItem[]> = {
264269
// faq
265270
[Chapters.xrobot_faq]: [gobackItem(Chapters.xrobot), ...items_xrobot_faq],
266271
};
272+
273+
// 获得章节items, 所有level~maxLevel层级的章节
274+
export function getChapterItems(
275+
level?: 1 | 2 | 3 | 4 | 5,
276+
maxLevel?: 1 | 2 | 3 | 4 | 5
277+
) {
278+
const _level = level ?? 1;
279+
const _maxLevel = maxLevel ?? 6;
280+
// 满足条件的ChapterItems的key
281+
const chap = Object.values(Chapters).filter((ch) => {
282+
const len = ch.split("/").filter((p) => p !== "").length;
283+
return _level <= len && len <= _maxLevel;
284+
});
285+
286+
let res = {};
287+
chap.forEach((ch) => {
288+
res[ch] = ChapterItems[ch];
289+
});
290+
return res;
291+
}

0 commit comments

Comments
 (0)