Currently, I am having an issue with converting MDX to HTML.
I'm doing it for Tailwind Blog
The complete code on Github → https://github.com/tailwindlabs/blog.tailwindcss.com
Here's the relevant code:
scripts/build-rss.js
import fs from 'fs'
import path from 'path'
import getAllPostPreviews from '../src/getAllPostPreviews'
import RSS from 'rss'
const siteUrl = 'https://blog.tailwindcss.com'
const feed = new RSS({
title: 'Blog – Tailwind CSS',
site_url: siteUrl,
feed_url: `${siteUrl}/feed.xml`,
})
getAllPostPreviews().forEach(({ link, module: { meta, default: html } }) => {
console.log(html)
const postText = `<div style="margin-top=55px; font-style: italic;">(The post <a href="${siteUrl + link}">${meta.title}</a> appeared first on <a href="${siteUrl}">Tailwind CSS Blog</a>.)</div>`;
feed.item({
title: meta.title,
guid: link,
url: `https://blog.tailwindcss.com${link}`,
date: meta.date,
description: meta.description,
custom_elements: [{
"content:encoded": html + postText
}].concat(meta.authors.map((author) => ({ author: [{ name: author.name }] }))),
})
})
fs.writeFileSync('./out/feed.xml', feed.xml({ indent: true }))
The html
variable logs to the console:
[Function: MDXContent] { isMDXComponent: true }
How do I get plain HTML?
This was a little complicated than I thought it would be. Anyways here are the changes I made to get the HTML:
I added
resourceQuery: /rss/
tonext.config.js
like:next.config.js
Used the above
rss
query ingetAllPostPreviews.js
by adding a new functiongetAllPosts
like:getAllPostPreviews.js
I exported
mdxComponents
fromPost.js
like:Post.js
Finally, changed
scripts/build-rss.js
to useReactDOMServer.renderToStaticMarkup()
by wrapping it inMDXProvider
like:Got the idea from https://ianmitchell.dev/blog/building-a-nextjs-blog-rss & https://github.com/IanMitchell/ianmitchell.dev