1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 15:14:23 +00:00
festa/components/generic/renderers/markdown.tsx

39 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Fragment, memo } from "react"
2022-06-11 03:08:49 +00:00
import { default as ReactMarkdown } from "react-markdown"
import { ReactMarkdownOptions } from "react-markdown/lib/react-markdown"
2022-07-26 23:51:06 +00:00
import { default as remarkGfm } from "remark-gfm"
2022-07-26 23:58:51 +00:00
import { default as remarkGemoji } from "remark-gemoji"
2022-06-11 03:08:49 +00:00
type FestaMarkdownRendererProps = Omit<ReactMarkdownOptions, "children"> & {
2022-06-11 03:08:49 +00:00
code: string,
}
/**
* Component rendering Markdown source with {@link ReactMarkdown}, using some custom settings to better handle user input.
*
* Currently, it converts `h1` and `h2` into `h3`, and disables the rendering of `img` elements.
*/
export const FestaMarkdownRenderer = memo(({ code, ...props }: FestaMarkdownRendererProps) => {
2022-06-11 03:08:49 +00:00
return (
<ReactMarkdown
{...props}
2022-06-11 03:08:49 +00:00
components={{
h1: "h2", // h1 is reserved for the page name
h2: "h3",
h3: "h4",
h4: "h5",
h5: "h6",
img: ({ }) => (<></>), // images reveal the IP of the user to third parties!
...props.components,
2022-06-11 03:08:49 +00:00
}}
2022-07-26 23:51:06 +00:00
remarkPlugins={[
remarkGfm,
2022-07-26 23:58:51 +00:00
remarkGemoji,
2022-07-26 23:51:06 +00:00
]}
2022-06-11 03:08:49 +00:00
>
{code}
</ReactMarkdown>
)
})
2022-07-16 14:12:29 +00:00
FestaMarkdownRenderer.displayName = "FestaMarkdownRenderer"