File size: 1,201 Bytes
6b3405c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
import { TypographyStylesProvider } from "@mantine/core";
import Markdown from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import syntaxHighlighterStyle from "react-syntax-highlighter/dist/esm/styles/prism/one-dark";

const FormattedMarkdown = ({ children }: { children: string }) => {
  return (
    <TypographyStylesProvider p="md">
      <Markdown
        components={{
          code(props) {
            const { children, className, node, ref, ...rest } = props;
            void node;
            const languageMatch = /language-(\w+)/.exec(className || "");
            return languageMatch ? (
              <SyntaxHighlighter
                {...rest}
                ref={ref as never}
                children={children?.toString().replace(/\n$/, "") ?? ""}
                language={languageMatch[1]}
                style={syntaxHighlighterStyle}
              />
            ) : (
              <code {...rest} className={className}>
                {children}
              </code>
            );
          },
        }}
      >
        {children}
      </Markdown>
    </TypographyStylesProvider>
  );
};

export default FormattedMarkdown;