CodeBlock.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import type { ReactNode } from '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. // remove font-family
  6. Object.entries<object>(oneDark).forEach(([key, value]) => {
  7. if ('fontFamily' in value) {
  8. delete oneDark[key].fontFamily;
  9. }
  10. });
  11. function extractChildrenToIgnoreReactNode(children: ReactNode): ReactNode {
  12. if (children == null) {
  13. return children;
  14. }
  15. // Single element array
  16. if (Array.isArray(children) && children.length === 1) {
  17. return extractChildrenToIgnoreReactNode(children[0]);
  18. }
  19. // Multiple element array
  20. if (Array.isArray(children) && children.length > 1) {
  21. return children.map(node => extractChildrenToIgnoreReactNode(node)).join('');
  22. }
  23. // object
  24. if (typeof children === 'object') {
  25. const grandChildren = (children as any).children ?? (children as any).props.children;
  26. return extractChildrenToIgnoreReactNode(grandChildren);
  27. }
  28. return String(children).replace(/\n$/, '');
  29. }
  30. function CodeBlockSubstance({ lang, children }: { lang: string, children: ReactNode }): JSX.Element {
  31. // return alternative element
  32. // in order to fix "CodeBlock string is be [object Object] if searched"
  33. // see: https://github.com/weseek/growi/pull/7484
  34. //
  35. // Note: You can also remove this code if the user requests to see the code highlighted in Prism as-is.
  36. const isSimpleString = Array.isArray(children) && children.length === 1 && typeof children[0] === 'string';
  37. if (!isSimpleString) {
  38. return (
  39. <div style={oneDark['pre[class*="language-"]']}>
  40. <code className={`language-${lang}`} style={oneDark['code[class*="language-"]']}>
  41. {children}
  42. </code>
  43. </div>
  44. );
  45. }
  46. return (
  47. <PrismAsyncLight
  48. PreTag="div"
  49. style={oneDark}
  50. language={lang}
  51. >
  52. {extractChildrenToIgnoreReactNode(children)}
  53. </PrismAsyncLight>
  54. );
  55. }
  56. export const CodeBlock = ({ className, children }: {className: string, children: JSX.Element}): JSX.Element => {
  57. // TODO: set border according to the value of 'customize:highlightJsStyleBorder'
  58. if (className === 'inline') {
  59. return <code className={`code-inline ${className ?? ''}`}>{children}</code>;
  60. }
  61. const match = /language-(\w+)(:?.+)?/.exec(className || '');
  62. const lang = match && match[1] ? match[1] : '';
  63. const name = match && match[2] ? match[2].slice(1) : null;
  64. return (
  65. <>
  66. {name != null && (
  67. <cite className={`code-highlighted-title ${styles['code-highlighted-title']}`}>{name}</cite>
  68. )}
  69. <CodeBlockSubstance lang={lang}>{children}</CodeBlockSubstance>
  70. </>
  71. );
  72. };