-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.tsx
55 lines (49 loc) · 1.58 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
import React, { ReactElement, ReactNode } from 'react'
import { ThemeProvider } from 'next-themes'
import type { LayoutProps } from './types'
import { BlogProvider } from './blog-context'
import { ArticleLayout } from './article-layout'
import { PostsLayout } from './posts-layout'
import { PageLayout } from './page-layout'
import { DEFAULT_THEME } from './constants'
import { useRouter } from 'next/router'
const layoutMap = {
post: ArticleLayout,
page: PageLayout,
posts: PostsLayout,
tag: PostsLayout
}
const BlogLayout = ({
config,
children,
opts
}: LayoutProps & { children: ReactNode }): ReactElement => {
const type = opts.frontMatter.type || 'post'
const Layout = layoutMap[type]
if (!Layout) {
throw new Error(
`nextra-theme-blog does not support the layout type "${type}" It only supports "post", "page", "posts" and "tag"`
)
}
return (
<BlogProvider opts={opts} config={config}>
<Layout>{children}</Layout>
</BlogProvider>
)
}
export default function Layout(props: any) {
const { route } = useRouter()
const context = globalThis.__nextra_pageContext__[route]
if (!context) throw new Error(`No content found for ${route}.`)
const extendedConfig = { ...DEFAULT_THEME, ...context.themeConfig }
return (
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<BlogLayout config={extendedConfig} opts={context.pageOpts}>
<context.Content {...props} />
</BlogLayout>
</ThemeProvider>
)
}
export { useTheme } from 'next-themes'
export { useBlogContext } from './blog-context'
export * from './types'