Skip to content

Instantly share code, notes, and snippets.

@vinhlh
Created November 13, 2016 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhlh/6ff862e41115442a1e0c9d6c4a5bff4b to your computer and use it in GitHub Desktop.
Save vinhlh/6ff862e41115442a1e0c9d6c4a5bff4b to your computer and use it in GitHub Desktop.
Minimal React Highlight.js component
import React, { Component, PropTypes } from 'react'
import hljs from 'highlight.js/lib/highlight'
import javascript from 'highlight.js/lib/languages/javascript'
import php from 'highlight.js/lib/languages/php'
import python from 'highlight.js/lib/languages/python'
import sql from 'highlight.js/lib/languages/sql'
import objectivec from 'highlight.js/lib/languages/objectivec'
import 'highlight.js/styles/tomorrow.css'
const propTypes = {
children: PropTypes.any
}
class CodeBlock extends Component {
componentDidMount() {
hljs.registerLanguage('javascript', javascript)
hljs.registerLanguage('php', php)
hljs.registerLanguage('python', python)
hljs.registerLanguage('sql', sql)
hljs.registerLanguage('objectivec', objectivec)
hljs.highlightBlock(this.element)
}
render() {
return (
<pre ref={ref => { this.element = ref }}>
<code>
{this.props.children}
</code>
</pre>
)
}
}
CodeBlock.propTypes = propTypes
export default CodeBlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment