2022-03-08 19:39:24 -05:00
|
|
|
import ReactMarkdown from "react-markdown"
|
|
|
|
import remarkGfm from "remark-gfm"
|
2022-03-09 17:57:44 -05:00
|
|
|
import { PrismAsyncLight as SyntaxHighlighter } from 'react-syntax-highlighter';
|
2022-03-08 19:39:24 -05:00
|
|
|
|
|
|
|
// @ts-ignore because of no types in remark-a11y-emoji
|
|
|
|
import a11yEmoji from '@fec/remark-a11y-emoji';
|
|
|
|
import styles from './preview.module.css'
|
2022-03-09 17:57:44 -05:00
|
|
|
import { duotoneDark, duotoneLight } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
|
|
|
import useSharedState from "../../lib/hooks/use-shared-state";
|
2022-03-08 19:39:24 -05:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
content: string | undefined
|
|
|
|
height: number | string
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReactMarkdownPreview = ({ content, height }: Props) => {
|
2022-03-09 17:57:44 -05:00
|
|
|
const [themeType] = useSharedState<string>('theme')
|
|
|
|
return (<div style={{ height }}>
|
|
|
|
<ReactMarkdown className={styles.markdownPreview} remarkPlugins={[remarkGfm, a11yEmoji]}
|
|
|
|
components={{
|
|
|
|
code({ node, inline, className, children, ...props }) {
|
|
|
|
const match = /language-(\w+)/.exec(className || '')
|
|
|
|
return !inline && match ? (
|
|
|
|
<SyntaxHighlighter
|
|
|
|
lineNumberStyle={{
|
|
|
|
minWidth: "1.25rem"
|
|
|
|
}}
|
|
|
|
customStyle={{
|
|
|
|
padding: 0,
|
|
|
|
margin: 0,
|
|
|
|
background: 'transparent'
|
|
|
|
}}
|
|
|
|
codeTagProps={{
|
|
|
|
style: { background: 'transparent' }
|
|
|
|
}}
|
|
|
|
style={themeType === 'dark' ? duotoneDark : duotoneLight}
|
|
|
|
showLineNumbers={true}
|
|
|
|
language={match[1]}
|
|
|
|
PreTag="div"
|
|
|
|
{...props}
|
|
|
|
>{String(children).replace(/\n$/, '')}</SyntaxHighlighter>
|
|
|
|
) : (
|
|
|
|
<code className={className} {...props}>
|
|
|
|
{children}
|
|
|
|
</code>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
{content || ""}
|
|
|
|
</ReactMarkdown></div>)
|
2022-03-08 19:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ReactMarkdownPreview
|