menuConfig.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. },
  134. {
  135. title: '系统',
  136. path: '/System',
  137. child: [
  138. {
  139. title: '系统搭配',
  140. path: '/SystemConfig.md'
  141. },
  142. {
  143. title: '系统配电',
  144. path: '/SystemDistribution.md'
  145. }
  146. ]
  147. },
  148. {
  149. title: '其他',
  150. path: '/Other',
  151. child: [
  152. {
  153. title: '资费套餐模版',
  154. path: '/TariffScheduleTemplates.md'
  155. }
  156. ]
  157. }
  158. ]
  159. type NavItem = DefaultTheme.NavItem
  160. type ChildNavItem = (DefaultTheme.NavItemComponent | DefaultTheme.NavItemChildren | DefaultTheme.NavItemWithLink)
  161. type SubChildNavItem = DefaultTheme.NavItemWithLink
  162. type SidebarItem = DefaultTheme.SidebarItem
  163. type SidebarMulti = DefaultTheme.SidebarMulti
  164. interface MenuConfig {
  165. nav?: NavItem[],
  166. sidebar?: SidebarMulti
  167. }
  168. // 遍历导航栏,items最大深度3
  169. function traverseNavigation(pages: Page[]): NavItem[] | undefined {
  170. const navItem = pages.map<NavItem>(item => {
  171. if (item.child && item.child.length > 0) {
  172. const parentPath = item.path
  173. const childNavItem = item.child.map<ChildNavItem>(childItem => {
  174. if (childItem.child && childItem.child.length > 0) {
  175. const childPath = parentPath + childItem.path
  176. const subChildItem = childItem.child.map<SubChildNavItem>(subChildItem => {
  177. return {
  178. text: subChildItem.title,
  179. link: childPath + subChildItem.path
  180. }
  181. })
  182. return {
  183. text: childItem.title,
  184. items: subChildItem
  185. }
  186. } else {
  187. return {
  188. text: childItem.title,
  189. link: parentPath + childItem.path
  190. }
  191. }
  192. })
  193. return {
  194. text: item.title,
  195. items: childNavItem
  196. }
  197. } else {
  198. return {
  199. text: item.title,
  200. link: item.path
  201. }
  202. }
  203. })
  204. if (navItem.length === 0) {
  205. return undefined
  206. } else {
  207. return navItem
  208. }
  209. }
  210. function traverseSidebar(path: string, pages: Page[]): SidebarItem[] {
  211. let config : SidebarItem[] = []
  212. for (const page of pages) {
  213. // 将当前页面加入配置中
  214. if (page.child && page.child.length > 0) {
  215. const childConfig = traverseSidebar(path + page.path, page.child); // 递归遍历子页面
  216. config.push({
  217. text: page.title,
  218. link: path + page.path,
  219. collapsed: page.collapsed,
  220. items: childConfig
  221. })
  222. } else {
  223. config.push({
  224. text: page.title,
  225. link: path + page.path,
  226. collapsed: page.collapsed
  227. })
  228. }
  229. }
  230. return config
  231. }
  232. function traverseMainSidebar(pages: Page[]): SidebarMulti {
  233. let config : SidebarMulti = {}
  234. for (const page of pages) {
  235. // 将当前页面加入配置中
  236. if (page.child && page.child.length > 0) {
  237. const childConfig = traverseSidebar("", page.child); // 递归遍历子页面
  238. config[page.path] = {
  239. base: page.path,
  240. items: [
  241. {
  242. text: page.title,
  243. items: childConfig
  244. }
  245. ]
  246. }
  247. } else {
  248. config[page.path] = {
  249. base: page.path,
  250. items: [
  251. {
  252. text: page.title
  253. }
  254. ]
  255. }
  256. }
  257. }
  258. return config
  259. }
  260. const config : MenuConfig = {
  261. nav: traverseNavigation(menu),
  262. // sidebar: [
  263. // {
  264. // text: '目录',
  265. // items: traverseSidebar('', menu)
  266. // }
  267. // ]
  268. sidebar: traverseMainSidebar(menu)
  269. }
  270. export default config