2022-03-08 16:39:24 -08:00
|
|
|
import remarkGfm from "remark-gfm"
|
2022-03-21 17:20:41 -07:00
|
|
|
import SyntaxHighlighter from 'react-syntax-highlighter/dist/cjs/prism-async-light';
|
2022-03-20 14:09:56 -07:00
|
|
|
import rehypeSlug from 'rehype-slug'
|
|
|
|
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
|
2022-03-21 22:50:25 -07:00
|
|
|
import rehypeRaw from 'rehype-raw'
|
2022-03-08 16:39:24 -08:00
|
|
|
|
|
|
|
// @ts-ignore because of no types in remark-a11y-emoji
|
2022-03-21 15:55:21 -07:00
|
|
|
// import a11yEmoji from '@fec/remark-a11y-emoji';
|
2022-03-20 14:09:56 -07:00
|
|
|
|
2022-03-08 16:39:24 -08:00
|
|
|
import styles from './preview.module.css'
|
2022-03-21 15:55:21 -07:00
|
|
|
import dark from 'react-syntax-highlighter/dist/cjs/styles/prism/vsc-dark-plus'
|
|
|
|
import light from 'react-syntax-highlighter/dist/cjs/styles/prism/vs'
|
2022-03-13 01:40:28 -03:00
|
|
|
import useSharedState from "@lib/hooks/use-shared-state";
|
2022-03-21 20:43:50 -07:00
|
|
|
import ReactMarkdown from "react-markdown";
|
|
|
|
|
2022-03-08 16:39:24 -08:00
|
|
|
type Props = {
|
|
|
|
content: string | undefined
|
|
|
|
height: number | string
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReactMarkdownPreview = ({ content, height }: Props) => {
|
2022-03-09 14:57:44 -08:00
|
|
|
const [themeType] = useSharedState<string>('theme')
|
2022-03-21 20:43:50 -07:00
|
|
|
|
2022-03-09 14:57:44 -08:00
|
|
|
return (<div style={{ height }}>
|
2022-03-20 14:09:56 -07:00
|
|
|
<ReactMarkdown className={styles.markdownPreview}
|
2022-03-21 15:55:21 -07:00
|
|
|
remarkPlugins={[remarkGfm]}
|
2022-03-21 22:50:25 -07:00
|
|
|
rehypePlugins={[rehypeSlug, [rehypeAutolinkHeadings, { behavior: 'wrap' }], rehypeRaw]}
|
2022-03-09 14:57:44 -08:00
|
|
|
components={{
|
|
|
|
code({ node, inline, className, children, ...props }) {
|
|
|
|
const match = /language-(\w+)/.exec(className || '')
|
|
|
|
return !inline && match ? (
|
|
|
|
<SyntaxHighlighter
|
|
|
|
lineNumberStyle={{
|
2022-03-09 17:11:37 -08:00
|
|
|
minWidth: "2.25rem"
|
2022-03-09 14:57:44 -08:00
|
|
|
}}
|
|
|
|
customStyle={{
|
|
|
|
padding: 0,
|
|
|
|
margin: 0,
|
|
|
|
background: 'transparent'
|
|
|
|
}}
|
|
|
|
codeTagProps={{
|
2022-03-20 20:45:37 -07:00
|
|
|
style: { background: 'transparent', color: 'inherit' }
|
2022-03-09 14:57:44 -08:00
|
|
|
}}
|
2022-03-11 15:59:52 -08:00
|
|
|
style={themeType === 'dark' ? dark : light}
|
2022-03-09 14:57:44 -08:00
|
|
|
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 16:39:24 -08:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:09:56 -07:00
|
|
|
|
2022-03-08 16:39:24 -08:00
|
|
|
export default ReactMarkdownPreview
|