Skip to content

Instantly share code, notes, and snippets.

@craftzdog
Last active January 24, 2018 08:12
Show Gist options
  • Save craftzdog/1bce29fbfedfec7b960dd83c6a03859e to your computer and use it in GitHub Desktop.
Save craftzdog/1bce29fbfedfec7b960dd83c6a03859e to your computer and use it in GitHub Desktop.
function Highlight (props) {
const { value, language } = props
const elements = []
let index = 0
const pushElement = (token, style) => {
elements.push(<span className={style || ''} key={++index}>{token}</span>)
}
const mode = CodeMirror.findModeByName(language)
CodeMirror.runMode(value, mode ? mode.mime : language, (token, style) => {
pushElement(token, style)
})
pushElement(tokenBuf, lastStyle)
const code = <code>{elements}</code>
return inline ? code : <pre>{code}</pre>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment