-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.tsx
66 lines (58 loc) · 1.94 KB
/
code.tsx
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
import * as React from 'react'
import { useEffect, useRef } from 'react'
import Highlight, { defaultProps, Language } from 'prism-react-renderer'
import theme from 'prism-react-renderer/themes/vsDark'
import useSource from './use-source'
import { basename } from 'path'
import { Panel } from './panel'
import { StageRect } from './stage'
type Props = {
src: string
language?: Language
frame?: StageRect
title?: string
scrollTo?: number
highlight?: [number, number]
}
export default ({ src, title, frame, language='typescript' as Language, scrollTo, highlight }: Props) => {
const code = useSource(src)
const lineRefs = scrollTo ?
{
[scrollTo]: e => {
e && e.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
} : {}
return <Panel title={title || basename(src)} frame={frame}>
<Code highlight={highlight} lineRefs={lineRefs} language={language}>{code}</Code>
</Panel>
}
type CodeProps = {
src?: string
language?: Language
children: string
lineRefs?: { [num: number]: any }
highlight?: [number, number]
}
const Code = ({ language='typescript' as Language, children,
lineRefs={},
highlight,
}: CodeProps) => {
if (!children) return null
return <Highlight {...defaultProps} theme={theme} code={children} language={language}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={{...style, background: 'none'}}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })} ref={lineRefs[i + 1]}
data-linum={i + 1}
data-highlight={isHighlighted(i + 1, highlight)}>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
)}
</Highlight>
}
const isHighlighted = (linum: number, highlight?: [number, number]) =>
(highlight && (linum >= highlight[0] && linum <= highlight[1])) ? true : void 0