sidebars.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { ensureLang } from '../utils/lang'
  2. import guideLocale from '../i18n/pages/guide.json'
  3. import componentLocale from '../i18n/pages/component.json'
  4. function getGuideSidebar() {
  5. return Object.fromEntries(
  6. Object.entries(guideLocale).map(([lang, val]) => [
  7. lang,
  8. Object.values(val).map((item) => mapPrefix(item, lang)),
  9. ])
  10. )
  11. }
  12. function getComponentsSideBar() {
  13. return Object.fromEntries(
  14. Object.entries(componentLocale).map(([lang, val]) => [
  15. lang,
  16. Object.values(val).map((item) => mapPrefix(item, lang, '/component')),
  17. ])
  18. )
  19. }
  20. // return sidebar with language configs.
  21. // this might create duplicated data but the overhead is ignorable
  22. const getSidebars = () => {
  23. return {
  24. '/guide/': getGuideSidebar(),
  25. '/component/': getComponentsSideBar(),
  26. }
  27. }
  28. type Item = {
  29. text: string
  30. children?: Item[]
  31. link?: string
  32. }
  33. function mapPrefix(item: Item, lang: string, prefix = '') {
  34. if (item.children && item.children.length > 0) {
  35. return {
  36. ...item,
  37. children: item.children.map((child) => mapPrefix(child, lang, prefix)),
  38. }
  39. }
  40. return {
  41. ...item,
  42. link: `${ensureLang(lang)}${prefix}${item.link}`,
  43. }
  44. }
  45. export const sidebars = getSidebars()