import { FC } from 'react' import CopyToClipboard from 'react-copy-to-clipboard' import { Light as SyntaxHighlighter } from 'react-syntax-highlighter' import monokaiCustomTheme from './CodeBlock.utils' import { Button, IconCheck, IconCopy } from 'ui' import js from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript' import ts from 'react-syntax-highlighter/dist/cjs/languages/hljs/typescript' import py from 'react-syntax-highlighter/dist/cjs/languages/hljs/python' import sql from 'react-syntax-highlighter/dist/cjs/languages/hljs/sql' import bash from 'react-syntax-highlighter/dist/cjs/languages/hljs/bash' import dart from 'react-syntax-highlighter/dist/cjs/languages/hljs/dart' import { useState } from 'react' import { useTheme } from '../Providers' interface Props { title?: string language: 'js' | 'jsx' | 'sql' | 'py' | 'bash' | 'ts' | 'dart' hideCopy?: boolean hideLineNumbers?: boolean className?: string value?: string children?: string } const CodeBlock: FC = ({ title, language, className, value, children, hideCopy = false, hideLineNumbers = false, }) => { console.log('CodeBlock', { title }) const { isDarkMode } = useTheme() const monokaiTheme = monokaiCustomTheme(isDarkMode) const [copied, setCopied] = useState(false) const handleCopy = () => { setCopied(true) setTimeout(() => { setCopied(false) }, 1000) } let xxxlang = language ? language : className ? className.replace('language-', '') : 'js' // force jsx to be js highlighted if (xxxlang === 'jsx') xxxlang = 'js' SyntaxHighlighter.registerLanguage('js', js) SyntaxHighlighter.registerLanguage('ts', ts) SyntaxHighlighter.registerLanguage('py', py) SyntaxHighlighter.registerLanguage('sql', sql) SyntaxHighlighter.registerLanguage('bash', bash) SyntaxHighlighter.registerLanguage('dart', dart) // const large = props.size === 'large' ? true : false const large = false // don't show line numbers if bash == lang const showLineNumbers = hideLineNumbers || xxxlang !== 'bash' return (
{title && (
{title.replace(/%20/g, ' ')}
)} {className ? ( {(value || children)?.trimEnd()} ) : ( {value || children} )} {!hideCopy && (value || children) && className ? (
) : null}
) } export default CodeBlock