#How to migrate from .md to .mdx
4 messages · Page 1 of 1 (latest)
Hi @blissful nova. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To migrate from .md to .mdx in Astro, you can follow these steps:
-
Install the
@astrojs/mdxintegration. You can do this by following the instructions provided in the Astro documentation (https://docs.astro.build/en/guides/integrations-guide/mdx/). -
Change your existing
.mdfile extensions to.mdx. -
Remove any
setup:properties from your frontmatter, and write any import statements below the frontmatter instead. Here's an example:
---
layout: '../../layouts/BaseLayout.astro'
title: 'Migrating to MDX'
date: 2022-07-26
tags: ["markdown", "mdx", "astro"]
---
import ReactCounter from '../../components/ReactCounter.jsx'
# {frontmatter.title}
Here is my counter component, working in MDX:
<ReactCounter client:load />
-
Update any
Astro.glob()statements that currently return.mdfiles so that they will now return your.mdxfiles. -
Update any use of the
<Content />component to use the default export when importing MDX:
---
// Multiple imports with Astro.glob
const mdxPosts = await Astro.glob('./posts/*.mdx');
---
{mdxPosts.map(Post => <Post.default />)}
---
// Import a single page
import { default as About } from './about.mdx';
---
<About />
- While you are transitioning to MDX, you may wish to enable the
legacy.astroFlavoredMarkdownflag (removed in v2.0) and include both.mdand.mdxfiles, so that your site continues to function normally even before all your files have been converted. Here is one way you can do that:
---
const mdPosts = await Astro.glob('../pages/posts/*.md');
const mdxPosts = await Astro.glob('../pages/posts/*.mdx');
const allPosts = [...mdxPosts, ...mdPosts];
---
Please note that the object returned when importing .mdx files (including using Astro.glob) differs from the object returned when importing .md files. However, frontmatter, file, and url work identically.