Created
November 11, 2021 21:32
-
-
Save johnpolacek/8097feb295a69955c3385a76b8971bc4 to your computer and use it in GitHub Desktop.
React Component for rendering blocks of code with Prism
This file contains hidden or 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 Highlight, { defaultProps } from "prism-react-renderer" | |
import github from "prism-react-renderer/themes/github" | |
const CodeBlock = ({ children, className }) => { | |
const language = className ? className.replace(/language-/, "") : "javascript" | |
return ( | |
<Highlight | |
{...defaultProps} | |
code={children} | |
language={language} | |
theme={github} | |
> | |
{({ className, style, tokens, getLineProps, getTokenProps }) => ( | |
<pre | |
className={className} | |
style={{ | |
textAlign: "left", | |
padding: "16px", | |
marginBottom: "32px", | |
...style, | |
}} | |
> | |
{tokens.map((line, i) => ( | |
<div key={i} {...getLineProps({ line, key: i })}> | |
{line.map((token, key) => ( | |
<span key={key} {...getTokenProps({ token, key })} /> | |
))} | |
</div> | |
))} | |
</pre> | |
)} | |
</Highlight> | |
) | |
} | |
export default CodeBlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment