All files / src/components/Markdown Code.tsx

0% Statements 0/8
0% Branches 0/5
0% Functions 0/2
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
import { Code as InlineCode } from "@chakra-ui/react";
import { Language } from "prism-react-renderer";
import { Children, FunctionComponent, ReactNode } from "react";
import { Code as BlockCode } from "../Code";
 
interface CodeProps {
  inline?: boolean;
  children: ReactNode;
  language?: Language;
}
 
export const Code: FunctionComponent<CodeProps> = ({
  inline,
  children,
  language = "typescript",
}) => {
  if (inline) {
    return (
      <InlineCode border="base" marginTop="md" mx={2} px={2} py={0}>
        {children}
      </InlineCode>
    );
  }
 
  const code = Children.toArray(children)
    .reduce((accum: string, child): string => {
      if (typeof child === "string") {
        return `${accum}${child}`;
      }
      return accum;
    }, "")
    .trim();
 
  return <BlockCode code={code} fontSize="0.9rem" language={language} />;
};