-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-react.tsx
36 lines (33 loc) · 1.4 KB
/
build-react.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
import { renderToStaticMarkup } from 'react-dom/server';
import React from 'react'
import * as jsxRuntime from 'react/jsx-runtime'
import { Layout, SinglePageContent } from './components/index.js';
import { wrapHtml } from './html.js';
import { extractFrontMatter } from './utils/index.js';
import { evaluate } from '@mdx-js/mdx';
import ReactMarkdown from 'react-markdown'
import { MDXProvider } from '@mdx-js/react';
import * as components from './components/index.js'
import rehypeRaw from 'rehype-raw'
export async function renderMDPage(fileContents: string) {
const { contents, frontmatter } = extractFrontMatter(fileContents)
const component =
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{contents}</ReactMarkdown>
return renderReact(component, frontmatter)
};
export async function renderMDXPage(fileContents: string) {
const { contents, frontmatter } = extractFrontMatter(fileContents)
const { default: MDXContent } = await evaluate(contents, { ...(jsxRuntime as any) })
return renderReact(<MDXProvider><MDXContent components={components} /></MDXProvider >, frontmatter)
};
function renderReact(component: React.ReactNode, frontmatter: Record<string, any>) {
const title = frontmatter.title
const html = renderToStaticMarkup(
<Layout>
<SinglePageContent frontmatter={frontmatter}>
{component}
</SinglePageContent>
</Layout >
)
return wrapHtml({ html, title, frontmatter })
}