forked from tinacms/tina.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
85 lines (76 loc) · 2.3 KB
/
index.tsx
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
import { NextSeo } from 'next-seo'
import React from 'react'
import { getMarkdownPreviewProps } from 'utils/getMarkdownPreviewProps'
import { getDocsNav } from 'utils/docs/getDocProps'
import { createTocListener } from 'utils'
import { DocsLayout, MarkdownContent } from 'components/layout'
import {
DocGridToc,
DocGridContent,
DocsGrid,
DocGridHeader,
DocsPageTitle,
} from '../docs/[...slug]'
import Toc from 'components/toc'
import { openGraphImage } from 'utils/open-graph-image'
const GuideTemplate = ({ markdownFile, navItems, tocItems }) => {
const { frontmatter, markdownBody, excerpt } = markdownFile.data
const [activeIds, setActiveIds] = React.useState([])
const isBrowser = typeof window !== `undefined`
const contentRef = React.useRef<HTMLDivElement>(null)
React.useEffect(() => {
if (!isBrowser || !contentRef.current) {
return
}
const activeTocListener = createTocListener(contentRef, setActiveIds)
window.addEventListener('scroll', activeTocListener)
return () => window.removeEventListener('scroll', activeTocListener)
}, [contentRef])
return (
<>
<NextSeo
title={frontmatter.title}
titleTemplate={'%s | TinaCMS Docs'}
description={excerpt}
openGraph={{
title: frontmatter.title,
description: excerpt,
images: [openGraphImage('TinaCMS Guides')],
}}
/>
<DocsLayout navItems={navItems}>
<DocsGrid>
<DocGridHeader>
<DocsPageTitle>{frontmatter.title}</DocsPageTitle>
</DocGridHeader>
<DocGridToc>
<Toc tocItems={tocItems} activeIds={activeIds} />
</DocGridToc>
<DocGridContent ref={contentRef}>
<hr />
<MarkdownContent escapeHtml={false} content={markdownBody} />
</DocGridContent>
</DocsGrid>
</DocsLayout>
</>
)
}
export default GuideTemplate
export const getStaticProps = async ctx => {
const {
props: { preview, file: markdownFile, tocItems },
} = await getMarkdownPreviewProps(
`content/guides/index.md`,
ctx.preview,
ctx.previewData
)
const navItems = await getDocsNav(preview, ctx.previewData)
return {
props: {
slug: '/guides',
markdownFile,
tocItems,
navItems: navItems.data,
},
}
}