File size: 1,058 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { NavigationSchema } from '../../scripts/docs/navigation'
import navigationSchema from '../data/navigationSchema.generated.json'
import subnavSchema from '../data/subnavSchema.generated.json'

export const getNavigations = (dir: string) => {
  const dirWithoutDocs = `/${dir.split('/').slice(2).join('/')}`

  let subnav = subnavSchema[dirWithoutDocs as keyof typeof subnavSchema]

  if (!subnav) {
    const dirWithIndex =
      dirWithoutDocs === '/' ? '/index' : `${dirWithoutDocs}/index`
    subnav = subnavSchema[dirWithIndex as keyof typeof subnavSchema]
  }

  return {
    sidebar: navigationSchema,
    subnav,
  }
}

export interface FlattenedNavigation {
  href: string
  hasChildren: boolean
}

export const flattenNavigationWithChildren = (
  array: NavigationSchema
): FlattenedNavigation[] =>
  array.reduce(
    (flattened, { children, href }) =>
      flattened
        .concat([{ href, hasChildren: children.length > 0 }])
        .concat(children ? flattenNavigationWithChildren(children) : []),
    [] as FlattenedNavigation[]
  )