Created
November 13, 2016 04:56
-
-
Save vinhlh/6ff862e41115442a1e0c9d6c4a5bff4b to your computer and use it in GitHub Desktop.
Minimal React Highlight.js component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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