File size: 1,166 Bytes
b1bbd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
import React from "react";
import ReactMarkdown from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { coy } from "react-syntax-highlighter/dist/esm/styles/prism";
import remarkGfm from "remark-gfm";
import "./App.css";

const ResponseDisplay = ({ response }) => {
  const renderContent = (content) => {
    const codeRegex = /```(.*?)```/gs;
    const parts = content.split(codeRegex);

    return parts.map((part, index) => {
      if (index % 2 === 1) {
        const language = part.split("\n")[0];
        const code = part.replace(`${language}\n`, "");
        return (
          <SyntaxHighlighter key={index} language={language} style={coy}>

            {code}

          </SyntaxHighlighter>
        );
      } else {
        return (
          <p key={index}>

            <ReactMarkdown

              children={response}

              remarkPlugins={[remarkGfm]}

              components={part}

            />

          </p>
        );
      }
    });
  };

  return <div className="response-display">{renderContent(response)}</div>;
};

export default ResponseDisplay;