Repository navigation
Expand file tree
/
Copy pathCode.js
More file actions
44 lines (40 loc) · 1.37 KB
/
Copy pathCode.js
File metadata and controls
44 lines (40 loc) · 1.37 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
import React from 'react'
import PropTypes from 'prop-types'
import { withTheme } from 'styled-components'
import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/prism-light'
import defaultTheme from 'react-syntax-highlighter/styles/prism/atom-dark'
import javascript from 'react-syntax-highlighter/languages/prism/javascript'
import jsx from 'react-syntax-highlighter/languages/prism/jsx'
export default withTheme(class Code extends React.Component {
static propTypes = {
children: PropTypes.string,
className: PropTypes.string,
theme: PropTypes.object
}
constructor(props) {
super(props)
registerLanguage('javascript', javascript)
registerLanguage('jsx', jsx)
if (props.theme && props.theme.prism && props.theme.prism.languages) {
const languages = props.theme.prism.languages
Object.keys(languages).forEach(key => {
registerLanguage(key, languages[key])
})
}
}
getLangauge = (lang) => {
return lang ? lang.replace('language-', '') : 'javascripts'
}
render() {
const { className, children, theme } = this.props
const language = this.getLangauge(className)
const style = (theme.prism && theme.prism.style)
? theme.prism.style
: defaultTheme
return (
<SyntaxHighlighter language={language} style={style}>
{children}
</SyntaxHighlighter>
)
}
})