forked from npm/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (47 loc) · 1.47 KB
/
index.js
File metadata and controls
55 lines (47 loc) · 1.47 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react'
// eslint-disable-next-line import/no-unresolved
import { Location } from '@reach/router'
import { Box, Link } from '@primer/components'
import { Link as GatsbyLink } from 'gatsby'
import NavHierarchy from '../nav-hierarchy'
function showHierarchy (items, props, depth = 1) {
let hierarchy
if (props.depth && depth > props.depth) {
return null
}
return (
<Box as="ul">
{items.map(item => (
<Box as="li" key={item.url}>
<Link as={GatsbyLink} key={item.title} to={item.url}>{item.title}</Link>
{item.description != null ? (
<>
<Box style={{ fontSize: '0.85em', marginBottom: '0.5em' }}>
{item.description}
</Box>
</>
) : null}
{(hierarchy = NavHierarchy.getHierarchy(item, props)) != null ? showHierarchy(hierarchy, props, depth + 1) : null}
</Box>
))}
</Box>
)
}
function Index (props) {
return (
<Location>
{({ location }) => {
const path = NavHierarchy.getLocation(location.pathname)
let root = props.root ? props.root : path
root = root.replace(/\/+$/g, '')
const rootItem = NavHierarchy.getItem(root)
const hierarchy = NavHierarchy.getHierarchy(rootItem, props)
if (!hierarchy) {
throw new Error(`could not find entry for ${root}`)
}
return showHierarchy(hierarchy, props)
}}
</Location>
)
}
export default Index