forked from iterative/dvc.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.js
65 lines (52 loc) · 1.27 KB
/
doc.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
import React from 'react'
import PropTypes from 'prop-types'
import fetch from 'isomorphic-fetch'
import Error from 'next/error'
import Head from 'next/head'
import Documentation from '../src/components/Documentation'
import { getItemByPath } from '../src/utils/sidebar'
import { makeAbsoluteURL } from '../src/utils/api'
import { META_BASE_TITLE } from '../src/consts'
export default function DocumentationPage({ item, markdown, errorCode }) {
if (errorCode) {
return <Error statusCode={errorCode} />
}
return (
<>
<Head>
<title>
{item.label} | {META_BASE_TITLE}
</title>
</Head>
<Documentation markdown={markdown} {...item} />
</>
)
}
DocumentationPage.propTypes = {
item: PropTypes.object,
markdown: PropTypes.string,
errorCode: PropTypes.number
}
DocumentationPage.getInitialProps = async ({ asPath, req }) => {
const item = getItemByPath(asPath.split(/[?#]/)[0])
if (!item) {
return {
errorCode: 404
}
}
try {
const res = await fetch(makeAbsoluteURL(req, item.source))
if (res.status !== 200) {
return {
errorCode: res.status
}
}
const markdown = await res.text()
return {
item,
markdown
}
} catch {
window.location.reload()
}
}