-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcode.tsx
More file actions
63 lines (58 loc) · 1.97 KB
/
code.tsx
File metadata and controls
63 lines (58 loc) · 1.97 KB
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
// Include additional languages here
// --
import Prism from "prism-react-renderer/prism";
(typeof global !== "undefined" ? global : window).Prism = Prism;
require("prismjs/components/prism-rust")
require("prismjs/components/prism-lua")
require("prismjs/components/prism-lisp")
require("../lib/prism-forth")
// --
import Highlight, { defaultProps, Language } from "prism-react-renderer";
import codeTheme from "./codeTheme";
interface CodeOptions {
showLineNumbers?: boolean;
highlightLines?: number[];
}
export default function Code({ children, language, options = { showLineNumbers: false, highlightLines: [] } }: { children: any, language: Language, options?: CodeOptions }) {
return (
<div className="code">
<Highlight
{...defaultProps}
theme={codeTheme}
code={children}
language={language}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<div className={className} style={style}>
{tokens.map((line, i) => {
const lineProps = getLineProps({ line, key: i });
return (
<div
key={i}
{...lineProps}
className={`${lineProps.className} code-line ${options.highlightLines?.includes(i) ? 'highlighted' : ''}`}
>
{options.showLineNumbers && <span style={{ color: "var(--light-text)", paddingRight: 15 }}>{i + 1}</span>}
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token, key })} />
))}
</div>
);
})}
</div>
)}
</Highlight>
<style jsx>{`
.code-line {
transition: background-color 0.2s;
padding-left: 0.75rem;
}
.code-line.highlighted {
background-color: #fef3c7;
border-left: 4px solid #f59e0b;
padding-left: 0.5rem;
}
`}</style>
</div>
);
}