This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
routes.js
123 lines (105 loc) · 2.7 KB
/
routes.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import path from 'path'
import React from 'react'
import { Location } from '@reach/router'
import lazyComponent from './lazyComponent'
import { Link } from './Link'
import StyleProvider from './StyleProvider'
import KeyboardShortcuts from './keyboard-shortcuts.md'
const name = 'mdx-go'
const context = __STATIC__
? require.context(__DIRNAME__, true, /\.(js|md|mdx)$/, 'sync')
: require.context(__DIRNAME__, true, /\.(js|md|mdx)$/, 'lazy')
const getSync = require.context(__DIRNAME__, false, /index\.(js|md|mdx)$/, 'sync')
export let Root = props => props.fullscreen ? props.children :
<StyleProvider
children={props.children}
style={{
padding: 32,
marginLeft: 'auto',
marginRight: 'auto',
maxWidth: 768
}}
/>
export let NotFound = (props) =>
<h1>Page not found</h1>
export const Directory = props =>
<React.Fragment>
<h1>{name}</h1>
{props.routes
.filter(route => route.path !== '/_')
.map(route => (
<div key={route.key}>
<Link href={route.path}
style={{
fontWeight: 'bold',
color: 'inherit',
}}>
{route.key}
</Link>
</div>
))}
<KeyboardShortcuts />
</React.Fragment>
export const parseRoute = (key, context) => {
const extname = path.extname(key)
const name = path.basename(key, extname)
if (/^_/.test(name)) return null
const dirname = path.dirname(key).replace(/^\./, '')
const index = name === 'index'
const pathname = dirname + '/' + (index ? '' : name)
let Component = lazyComponent(() => context(key))
let exports = {}
if (index) {
exports = context(key)
Component = lazyComponent(() => exports)
}
if (name === '404') {
NotFound = Component
return
}
return {
key,
extname,
name,
dirname,
path: pathname,
Component,
...exports
}
}
export const [ index ] = getSync.keys()
.map(key => parseRoute(key, getSync))
.filter(Boolean)
const withRouter = Component => React.forwardRef((props, ref) =>
<Location
children={router => (
<Component
ref={ref}
{...props}
{...router}
/>
)}
/>
)
if (typeof index.Root === 'function') {
Root = withRouter(index.Root)
}
export const mainRoutes = context.keys()
.filter(key => key !== index.key)
.map(key => parseRoute(key, context))
.filter(Boolean)
export const subRoutes = (typeof index.files === 'function' && index.files.keys)
? index.files.keys().map(key => parseRoute(key, index.files))
: []
const directory = {
key: '_',
path: '/_',
Component: lazyComponent(() => ({ default: Directory })),
}
export const routes = [
index,
...mainRoutes,
...subRoutes,
directory
]
export default routes