Latex plugin (#165)

This commit is contained in:
Bruce Shi 2023-03-25 21:37:00 -07:00 committed by GitHub
parent 14fe29c03a
commit 499007da94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5245 additions and 78 deletions

View File

@ -3,9 +3,10 @@ import { IconEdit } from "@tabler/icons-react";
import { useTranslation } from "next-i18next";
import { FC, useEffect, useRef, useState } from "react";
import ReactMarkdown from "react-markdown";
import rehypeMathjax from "rehype-mathjax";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import { CodeBlock } from "../Markdown/CodeBlock";
import { CopyButton } from "./CopyButton";
interface Props {
message: Message;
@ -132,51 +133,43 @@ export const ChatMessage: FC<Props> = ({ message, messageIndex, lightMode, onEdi
)}
</div>
) : (
<>
<ReactMarkdown
className="prose dark:prose-invert"
remarkPlugins={[remarkGfm]}
components={{
code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || "");
return !inline && match ? (
<CodeBlock
key={Math.random()}
language={match[1]}
value={String(children).replace(/\n$/, "")}
lightMode={lightMode}
{...props}
/>
) : (
<code
className={className}
{...props}
>
{children}
</code>
);
},
table({ children }) {
return <table className="border-collapse border border-black dark:border-white py-1 px-3">{children}</table>;
},
th({ children }) {
return <th className="border border-black dark:border-white break-words py-1 px-3 bg-gray-500 text-white">{children}</th>;
},
td({ children }) {
return <td className="border border-black dark:border-white break-words py-1 px-3">{children}</td>;
}
}}
>
{message.content}
</ReactMarkdown>
{(isHovering || window.innerWidth < 640) && (
<CopyButton
messagedCopied={messagedCopied}
copyOnClick={copyOnClick}
/>
)}
</>
<ReactMarkdown
className="prose dark:prose-invert"
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeMathjax]}
components={{
code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || "");
return !inline && match ? (
<CodeBlock
key={Math.random()}
language={match[1]}
value={String(children).replace(/\n$/, "")}
lightMode={lightMode}
{...props}
/>
) : (
<code
className={className}
{...props}
>
{children}
</code>
);
},
table({ children }) {
return <table className="border-collapse border border-black dark:border-white py-1 px-3">{children}</table>;
},
th({ children }) {
return <th className="border border-black dark:border-white break-words py-1 px-3 bg-gray-500 text-white">{children}</th>;
},
td({ children }) {
return <td className="border border-black dark:border-white break-words py-1 px-3">{children}</td>;
}
}}
>
{message.content}
</ReactMarkdown>
)}
</div>
</div>

1335
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,19 +21,21 @@
"react-i18next": "^12.2.0",
"react-markdown": "^8.0.5",
"react-syntax-highlighter": "^15.5.0",
"remark-gfm": "^3.0.1"
"rehype-mathjax": "^4.0.2",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
"@types/react-syntax-highlighter": "^15.5.6",
"autoprefixer": "^10.4.14",
"postcss": "^8.4.21",
"tailwindcss": "^3.2.7",
"@types/node": "18.15.0",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
"@types/react-syntax-highlighter": "^15.5.6",
"autoprefixer": "^10.4.14",
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",
"postcss": "^8.4.21",
"tailwindcss": "^3.2.7",
"typescript": "4.9.5"
}
}

3891
yarn.lock Normal file

File diff suppressed because it is too large Load Diff