MDX blog just displays markdown content instead of rendering it while using MDXProvider
from @mdx-js/react
in Next JS
#1276
-
I am making a blog with MDX & Next.js but I am unable to render Markdown content. The blog post just shows markdown content as Here's my complete source code → https://github.com/deadcoder0904/blog-mdx-next/ I have the following folder structure: .
|-- README.md
|-- components
| `-- Image.js
|-- next.config.js
|-- package-lock.json
|-- package.json
|-- pages
| |-- _app.js
| |-- blog
| | `-- [slug].js
| |-- dark.css
| |-- index.js
| `-- new.css
|-- posts
| |-- blog
| | |-- hello-world
| | | |-- Rustin_Cohle.jpg
| | | `-- index.mdx
| | `-- shit-world
| | `-- index.mdx
| `-- tutorials
| `-- console-log-in-javascript
| `-- index.mdx
|-- prettier.config.js
`-- utils
`-- mdxUtils.js I have all my content in I have 2 folders in it: Each post is in their own folder inside of I want to render my The contents of it are as follows: pages/blog/[slug].jsimport fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import { MDXProvider } from '@mdx-js/react'
import { BLOG_PATH, blogFilePaths } from '../../utils/mdxUtils'
import { Image } from '../../components/Image'
const MDXComponents = { Image }
const Blog = ({ source, frontMatter }) => {
return (
<div>
<h1>{frontMatter.title}</h1>
<MDXProvider components={MDXComponents}>{source}</MDXProvider>
</div>
)
}
export async function getStaticPaths() {
const paths = blogFilePaths.map((path) => {
const split = path.split('/')
const slug = split[split.length - 2]
return {
params: {
slug,
},
}
})
return {
paths,
fallback: false,
}
}
export const getStaticProps = async ({ params }) => {
const { slug } = params
const blogFilePath = path.join(BLOG_PATH, `/blog/${slug}/index.mdx`)
const source = fs.readFileSync(blogFilePath)
const { content: mdx, data } = matter(source)
if (!blogFilePath) {
console.warn('No MDX file found for slug')
}
return {
props: {
source: mdx,
frontMatter: data,
},
}
}
export default Blog The important part is: <MDXProvider components={MDXComponents}>{source}</MDXProvider> I thought this would render markdown content but it only displays markdown content as a You can check the output at https://codesandbox.io/s/github/deadcoder0904/blog-mdx-next?file=/pages/blog/%5Bslug%5D.js by clicking any blog post. It displays the following when I click on How do I actually render the content? I looked at other Github repos like Tailwind CSS Blog & it works fine for them but I am not sure how it works sadly :( I do know that I have to convert the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Made this work (technically used another solution), check branch |
Beta Was this translation helpful? Give feedback.
Made this work (technically used another solution), check branch
tailwind
:)