Last active
July 31, 2024 11:30
-
-
Save Profesor08/63f8a23944cbb98705477198639cd3a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const Example1 = () => { | |
return ( | |
<Markdown className="terms-text" inline> | |
This is an exmplple of usage for [this gist](https://gist.github.com/Profesor08/63f8a23944cbb98705477198639cd3a5) in react | |
</Markdown> | |
); | |
}; | |
export const Example2 = () => { | |
return ( | |
<div><Markdown as="span" inline> | |
[this gist](https://gist.github.com/Profesor08/63f8a23944cbb98705477198639cd3a5) in react | |
</Markdown></div> | |
); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { marked, MarkedOptions } from "marked"; | |
import React, { useMemo } from "react"; | |
interface Markdown { | |
<T extends keyof JSX.IntrinsicElements = "div">( | |
props: React.HTMLAttributes<T> & { | |
as?: T; | |
children?: string; | |
inline?: boolean; | |
options?: Omit<MarkedOptions, "async">; | |
}, | |
): JSX.Element; | |
} | |
export const Markdown: Markdown = ({ | |
as: Component = "div", | |
children, | |
inline, | |
options, | |
...props | |
}) => { | |
const html = useMemo(() => { | |
const markdown = children ?? ""; | |
return { | |
__html: (inline === true | |
? marked.parseInline(markdown, options) | |
: marked.parse(markdown, options)) as string, | |
}; | |
}, [children, inline, options]); | |
// @ts-ignore | |
return <Component dangerouslySetInnerHTML={html} {...props} />; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment