|
1 | 1 | import React from 'react';
|
2 | 2 | import PropTypes from 'prop-types';
|
| 3 | +import Highlight, { defaultProps } from 'prism-react-renderer'; |
| 4 | +import github from 'prism-react-renderer/themes/github'; |
3 | 5 |
|
4 |
| -const CodeSnippet = ({ children, copy }) => ( |
5 |
| - <div className="CodeSnippet"> |
6 |
| - <div className="CodeSnippet-code">{children}</div> |
7 |
| - {copy !== 'false' && <div className="CodeSnippet-copy">COPY ME</div>} |
8 |
| - </div> |
9 |
| -); |
| 6 | +const CodeSnippet = ({ children, copy, className }) => { |
| 7 | + const language = className.replace('language-', ''); |
| 8 | + return ( |
| 9 | + <div className="CodeSnippet"> |
| 10 | + <div className="CodeSnippet-code"> |
| 11 | + <Highlight |
| 12 | + {...defaultProps} |
| 13 | + theme={github} |
| 14 | + code={children} |
| 15 | + language={language} |
| 16 | + > |
| 17 | + {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
| 18 | + <pre className={className} style={{ ...style, padding: '20px' }}> |
| 19 | + {tokens.map((line, i) => ( |
| 20 | + <div key={i} {...getLineProps({ line, key: i })}> |
| 21 | + {line.map((token, key) => ( |
| 22 | + <span key={key} {...getTokenProps({ token, key })} /> |
| 23 | + ))} |
| 24 | + </div> |
| 25 | + ))} |
| 26 | + </pre> |
| 27 | + )} |
| 28 | + </Highlight> |
| 29 | + </div> |
| 30 | + {copy !== 'false' && <div className="CodeSnippet-copy">COPY ME</div>} |
| 31 | + </div> |
| 32 | + ); |
| 33 | +}; |
10 | 34 |
|
11 | 35 | CodeSnippet.propTypes = {
|
12 | 36 | children: PropTypes.node.isRequired,
|
13 | 37 | copy: PropTypes.string, // TODO: limit options
|
| 38 | + className: PropTypes.string, |
14 | 39 | };
|
15 | 40 |
|
16 | 41 | CodeSnippet.defaultProps = {
|
17 | 42 | copy: 'true',
|
| 43 | + className: 'language-javascript', |
18 | 44 | };
|
19 | 45 |
|
20 | 46 | export default CodeSnippet;
|
0 commit comments