Skip to content

Instantly share code, notes, and snippets.

@johnpolacek
Created November 11, 2021 21:32
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 johnpolacek/8097feb295a69955c3385a76b8971bc4 to your computer and use it in GitHub Desktop.
Save johnpolacek/8097feb295a69955c3385a76b8971bc4 to your computer and use it in GitHub Desktop.
React Component for rendering blocks of code with Prism
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