import { Box, BoxProps, chakra, useColorMode } from '@chakra-ui/react'; import nightOwlLightTheme from 'prism-react-renderer/themes/nightOwlLight'; import nightOwlDarkTheme from 'prism-react-renderer/themes/nightOwl'; import uiTheme from '../lib/theme'; import BaseHighlight, { defaultProps, Language } from 'prism-react-renderer'; import { CSSProperties } from 'react'; import CopyButton from './CopyButton'; const editorStyle: CSSProperties = { fontSize: 14, overflowX: 'auto', fontFamily: 'SF Mono, Menlo, monospace', height: '100%', }; const CodeContainer = (props: BoxProps) => ( ); const RE = /{([\d,-]+)}/; const calculateLinesToHighlight = (meta: string) => { if (!RE.test(meta)) { return () => false; } const lineNumbers = RE.exec(meta)[1] .split(`,`) .map(v => v.split(`-`).map(x => parseInt(x, 10))); return (index: number) => { const lineNumber = index + 1; const inRange = lineNumbers.some(([start, end]) => end ? lineNumber >= start && lineNumber <= end : lineNumber === start, ); return inRange; }; }; interface HighlightProps extends BoxProps { code: string; language: Language; metastring?: string; showLines?: boolean; } function CodeBlock({ code, language, metastring, showLines, ...props }: HighlightProps) { const shouldHighlightLine = calculateLinesToHighlight(metastring); const { colorMode } = useColorMode(); const backgroundColor = colorMode === 'light' ? uiTheme.colors.gray[100] : uiTheme.colors.gray[900]; const codeTheme = colorMode === 'light' ? nightOwlLightTheme : nightOwlDarkTheme; const customizedCodeTheme = { ...codeTheme, plain: { ...codeTheme.plain, backgroundColor, }, }; return ( {/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */} {/* @ts-ignore */} {({ className, style, tokens, getLineProps, getTokenProps }) => (
                {tokens.slice(0, -1).map((line, i) => {
                  const lineProps = getLineProps({ line, key: i });
                  return (
                    
                      {showLines && (
                        
                          {i + 1}
                        
                      )}
                      {line.map((token, key) => (
                        
                      ))}
                    
                  );
                })}
              
)}
); } export default CodeBlock;