menuConfig.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import type { DefaultTheme } from 'vitepress/theme'
  2. interface Page {
  3. title: string;
  4. path: string;
  5. child?: Page[];
  6. collapsed?: boolean, // 添加折叠,false:显示,true:隐藏
  7. }
  8. const menu : Page[] = [
  9. {
  10. title: '硬件产品',
  11. path: '/Hardware',
  12. child: [
  13. {
  14. title: 'Gate',
  15. path: '/Gate',
  16. collapsed: false,
  17. child: [
  18. {
  19. title: 'aGate 1.0/1.1',
  20. path: '/aGate_1.0.md'
  21. },
  22. {
  23. title: 'aGate 1.2',
  24. path: '/aGate_1.2.md'
  25. },
  26. {
  27. title: 'aGate 1.3',
  28. path: '/aGate_1.3.md'
  29. },
  30. {
  31. title: 'aGate 1.3.1',
  32. path: '/aGate_1.3.1.md'
  33. },
  34. {
  35. title: 'MSA-C',
  36. path: '/MSA_C.md'
  37. },
  38. {
  39. title: 'MSA-C2',
  40. path: '/MSA_C2.md'
  41. }
  42. ]
  43. },
  44. {
  45. title: 'Power',
  46. path: '/Power',
  47. collapsed: false,
  48. child: [
  49. {
  50. title: 'aPower 1.0',
  51. path: '/aPower_1.0.md'
  52. },
  53. {
  54. title: 'aPower 1.2',
  55. path: '/aPower_1.2.md'
  56. },
  57. {
  58. title: 'aPower 1.3',
  59. path: '/aPower_1.3.md'
  60. },
  61. {
  62. title: 'aPower 2',
  63. path: '/aPower_2.md'
  64. },
  65. {
  66. title: 'aPower S',
  67. path: '/aPower_S.md'
  68. }
  69. ]
  70. },
  71. {
  72. title: '配件',
  73. path: '/Other',
  74. collapsed: false,
  75. child: [
  76. {
  77. title: 'aPbox 1.0',
  78. path: '/aPbox_1.0.md'
  79. },
  80. {
  81. title: 'aPbox 2.0',
  82. path: '/aPbox_2.0.md'
  83. },
  84. {
  85. title: 'Meter Kit',
  86. path: '/MeterKit.md'
  87. },
  88. {
  89. title: 'Generator Module',
  90. path: '/GeneratorModule.md'
  91. }
  92. ]
  93. }
  94. ]
  95. },
  96. {
  97. title: '软件功能',
  98. path: '/Software',
  99. child: [
  100. {
  101. title: '模式管理',
  102. path: '/ModeManagement.md'
  103. },
  104. {
  105. title: 'PCS',
  106. path: '/PCS.md'
  107. },
  108. {
  109. title: '黑启动',
  110. path: '/BlackStart.md'
  111. },
  112. {
  113. title: '光伏调频',
  114. path: '/PV_FreqWatt.md'
  115. },
  116. {
  117. title: '电池加热',
  118. path: '/BatteryHeating.md'
  119. },
  120. {
  121. title: 'SOC均衡',
  122. path: '/SocBalance.md'
  123. },
  124. {
  125. title: '调度配置脚本化',
  126. path: '/ScriptingForSchedulingConfiguration.md'
  127. },
  128. {
  129. title: '能量统计',
  130. path: '/EnergyStatistics.md'
  131. },
  132. {
  133. title: '在线升级(OTA)',
  134. path: '/OTA.md'
  135. },
  136. {
  137. title: '虚拟电厂(VPP)',
  138. path: '/VPP.md'
  139. }
  140. ]
  141. },
  142. {
  143. title: '系统',
  144. path: '/System',
  145. child: [
  146. {
  147. title: '系统搭配',
  148. path: '/SystemConfig.md'
  149. },
  150. {
  151. title: '系统配电',
  152. path: '/SystemDistribution.md'
  153. }
  154. ]
  155. },
  156. {
  157. title: '其他',
  158. path: '/Other',
  159. child: [
  160. {
  161. title: '资费套餐模版',
  162. path: '/TariffScheduleTemplates.md'
  163. }
  164. ]
  165. }
  166. ]
  167. type NavItem = DefaultTheme.NavItem
  168. type ChildNavItem = (DefaultTheme.NavItemComponent | DefaultTheme.NavItemChildren | DefaultTheme.NavItemWithLink)
  169. type SubChildNavItem = DefaultTheme.NavItemWithLink
  170. type SidebarItem = DefaultTheme.SidebarItem
  171. type SidebarMulti = DefaultTheme.SidebarMulti
  172. interface MenuConfig {
  173. nav?: NavItem[],
  174. sidebar?: SidebarMulti
  175. }
  176. // 遍历导航栏,items最大深度3
  177. function traverseNavigation(pages: Page[]): NavItem[] | undefined {
  178. const navItem = pages.map<NavItem>(item => {
  179. if (item.child && item.child.length > 0) {
  180. const parentPath = item.path
  181. const childNavItem = item.child.map<ChildNavItem>(childItem => {
  182. if (childItem.child && childItem.child.length > 0) {
  183. const childPath = parentPath + childItem.path
  184. const subChildItem = childItem.child.map<SubChildNavItem>(subChildItem => {
  185. return {
  186. text: subChildItem.title,
  187. link: childPath + subChildItem.path
  188. }
  189. })
  190. return {
  191. text: childItem.title,
  192. items: subChildItem
  193. }
  194. } else {
  195. return {
  196. text: childItem.title,
  197. link: parentPath + childItem.path
  198. }
  199. }
  200. })
  201. return {
  202. text: item.title,
  203. items: childNavItem
  204. }
  205. } else {
  206. return {
  207. text: item.title,
  208. link: item.path
  209. }
  210. }
  211. })
  212. if (navItem.length === 0) {
  213. return undefined
  214. } else {
  215. return navItem
  216. }
  217. }
  218. function traverseSidebar(path: string, pages: Page[]): SidebarItem[] {
  219. let config : SidebarItem[] = []
  220. for (const page of pages) {
  221. // 将当前页面加入配置中
  222. if (page.child && page.child.length > 0) {
  223. const childConfig = traverseSidebar(path + page.path, page.child); // 递归遍历子页面
  224. config.push({
  225. text: page.title,
  226. link: path + page.path,
  227. collapsed: page.collapsed,
  228. items: childConfig
  229. })
  230. } else {
  231. config.push({
  232. text: page.title,
  233. link: path + page.path,
  234. collapsed: page.collapsed
  235. })
  236. }
  237. }
  238. return config
  239. }
  240. function traverseMainSidebar(pages: Page[]): SidebarMulti {
  241. let config : SidebarMulti = {}
  242. for (const page of pages) {
  243. // 将当前页面加入配置中
  244. if (page.child && page.child.length > 0) {
  245. const childConfig = traverseSidebar("", page.child); // 递归遍历子页面
  246. config[page.path] = {
  247. base: page.path,
  248. items: [
  249. {
  250. text: page.title,
  251. items: childConfig
  252. }
  253. ]
  254. }
  255. } else {
  256. config[page.path] = {
  257. base: page.path,
  258. items: [
  259. {
  260. text: page.title
  261. }
  262. ]
  263. }
  264. }
  265. }
  266. return config
  267. }
  268. const config : MenuConfig = {
  269. nav: traverseNavigation(menu),
  270. // sidebar: [
  271. // {
  272. // text: '目录',
  273. // items: traverseSidebar('', menu)
  274. // }
  275. // ]
  276. sidebar: traverseMainSidebar(menu)
  277. }
  278. export default config