can nextjs render markdown as component to be used inside a react component ?
Answered
Asian black bear posted this in #help-forum

Asian black bearOP
Just like the title I was wondering if it's possible to use MDX as a component inside the react component
Answered by iyxan23
7 Replies
Answer

or are you trying to render an mdx code right into an element or something?

Asian black bearOP
I wanted to render an MDX inside a react element, not just rendering it as a page

Asian black bearOP
something like
function Product({name,description,price,img}:{name: string, description: string, price:number,img:string}){
return (
<h1> {name} </h1>
<img src={img}/>
<MarkdownRender markdown={description}/>
<p> {price} </p>
)
}

@Asian black bear I wanted to render an MDX inside a react element, not just rendering it as a page

The set up guide above still helps. Just follow the guide and you can then import the mdx
import Welcome from '@/markdown/welcome.mdx'
export default function Page() {
return <Welcome />
}

Northeast Congo Lion
I use
Then where you need it,
If you have concerns about the markdown possibly having embedded HTML, you should sanitize the generated HTML using DOMPurify
marked
for this. Basically:const html_version = marked.parse(markdown_version);
Then where you need it,
<div dangerouslySetInnerHTML={{ __html: html_version }} />
If you have concerns about the markdown possibly having embedded HTML, you should sanitize the generated HTML using DOMPurify

Asian black bearOP
thanks everyone for the help, I'll try to implement the suggestion...