CodeBlock.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import type { CodeComponent } from 'react-markdown/lib/ast-to-react';
  2. import { PrismAsyncLight } from 'react-syntax-highlighter';
  3. import { oneDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
  4. import styles from './CodeBlock.module.scss';
  5. export const CodeBlock: CodeComponent = ({ inline, className, children }) => {
  6. if (inline) {
  7. return <code className={`code-inline ${className ?? ''}`}>{children}</code>;
  8. }
  9. // TODO: set border according to the value of 'customize:highlightJsStyleBorder'
  10. const match = /language-(\w+)(:?.+)?/.exec(className || '');
  11. const lang = match && match[1] ? match[1] : '';
  12. const name = match && match[2] ? match[2].slice(1) : null;
  13. return (
  14. <>
  15. {name != null && (
  16. <cite className={`code-highlighted-title ${styles['code-highlighted-title']}`}>{name}</cite>
  17. )}
  18. <PrismAsyncLight
  19. className="code-highlighted"
  20. PreTag="div"
  21. style={oneDark}
  22. language={lang}
  23. >
  24. {String(children).replace(/\n$/, '')}
  25. </PrismAsyncLight>
  26. </>
  27. );
  28. };