@astrojs/mdx
This Astro integration enables the usage of MDX components and allows you to create pages as .mdx
files.
Why MDX?
Section titled Why MDX?MDX is the defacto solution for embedding components, such as interactive charts or alerts, within Markdown content. If you have existing content authored in MDX, this integration makes migrating to Astro a breeze.
Want to learn more about MDX before using this integration?
Check out “What is MDX?”, a deep-dive on the MDX format.
Installation
Section titled InstallationQuick Install
Section titled Quick InstallThe astro add
command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren’t sure which package manager you’re using, run the first command.) Then, follow the prompts, and type “y” in the terminal (meaning “yes”) for each one.
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
Manual Install
Section titled Manual InstallFirst, install the @astrojs/mdx
package using your package manager. If you’re using npm or aren’t sure, run this in the terminal:
Then, apply this integration to your astro.config.*
file using the integrations
property:
Finally, restart the dev server.
Editor Integration
Section titled Editor IntegrationVS Code supports Markdown by default. However, for MDX editor support, you may wish to add the following setting in your VSCode config. This ensures authoring MDX files provides a Markdown-like editor experience.
Usage
Section titled UsageYou can add MDX pages to your project by adding .mdx
files within your src/pages/
directory.
Components
Section titled ComponentsTo use components in your MDX pages in Astro, head to our UI framework documentation. You’ll explore:
- 📦 how framework components are loaded,
- 💧 client-side hydration options, and
- 🤝 opportunities to mix and nest frameworks together
Client Directives are still required in .mdx
files.
Variables
Section titled VariablesMDX supports export
statements to add variables to your templates. These variables are accessible both from the template itself and as named properties when importing the template somewhere else.
For instance, you can export a title
field from an MDX page or component to use as a heading with {JSX expressions}
:
This title
will be accessible from import
and glob statements as well:
See the official “how MDX works” guide for more on MDX variables.
Exported properties
Section titled Exported propertiesAlongside your MDX variable exports, we generate a few helpful exports as well. These are accessible when importing an MDX file via import
statements or Astro.glob
.
The absolute path to the MDX file (e.g. home/user/projects/.../file.md
).
The browser-ready URL for MDX files under src/pages/
. For example, src/pages/en/about.mdx
will provide a url
of /en/about/
. For MDX files outside of src/pages
, url
will be undefined
.
getHeadings()
Section titled getHeadings()Returns: { depth: number; slug: string; text: string }[]
A function that returns an array of all headings (i.e. h1 -> h6
elements) in the MDX file. Each heading’s slug
corresponds to the generated ID for a given heading and can be used for anchor links.
Frontmatter
Section titled FrontmatterAstro also supports YAML-based frontmatter out-of-the-box. By default, all variables declared in a frontmatter fence (---
) will be accessible via the frontmatter
export.
For example, we can add a title
and publishDate
to an MDX page or component like so:
Now, this title
and publishDate
will be accessible from import
and glob statements via the frontmatter
property. This matches the behavior of plain markdown in Astro as well!
Inject frontmatter via remark or rehype plugins
Section titled Inject frontmatter via remark or rehype pluginsYou may want to inject frontmatter properties across all of your MDX files. By using a remark or rehype plugin, you can generate these properties based on a file’s contents.
You can append to the data.astro.frontmatter
property from your plugin’s file
argument like so:
After applying this plugin to your MDX integration config:
…every MDX file will have customProperty
in its frontmatter! See our Markdown documentation for more usage instructions and a reading time plugin example.
Layouts
Section titled LayoutsLayouts can be applied in the same way as standard Astro Markdown. You can add a layout
to your frontmatter like so:
Then, you can retrieve all other frontmatter properties from your layout via the frontmatter
property, and render your MDX using the default <slot />
. See layout props for a complete list of props available.
You can set a layout’s Props
type with the MDXLayoutProps
helper.
Layout props
Section titled Layout propsAll exported properties are available from Astro.props
in your layout, with two key differences:
- Heading information (i.e.
h1 -> h6
elements) is available via theheadings
array, rather than agetHeadings()
function. file
andurl
are also available as nestedfrontmatter
properties (i.e.frontmatter.url
andfrontmatter.file
). This is consistent with Astro’s Markdown layout properties.
Astro recommends using the MDXLayoutProps
type (see previous section) to explore all available properties.
Importing layouts manually
Section titled Importing layouts manuallyYou may need to pass information to your layouts that does not (or cannot) exist in your frontmatter. In this case, you can import and use a <Layout />
component like any other component:
Then, your values are available to you through Astro.props
in your layout, and your MDX content will be injected into the page where your <slot />
component is written:
Custom components
Section titled Custom componentsUnder the hood, MDX will convert Markdown into HTML components. For example, this blockquote:
will be converted into this HTML:
But what if you want to specify your own markup for these blockquotes? In the above example, you could create a custom <Blockquote />
component (in any language) that either has a <slot />
component or accepts a children
prop.
Then in the MDX file you import the component and export it to the components
export.
Now, writing the standard Markdown blockquote syntax (>
) will use your custom <Blockquote />
component instead. No need to use a component in Markdown, or write a remark/rehype plugin! Visit the MDX website for a full list of HTML elements that can be overwritten as custom components.
Custom components with imported mdx
Section titled Custom components with imported mdxWhen rendering imported MDX content, custom components can be passed via the components
prop.
Note: An MDX file’s exported components will not be used unless you manually import and pass them via the components
property. See the example below:
Syntax highlighting
Section titled Syntax highlightingThe MDX integration respects your project’s markdown.syntaxHighlight
configuration.
We will highlight your code blocks with Shiki by default. You can customize this highlighter using the markdown.shikiConfig
option in your astro.config
. For example, you can apply a different built-in theme like so:
Visit our Shiki configuration docs for more on using Shiki with Astro.
Switch to Prism
Section titled Switch to PrismYou can also use the Prism syntax highlighter by setting markdown.syntaxHighlight
to 'prism'
in your astro.config
like so:
This applies a minimal Prism renderer with added support for astro
code blocks. Visit our “Prism configuration” docs for more on using Prism with Astro.
Switch to a custom syntax highlighter
Section titled Switch to a custom syntax highlighterYou may want to apply your own syntax highlighter too. If your highlighter offers a remark or rehype plugin, you can flip off our syntax highlighting by setting markdown.syntaxHighlight: false
and wiring up your plugin. For example, say you want to apply Shiki Twoslash’s remark plugin:
Configuration
Section titled ConfigurationremarkPlugins
Section titled remarkPluginsRemark plugins allow you to extend your Markdown with new capabilities. This includes auto-generating a table of contents, applying accessible emoji labels, and more. We encourage you to browse awesome-remark for a full curated list!
This example applies the remark-toc
plugin to .mdx
files. To customize plugin inheritance from your Markdown config or Astro’s defaults, see the extendPlugins
option.
rehypePlugins
Section titled rehypePluginsRehype plugins allow you to transform the HTML that your Markdown generates. We encourage you to browse awesome-rehype for a full curated list of plugins!
We apply our own (non-removable) collect-headings
plugin. This applies IDs to all headings (i.e. h1 -> h6
) in your MDX files to link to headings via anchor tags.
This example applies the rehype-minify
plugin to .mdx
files. To customize plugin inheritance from your Markdown config or Astro’s defaults, see the extendPlugins
option.
extendPlugins
Section titled extendPluginsType: 'markdown' | 'astroDefaults' | false
Default: 'markdown'
markdown
(default)
Section titled markdown (default)By default, Astro inherits all remark and rehype plugins from the markdown
option in your Astro config. This also respects the markdown.extendDefaultPlugins
option to extend Astro’s defaults. Any additional plugins you apply in your MDX config will be applied after your configured Markdown plugins.
This example applies remark-toc
to Markdown and MDX, and rehype-minify
to MDX alone:
astroDefaults
Section titled astroDefaultsYou may only want to extend Astro’s default plugins without inheriting your Markdown config. This example will apply the default GitHub-Flavored Markdown and Smartypants plugins alongside remark-toc
:
false
Section titled falseIf you don’t want to extend any plugins, set extendPlugins
to false
:
recmaPlugins
Section titled recmaPluginsThese are plugins that modify the output estree directly. This is useful for modifying or injecting JavaScript variables in your MDX files.
We suggest using AST Explorer to play with estree outputs, and trying estree-util-visit
for searching across JavaScript nodes.
remarkRehype
Section titled remarkRehypeMarkdown content is transformed into HTML through remark-rehype which has a number of options.
You can use remark-rehype options in your MDX integration config file like so:
This inherits the configuration of markdown.remarkRehype
. This behavior can be changed by configuring extendPlugins
.
Examples
Section titled Examples- The Astro MDX example shows how to use MDX files in your Astro project.
Troubleshooting
Section titled TroubleshootingFor help, check out the #support
channel on Discord. Our friendly Support Squad members are here to help!
You can also check our Astro Integration Documentation for more on integrations.
Contributing
Section titled ContributingThis package is maintained by Astro’s Core team. You’re welcome to submit an issue or PR!
Changelog
Section titled ChangelogSee CHANGELOG.md for a history of changes to this integration.