CodeBlock.tsx 2.7 KB

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