This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-browser.js
87 lines (82 loc) · 2.36 KB
/
gatsby-browser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React from "react"
import { MDXProvider } from "@mdx-js/react"
import { useMDXScope } from "gatsby-plugin-mdx/context"
import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live"
import prismTheme from "prism-react-renderer/themes/nightOwl"
import Highlight, { defaultProps } from "prism-react-renderer"
import { copyToClipboard } from "./src/utils/copy-to-clipboard"
import GlobalContextProvider from "./src/context/GlobalContextProvider"
import WrapWithProvider from "./src/context/configure_store"
const LiveCode = props => {
const components = useMDXScope()
return (
<LiveProvider
code={props.children.props.children.trim()}
scope={components}
>
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
)
}
const SyntaxHighlighter = props => {
const handleClick = () => {
copyToClipboard(props.children.props.children.trim())
}
const className = props.children.props.className || ""
const matches = className.match(/language-(?<lang>.*)/)
return (
<Highlight
{...defaultProps}
code={props.children.props.children.trim()}
language={
matches && matches.groups && matches.groups.lang
? matches.groups.lang
: ""
}
theme={prismTheme}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
<button
onClick={handleClick}
style={{
position: "relative",
border: "0px",
borderRadius: "3px",
float: "right"
}}
>
Copy
</button>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
)}
</Highlight>
)
}
const components = {
pre: props => {
if (props.children.props["react-live"]) {
return <LiveCode {...props} />
} else {
return <SyntaxHighlighter {...props} />
}
}
}
export const wrapRootElement = ({ element }) => {
return (
<WrapWithProvider>
<GlobalContextProvider>
<MDXProvider components={components}>{element}</MDXProvider>
</GlobalContextProvider>
</WrapWithProvider>
)
}