-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
LayoutDocs.js
197 lines (182 loc) 路 5.44 KB
/
LayoutDocs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as React from 'react'
import { MDXProvider } from '@mdx-js/react'
import { Nav } from 'components/Nav'
import { Sidebar } from 'components/Sidebar'
import { SidebarCategory } from 'components/SidebarCategory'
import { SidebarHeading } from 'components/SidebarHeading'
import { SidebarMobile } from 'components/SidebarMobile'
import { SidebarPost } from 'components/SidebarPost'
import { Sticky } from 'components/Sticky'
import { useIsMobile } from 'components/useIsMobile'
import { findRouteByPath } from 'lib/docs/findRouteByPath'
import { removeFromLast } from 'lib/docs/utils'
import { getRouteContext } from 'lib/get-route-context'
import { useRouter } from 'next/router'
import { Toc } from './Toc'
import s from './markdown.module.css'
import { Footer } from './Footer'
import { DocsPageFooter } from './DocsPageFooter'
import { Seo } from './Seo'
import MDXComponents from './MDXComponents'
import Head from 'next/head'
import { getManifest } from 'manifests/getManifest'
const getSlugAndTag = path => {
const parts = path.split('/')
if (parts[2] === '1.5.8' || parts[2] === '2.1.4') {
return {
tag: parts[2],
slug: `/docs/${parts.slice(2).join('/')}`,
}
}
return {
slug: path,
}
}
const addTagToSlug = (slug, tag) => {
return tag ? `/docs/${tag}/${slug.replace('/docs/', '')}` : slug
}
export const LayoutDocs = props => {
const router = useRouter()
const { slug, tag } = getSlugAndTag(router.asPath)
const { routes } = getManifest(tag)
const _route = findRouteByPath(removeFromLast(slug, '#'), routes) // @ts-ignore
const isMobile = useIsMobile()
const { route, prevRoute, nextRoute } = getRouteContext(_route, routes)
const title = route && `${route.title}`
return (
<>
{tag && (
<Head>
<meta name="robots" content="noindex" />
</Head>
)}
<div>
{isMobile ? (
<>
<Nav />
<Sticky shadow>
<SidebarMobile>
<SidebarRoutes isMobile={true} routes={routes} />
</SidebarMobile>
</Sticky>
</>
) : (
<Sticky>
<Nav />
</Sticky>
)}
<Seo
title={props.meta.title || title}
description={props.meta.description}
/>
<div className="block">
<>
<div className="container mx-auto pb-12 pt-6 content">
<div className="flex relative">
{!isMobile && (
<Sidebar fixed>
<SidebarRoutes routes={routes} />
</Sidebar>
)}
<div className={s['markdown'] + ' w-full docs'}>
<h1 id="_top">{props.meta.title}</h1>
<MDXProvider components={MDXComponents}>
{props.children}
</MDXProvider>
<DocsPageFooter
href={route?.path || ''}
route={route}
prevRoute={prevRoute}
nextRoute={nextRoute}
/>
</div>
{props.meta.toc === false ? null : (
<div
className="hidden xl:block ml-10 flex-shrink-0"
style={{
width: 200,
}}
>
<div className="sticky top-24">
<h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
On this page
</h4>
<Toc title={props.meta.title} />
</div>
</div>
)}
</div>
</div>
</>
</div>
</div>
<Footer />
<style jsx>{`
.docs {
min-width: calc(100% - 300px - 1rem - 200px);
}
`}</style>
</>
)
}
function getCategoryPath(routes) {
const route = routes.find(r => r.path)
return route && removeFromLast(route.path, '/')
}
function SidebarRoutes({ isMobile, routes: currentRoutes, level = 1 }) {
const { asPath } = useRouter()
let { slug, tag } = getSlugAndTag(asPath)
return currentRoutes.map(({ path, title, routes, heading, open }) => {
if (routes) {
const pathname = getCategoryPath(routes)
const selected = slug.startsWith(pathname)
const opened = selected || isMobile ? false : open
if (heading) {
return (
<SidebarHeading key={'parent' + pathname} title={title}>
<SidebarRoutes
isMobile={isMobile}
routes={routes}
level={level + 1}
/>
</SidebarHeading>
)
}
return (
<SidebarCategory
key={pathname}
isMobile={isMobile}
level={level}
title={title}
selected={selected}
opened={opened}
>
<SidebarRoutes
isMobile={isMobile}
routes={routes}
level={level + 1}
/>
</SidebarCategory>
)
}
const pagePath = removeFromLast(path, '.')
const pathname = addTagToSlug(pagePath, tag)
const selected = slug === pagePath
const route = {
href: pagePath,
path,
title,
pathname,
selected,
}
return (
<SidebarPost
key={title}
isMobile={isMobile}
level={level}
route={route}
/>
)
})
}
LayoutDocs.displayName = 'LayoutDocs'