-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (53 loc) · 1.69 KB
/
index.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
import React from "react"
import Highlight, { defaultProps } from "prism-react-renderer"
import theme from "prism-react-renderer/themes/nightOwl"
import {
container as containerStyles,
fileTitle as fileTitleStyles,
languageTag as languageTagStyles,
preWrapper as preWrapperStyles,
pre as preStyles,
code as codeStyles,
} from "./styles.module.css"
const CodeBlock = (props) => {
const code = props.children.props.children.trim()
const className = props.children.props.className || ""
const language = className.replace(/language-/, "")
const fileTitle = props.title || ""
return (
<div className={containerStyles}>
{fileTitle != "" && (
<div className={fileTitleStyles}>
<span aria-label="file">📄</span> {fileTitle}
</div>
)}
{language && <div className={languageTagStyles}>{language}</div>}
<div className={preWrapperStyles}>
<Highlight
{...defaultProps}
code={code}
language={language}
theme={theme}
>
{({ className, tokens, getLineProps, getTokenProps }) => (
<pre className={`${className} ${preStyles}`}>
{/* for each line in the code */}
{tokens.map((line, i) => (
<code
{...getLineProps({ line, key: i })}
className={codeStyles}
>
{/* for each token in the line */}
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</code>
))}
</pre>
)}
</Highlight>
</div>
</div>
)
}
export default CodeBlock