How to use rehype-react single #52
-
※ I'm not good at English. So please be kind ❤️ I'm creating Gatsby site. And I use Then I can use rehype-react like this. /* ~/util/markdown.ts */
import rehypeReact from "rehype-react"
import { createElement } from "react"
export const renderAst = new rehypeReact({
createElement,
components: {
// custom component
p: (props) => <SomeParagraph {...props} />,
},
}).Compiler
/* ~/pages/index.tsx */
import { renderAst } from "~/util/markdown"
export default IndexPage = () => {
const data = useStaticQuery(/* this is Gatsby feature to fetch data */)
// markdownAst is transformed by remark
const markdownAst = data.markdownRemark.htmlAst
return (
<div>{renderAst(markdownAst)}</div>
)
} This works completely except typescript errors.
Why don't you provide a way to use only rehype-react. Or do you have any solutions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I'd recommend using it as a unified plugin, as shown in the type tests https://github.com/rehypejs/rehype-react/blob/main/types/rehype-react-test.tsx const reactElement = unified.use(rehypeReact, {
createElement,
components: {
// custom component
p: (props) => <SomeParagraph {...props} />,
},
}).stringify(htmlAst).result |
Beta Was this translation helpful? Give feedback.
I'd recommend using it as a unified plugin, as shown in the type tests https://github.com/rehypejs/rehype-react/blob/main/types/rehype-react-test.tsx
For your specific case where you only need to stringify/compile to HTML, it would look something like: