1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-29 13:58:02 +03:00

Merge pull request #250 from rexxars/fix-default-language

Fix default language for syntax highlighting
This commit is contained in:
Brent Jackson 2019-02-18 16:27:02 -05:00 committed by GitHub
commit 246218d56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,44 +1,47 @@
import React from 'react'
import PropTypes from 'prop-types'
import { withTheme } from 'styled-components'
import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/prism-light'
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
}
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])
})
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])
})
}
}
getLanguage = lang => {
return lang ? lang.replace('language-', '') : 'javascript'
}
render() {
const { className, children, theme } = this.props
const language = this.getLanguage(className)
const style =
theme.prism && theme.prism.style ? theme.prism.style : defaultTheme
return (
<SyntaxHighlighter language={language} style={style}>
{children}
</SyntaxHighlighter>
)
}
}
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>
)
}
})
)