|
| 1 | +export type ChapterItem = { |
| 2 | + // 标题 |
| 3 | + text: string; |
| 4 | + // 链接 |
| 5 | + link: string; |
| 6 | + // 初始时是否折叠, 如果未指定,侧边栏组不可折叠 |
| 7 | + collapsed?: boolean; |
| 8 | + // 子项 |
| 9 | + items?: ChapterItem[]; |
| 10 | + // 返回上级章节 |
| 11 | + goback?: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +// 章节路由,注意,首尾都要有 `/` |
| 15 | +// 不在其中的章节不会正确生成目录 |
| 16 | +export enum Chapters { |
| 17 | + // xrobot 分章 |
| 18 | + xrobot = "/xrobot/", |
| 19 | + xrobot_device = "/xrobot/device/", |
| 20 | + xrobot_api = "/xrobot/api/", |
| 21 | +} |
| 22 | + |
| 23 | +// 判断一个link是否是章节link |
| 24 | +export function isChapter<T extends Record<string, string>>( |
| 25 | + link: string |
| 26 | +): link is T[keyof T] { |
| 27 | + return Object.values(Chapters).includes(link); |
| 28 | +} |
| 29 | + |
| 30 | +function apply_prefix(item: ChapterItem, prefix: Chapters) { |
| 31 | + if (item?.link.startsWith("/") && prefix.endsWith("/")) { |
| 32 | + return { ...item, link: prefix.slice(0, -1) + item.link }; |
| 33 | + } else if (!item.link.startsWith("/") && !prefix.endsWith("/")) { |
| 34 | + return { ...item, link: prefix + "/" + item.link }; |
| 35 | + } |
| 36 | + return { ...item, link: prefix + item.link }; |
| 37 | +} |
| 38 | + |
| 39 | +const items_xrobot_api = [ |
| 40 | + { |
| 41 | + text: "API参考", |
| 42 | + items: [ |
| 43 | + // { text: "章节目录", link: "" }, |
| 44 | + { text: "绑定设备", link: "api" }, |
| 45 | + { text: "音色克隆", link: "voice-clone" }, |
| 46 | + ].map((item) => apply_prefix(item, Chapters.xrobot_api)), |
| 47 | + collapsed: false, |
| 48 | + link: Chapters.xrobot_api, |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +const items_xrobot_device = [ |
| 53 | + { |
| 54 | + text: "设备指南", |
| 55 | + items: [ |
| 56 | + // { text: "章节目录", link: "" }, |
| 57 | + { text: "设备使用指南", link: "device-intro" }, |
| 58 | + { text: "设备绑定", link: "device-bind" }, |
| 59 | + { text: "设备服务通信协议", link: "device-protocol" }, |
| 60 | + { text: "智能体连接指南", link: "device-connection" }, |
| 61 | + ].map((item) => apply_prefix(item, Chapters.xrobot_device)), |
| 62 | + link: Chapters.xrobot_device, |
| 63 | + collapsed: false, |
| 64 | + }, |
| 65 | + // 子章节 |
| 66 | +]; |
| 67 | + |
| 68 | +// xrobot章节整体 |
| 69 | +const items_xrobot = [ |
| 70 | + { |
| 71 | + text: "Xrobot", |
| 72 | + items: [...items_xrobot_api, ...items_xrobot_device], |
| 73 | + link: Chapters.xrobot, |
| 74 | + collapsed: false, |
| 75 | + }, |
| 76 | +]; |
| 77 | + |
| 78 | +function gobackItem(chapter: Chapters) { |
| 79 | + return { |
| 80 | + text: "返回上级", |
| 81 | + link: chapter, |
| 82 | + goback: true, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +// todo: 把子章节从ChapterItems中抽离出来 |
| 87 | +export const ChapterItems: Record<Chapters, ChapterItem[]> = { |
| 88 | + [Chapters.xrobot]: items_xrobot, |
| 89 | + [Chapters.xrobot_device]: [ |
| 90 | + gobackItem(Chapters.xrobot), |
| 91 | + ...items_xrobot_device, |
| 92 | + ], |
| 93 | + [Chapters.xrobot_api]: [gobackItem(Chapters.xrobot), ...items_xrobot_api], |
| 94 | +}; |
0 commit comments