Moving my blog from Remark to MDX.
MDX means that you can use JSX components in your markdown.
This means that blog posts down the road can have any type of content. Embed tweets, iframes without wandering around for a remark plugin, use live, editable snapshots, provide beautiful chart based visualisations, showcase some animations, bounded only by imagination (instead of remark plugins)
mdx
on your project with:npm install @mdx-js/mdx @mdx-js/tag
gatsby
plugin for mdx
:gatsby-mdx
gatsby-config.js
plugins: [
{
resolve: `gatsby-mdx`,
options: {
defaultLayouts: { default: path.resolve("./src/components/layout.js") },
},
},
];
Change the gatsby-node
page generation algorithm for mdx
:
{
- allMarkdownRemark {
+ allMdx {
}
result.data.allMdx.edges;
onCreateNode
, when checking for node.internal.type
:if (node.internal.type === `Mdx`) {
{
- allMarkdownRemark {
+ allMdx {
}
Sure you can. That's the beauty.
Gatsby MDX provides an option for gatsbyRemarkPlugins
to be included. You can use it like:
options: {
defaultLayouts: {
default: path.resolve('./src/templates/blog-post.js'),
},
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
showCaptions: true,
},
},
{
resolve: `gatsby-remark-external-links`,
},
]
}
Caution: It does not work with the `` format just yet. So you will always have to use an object with resolve
key to put it in there.
Now, you can rename all files to mdx
and restart the server.
I got lots and lots.
SyntaxError: unknown: Expected corresponding JSX closing tag for <img>
If you were using Github flavored markdown before, there are chances that you were embedding some JSX earlier. But the transition to MDX means that all that is going to be treated as JSX from now and rules for JSX applies.
Images are <img />
and not <img>
Once this is fixed, you can restart the server (I had to delete the cache and try too)
Not so good. But I'm hoping for it to get better.
There were several times during this when I had to stop and start the server to have it working. Worse, there were times when deleting the cache and restarting was the only way to fix the issue.
MDX isn't very good at errors either. If you make an import error, which you will obviously do, MDX throws a cryptic error (with bright red background) and you have to restart the server to make it go away.
The editor support is not very good. Unlike remark, there is no output directly from VS Code. You can install the MDX language support to get more syntax highlighting. But it might stop working when there is a space or tab extra. So barely working! Because of the support, writing articles becomes a pain with editor showing off unnecessary suggestions when typing.
I'm also moving from remark-prismjs
to prism-react-renderer
now that I can write JSX components. That has been smooth and hope to have more customisations working on how code is being shown. There has been a bug bothering me, with prism-react-renderer
and mdx
renderer not allowing any blank lines in between code. Currently that is hotfixed with spaces on those lines, but that is not going to be pretty to copy. Have to get that fixed.
Is there something I missed? Something wrong? Something good? Ping me on Twitter