menuConfig.ts 6.0 KB

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