-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
69 lines (64 loc) · 1.73 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
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
import "katex/dist/katex.min.css"
import React from "react"
import { MDXProvider } from "@mdx-js/react"
import Highlight, { defaultProps } from "prism-react-renderer"
import github from "prism-react-renderer/themes/github"
const loadExternalStyles = url => {
const styles = document.createElement("link")
styles.rel = "stylesheet"
styles.href = url
styles.type = "text/css"
document.head.appendChild(styles)
}
export const onClientEntry = () => {
window.onload = () => {
loadExternalStyles(
// Load cm-webfonts
"https://cdn.jsdelivr.net/gh/aaaakshat/cm-web-fonts@latest/fonts.css"
)
}
}
// Credits: https://github.com/kunalJa/gatsby-starter-math-blog
const component = {
pre: props => {
const className = props.children.props.className || ""
const matches = className.match(/language-(?<lang>.*)/)
return (
<Highlight
{...defaultProps}
code={props.children.props.children}
language={
matches && matches.groups && matches.groups.lang
? matches.groups.lang
: ""
}
theme={github}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className + " overflow-auto"} style={style}>
{tokens.map((line, i) => {
if (i === tokens.length - 1) {
return null
}
return (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => {
return <span {...getTokenProps({ token, key })} />
})}
</div>
)
})}
</pre>
)}
</Highlight>
)
},
}
export const wrapRootElement = ({ element }) => {
return <MDXProvider components={component}>{element}</MDXProvider>
}