|
| 1 | +import Highlight, { defaultProps, Language } from 'prism-react-renderer'; |
| 2 | +import dracula from 'prism-react-renderer/themes/vsDark'; |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +const CodeHighlight = ({ |
| 6 | + plainText, |
| 7 | + className, |
| 8 | + lang, |
| 9 | + highlight, |
| 10 | +}: { |
| 11 | + plainText: string; |
| 12 | + className?: string; |
| 13 | + lang?: string; |
| 14 | + highlight?: string[]; |
| 15 | +}) => { |
| 16 | + const language = |
| 17 | + lang || className?.replace(/language-/, '') || ('bash' as any); |
| 18 | + |
| 19 | + const children = plainText; |
| 20 | + |
| 21 | + const getTokenSetup = ({ getTokenProps, token, key }: any) => { |
| 22 | + const tokenProps = getTokenProps({ token, key }); |
| 23 | + // if (highlight && highlight.includes(token.content)) { |
| 24 | + // return <WordHighlight>{token.content}</WordHighlight>; |
| 25 | + // } |
| 26 | + return <span key={key} {...tokenProps} />; |
| 27 | + }; |
| 28 | + |
| 29 | + const Button = (props: any) => ( |
| 30 | + <button |
| 31 | + style={{ |
| 32 | + position: 'absolute', |
| 33 | + top: 0, |
| 34 | + right: 0, |
| 35 | + border: 'none', |
| 36 | + boxShadow: 'none', |
| 37 | + textDecoration: 'none', |
| 38 | + margin: '8px', |
| 39 | + padding: '8px 12px', |
| 40 | + background: '#E2E8F022', |
| 41 | + color: 'white', |
| 42 | + borderRadius: '8px', |
| 43 | + cursor: 'pointer', |
| 44 | + fontSize: '14px', |
| 45 | + fontFamily: 'sans-serif', |
| 46 | + lineHeight: '1', |
| 47 | + }} |
| 48 | + {...props} |
| 49 | + /> |
| 50 | + ); |
| 51 | + |
| 52 | + const [isCopied, setIsCopied] = useState(false); |
| 53 | + const copyToClipboard = (str: string) => { |
| 54 | + const el = document.createElement('textarea'); |
| 55 | + el.value = str; |
| 56 | + el.setAttribute('readonly', ''); |
| 57 | + el.style.position = 'absolute'; |
| 58 | + el.style.left = '-9999px'; |
| 59 | + document.body.appendChild(el); |
| 60 | + el.select(); |
| 61 | + document.execCommand('copy'); |
| 62 | + document.body.removeChild(el); |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <Highlight |
| 67 | + {...defaultProps} |
| 68 | + code={children} |
| 69 | + language={language} |
| 70 | + theme={dracula} |
| 71 | + > |
| 72 | + {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
| 73 | + <pre |
| 74 | + className={className} |
| 75 | + style={{ ...style, padding: '2rem', position: 'relative' }} |
| 76 | + > |
| 77 | + <Button |
| 78 | + onClick={() => { |
| 79 | + copyToClipboard(children); |
| 80 | + setIsCopied(true); |
| 81 | + setTimeout(() => setIsCopied(false), 3000); |
| 82 | + }} |
| 83 | + > |
| 84 | + {isCopied ? '🎉 Copied!' : 'Copy'} |
| 85 | + </Button> |
| 86 | + {tokens.map((line, i) => ( |
| 87 | + <div key={i} {...getLineProps({ line, key: i })}> |
| 88 | + {line.map((token, key) => |
| 89 | + getTokenSetup({ getTokenProps, token, key }) |
| 90 | + )} |
| 91 | + </div> |
| 92 | + ))} |
| 93 | + </pre> |
| 94 | + )} |
| 95 | + </Highlight> |
| 96 | + ); |
| 97 | +}; |
| 98 | +export default CodeHighlight; |
0 commit comments