-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
67 lines (59 loc) · 2.01 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
import React, { useEffect, useState } from 'react';
import { MDXProvider } from '@mdx-js/react';
import moment from 'moment';
import vsDark from 'prism-react-renderer/themes/vsDark';
import Highlight, { defaultProps } from "prism-react-renderer";
export const izOffHrs = () => (
moment().day() >= 6 || moment().hour() >= 17
);
require("github-markdown-css");
export const ThemeContext = React.createContext({ izOffHrs: izOffHrs() });
const hasWindow = typeof window === 'object';
const SyntaxHighlighter = (props) => {
const [isClient, setClient] = useState(hasWindow);
const className = props.children.props.className || "";
const matches = className.match(/language-(?<lang>.*)/);
useEffect(() => {
if (!isClient) {
setClient(true);
}
}, []);
return (
<Highlight
{...defaultProps}
theme={vsDark}
code={props.children.props.children.trim()}
language={
matches && matches.groups && matches.groups.lang
? matches.groups.lang
: ""
}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
<div className="line-number">{i + 1}</div>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
)}
</Highlight>
);
}
const components = {
pre: (props) => <SyntaxHighlighter {...props} />
};
export const wrapRootElement = ({
element
}) => {
return (
<ThemeContext.Provider value={{ izOffHrs: izOffHrs() }}>
<MDXProvider components={components}>
{element}
</MDXProvider>
</ThemeContext.Provider>
);
};