2023-03-19 01:14:19 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
|
|
|
|
import rehypeKatex from 'rehype-katex';
|
|
|
|
|
import rehypeHighlight from 'rehype-highlight';
|
|
|
|
|
import remarkMath from 'remark-math';
|
|
|
|
|
import remarkGfm from 'remark-gfm';
|
2023-05-18 11:09:31 -07:00
|
|
|
import rehypeRaw from 'rehype-raw';
|
2023-03-19 01:14:19 -04:00
|
|
|
import CodeBlock from './CodeBlock';
|
2023-04-01 12:56:32 -07:00
|
|
|
import { langSubset } from '~/utils/languages.mjs';
|
2023-03-19 01:14:19 -04:00
|
|
|
|
2023-03-26 23:02:57 +09:00
|
|
|
const Content = React.memo(({ content }) => {
|
2023-03-24 18:44:12 -04:00
|
|
|
let rehypePlugins = [
|
2023-03-22 17:15:32 -04:00
|
|
|
[rehypeKatex, { output: 'mathml' }],
|
|
|
|
|
[
|
|
|
|
|
rehypeHighlight,
|
|
|
|
|
{
|
|
|
|
|
detect: true,
|
|
|
|
|
ignoreMissing: true,
|
|
|
|
|
subset: langSubset
|
|
|
|
|
}
|
|
|
|
|
],
|
2023-05-18 11:09:31 -07:00
|
|
|
[rehypeRaw]
|
2023-03-24 18:44:12 -04:00
|
|
|
];
|
|
|
|
|
|
2023-03-19 01:14:19 -04:00
|
|
|
return (
|
2023-03-26 23:02:57 +09:00
|
|
|
<ReactMarkdown
|
|
|
|
|
remarkPlugins={[remarkGfm, [remarkMath, { singleDollarTextMath: false }]]}
|
|
|
|
|
rehypePlugins={rehypePlugins}
|
|
|
|
|
linkTarget="_new"
|
|
|
|
|
components={{
|
|
|
|
|
code,
|
2023-05-18 11:09:31 -07:00
|
|
|
p
|
2023-03-26 23:02:57 +09:00
|
|
|
// em,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
</ReactMarkdown>
|
2023-03-19 01:14:19 -04:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const code = React.memo((props) => {
|
|
|
|
|
const { inline, className, children } = props;
|
|
|
|
|
const match = /language-(\w+)/.exec(className || '');
|
|
|
|
|
const lang = match && match[1];
|
|
|
|
|
|
|
|
|
|
if (inline) {
|
|
|
|
|
return <code className={className}>{children}</code>;
|
|
|
|
|
} else {
|
2023-05-18 11:09:31 -07:00
|
|
|
return <CodeBlock lang={lang || 'text'} codeChildren={children} />;
|
2023-03-19 01:14:19 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const p = React.memo((props) => {
|
2023-05-18 11:09:31 -07:00
|
|
|
return <p className="mb-2 whitespace-pre-wrap">{props?.children}</p>;
|
2023-03-19 01:14:19 -04:00
|
|
|
});
|
|
|
|
|
|
2023-03-22 17:15:32 -04:00
|
|
|
// const blinker = ({ node }) => {
|
|
|
|
|
// if (node.type === 'text' && node.value === '█') {
|
|
|
|
|
// return <span className="result-streaming">{node.value}</span>;
|
|
|
|
|
// }
|
2023-03-19 11:25:12 -04:00
|
|
|
|
2023-03-22 17:15:32 -04:00
|
|
|
// return null;
|
|
|
|
|
// };
|
2023-03-19 11:25:12 -04:00
|
|
|
|
2023-03-22 17:15:32 -04:00
|
|
|
// const em = React.memo(({ node, ...props }) => {
|
|
|
|
|
// if (
|
|
|
|
|
// props.children[0] &&
|
|
|
|
|
// typeof props.children[0] === 'string' &&
|
|
|
|
|
// props.children[0].startsWith('^')
|
|
|
|
|
// ) {
|
|
|
|
|
// return <sup>{props.children[0].substring(1)}</sup>;
|
|
|
|
|
// }
|
|
|
|
|
// if (
|
|
|
|
|
// props.children[0] &&
|
|
|
|
|
// typeof props.children[0] === 'string' &&
|
|
|
|
|
// props.children[0].startsWith('~')
|
|
|
|
|
// ) {
|
|
|
|
|
// return <sub>{props.children[0].substring(1)}</sub>;
|
|
|
|
|
// }
|
|
|
|
|
// return <i {...props} />;
|
|
|
|
|
// });
|
2023-03-19 01:14:19 -04:00
|
|
|
|
|
|
|
|
export default Content;
|