-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap-root-element.js
41 lines (37 loc) · 914 Bytes
/
wrap-root-element.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
import React from "react"
import { MDXProvider } from "@mdx-js/react"
import { Code } from "./src/components/code"
const preToCodeBlock = preProps => {
if (
preProps.children &&
preProps.children.props &&
preProps.children.props.mdxType === "code"
) {
const {
children: codeString,
className = "",
...props
} = preProps.children.props
const match = className.match(/language-([\0-\uFFFF]*)/)
return {
codeString: codeString.trim(),
className,
language: match != null ? match[1] : "",
...props,
}
}
return undefined
}
const components = {
pre: preProps => {
const props = preToCodeBlock(preProps)
if (props) {
return <Code {...props} />
} else {
return <pre {...preProps} />
}
},
}
export const wrapRootElement = ({ element }) => (
<MDXProvider components={components}>{element}</MDXProvider>
)