fix: render of markdown preview

This commit is contained in:
h z
2024-12-06 17:22:42 +00:00
parent ccdded32a8
commit df7ba4c490
9 changed files with 164 additions and 79 deletions

View File

@@ -0,0 +1,42 @@
import React from "react";
import ReactMarkdown from "react-markdown";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import rehypeRaw from "rehype-raw";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
import "katex/dist/katex.min.css";
import "./MarkdownView.css";
const MarkdownPreview = ({ content, height="auto" }) => {
return (
<div className="markdown-preview" style={{height}}>
<ReactMarkdown
children={content}
remarkPlugins={[remarkMath]}
rehypePlugins={[rehypeKatex, rehypeRaw]}
components={{
code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || "");
return !inline && match ? (
<SyntaxHighlighter
style={okaidia}
language={match[1]}
PreTag="div"
{...props}
>
{String(children).replace(/\n$/, "")}
</SyntaxHighlighter>
) : (
<code className={className} {...props}>
{children}
</code>
);
},
}}
/>
</div>
);
};
export default MarkdownPreview;