onedrive/components/previews/MarkdownPreview.tsx

111 lines
3 KiB
TypeScript
Raw Normal View History

import { useEffect, FunctionComponent, CSSProperties } from 'react'
2021-06-25 15:08:04 +00:00
import Prism from 'prismjs'
import ReactMarkdown from 'react-markdown'
import gfm from 'remark-gfm'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'
2021-08-15 19:48:29 +00:00
import rehypeRaw from 'rehype-raw'
2021-06-25 15:08:04 +00:00
import 'katex/dist/katex.min.css'
import FourOhFour from '../FourOhFour'
import Loading from '../Loading'
import DownloadBtn from '../DownloadBtn'
2021-08-29 14:50:38 +00:00
import { useStaleSWR } from '../../utils/tools'
2021-06-25 15:08:04 +00:00
const MarkdownPreview: FunctionComponent<{ file: any; path: string; standalone?: boolean }> = ({
file,
path,
standalone = true,
}) => {
2021-08-29 14:50:38 +00:00
const { data, error } = useStaleSWR(file['@microsoft.graph.downloadUrl'])
2021-08-23 15:14:08 +00:00
// The parent folder of the markdown file, which is also the relative image folder
const parentPath = path.substring(0, path.lastIndexOf('/'))
// Check if the image is relative path instead of a absolute url
const isUrlAbsolute = url => url.indexOf('://') > 0 || url.indexOf('//') === 0
// Custom renderer to render images with relative path
const relativeImagePathRenderer = {
img: ({
alt,
src,
title,
width,
height,
style,
}: {
alt?: string
src?: string
title?: string
width?: string | number
height?: string | number
style?: CSSProperties
}) => {
if (isUrlAbsolute(src as string)) {
return (
// eslint-disable-next-line @next/next/no-img-element
<img alt={alt} src={src} title={title} width={width} height={height} style={style} />
)
}
return (
// eslint-disable-next-line @next/next/no-img-element
<img
alt={alt}
src={`/api?path=${parentPath}/${src}&raw=true`}
title={title}
width={width}
height={height}
style={style}
/>
)
},
}
2021-06-25 15:08:04 +00:00
useEffect(() => {
Prism.highlightAll()
2021-06-25 15:08:04 +00:00
}, [data])
if (error) {
return (
2021-08-23 15:14:08 +00:00
<div className={`${standalone ? 'shadow bg-white dark:bg-gray-900 rounded p-3' : ''}`}>
2021-06-25 15:08:04 +00:00
<FourOhFour errorMsg={error.message} />
</div>
)
}
if (!data) {
return (
2021-08-23 15:14:08 +00:00
<div className={standalone ? 'shadow bg-white dark:bg-gray-900 rounded p-3' : ''}>
2021-06-25 15:08:04 +00:00
<Loading loadingText="Loading file content..." />
</div>
)
}
return (
<>
2021-08-23 15:14:08 +00:00
<div
className={
standalone
? 'markdown-body shadow bg-white dark:bg-gray-900 rounded p-3 dark:text-white'
: 'markdown-body p-3 dark:text-white'
}
>
{/* Using rehypeRaw to render HTML inside Markdown is potentially dangerous, use under safe environments. (#18) */}
<ReactMarkdown
remarkPlugins={[gfm, remarkMath]}
rehypePlugins={[rehypeKatex, rehypeRaw as any]}
components={relativeImagePathRenderer}
>
2021-06-25 15:08:04 +00:00
{data}
</ReactMarkdown>
</div>
2021-06-30 11:53:17 +00:00
{standalone && (
<div className="mt-4">
<DownloadBtn downloadUrl={file['@microsoft.graph.downloadUrl']} />
</div>
)}
2021-06-25 15:08:04 +00:00
</>
)
}
export default MarkdownPreview