* feaet: allow remark and rehype plugin config * deps: add remark-gfm, remark-smartypants * feat: add gfm and smartypants by default * test: add GFM and remark plugin tests * feat: preserve default plugins with "extends" * docs: add remarkPlugins * docs: add rehypePlugins * chore: changeset * fix: remove skip from mdx tests * chore: dup hyperlink flavor text * chore: authGen -> autoGen * nit: markdown -> Markdown Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 1 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 2 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 3 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 4 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
6.1 KiB
@astrojs/mdx 📝
This Astro integration enables the usage of MDX components and allows you to create pages as .mdx
files.
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
Quick Install
The 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.
# Using NPM
npx astro add mdx
# Using Yarn
yarn astro add mdx
# Using PNPM
pnpx astro add mdx
Then, restart the dev server by typing CTRL-C
and then npm run astro dev
in the terminal window that was running Astro.
Because this command is new, it might not properly set things up. If that happens, feel free to log an issue on our GitHub and try the manual installation steps below.
Manual Install
First, install the @astrojs/mdx
package using your package manager. If you're using npm or aren't sure, run this in the terminal:
npm install @astrojs/mdx
Then, apply this integration to your astro.config.*
file using the integrations
property:
astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
export default defineConfig({
// ...
integrations: [mdx()],
});
Finally, restart the dev server.
Usage
To write your first MDX page 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.
Note
:
.mdx
files adhere to strict JSX syntax rather than Astro's HTML-like syntax.
Also check our Astro Integration Documentation for more on integrations.
Configuration
remarkPlugins
Default plugins: remark-gfm, remark-smartypants
Remark 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!
We apply GitHub-flavored Markdown and Smartypants by default. This brings some niceties like auto-generating clickable links from text (ex. https://example.com
) and formatting quotes for readability. When applying your own plugins, you can choose to preserve or remove these defaults.
To apply plugins while preserving Astro's default plugins, use a nested extends
object like so:
// astro.config.mjs
import remarkToc from 'remark-toc';
export default {
integrations: [mdx({
// apply remark-toc alongside GitHub-flavored markdown and Smartypants
remarkPlugins: { extends: [remarkToc] },
})],
}
To apply plugins without Astro's defaults, you can apply a plain array:
// astro.config.mjs
import remarkToc from 'remark-toc';
export default {
integrations: [mdx({
// apply remark-toc alone, removing other defaults
remarkPlugins: [remarkToc],
})],
}
rehypePlugins
Default plugins: none
Rehype plugins allow you to transform the HTML that your Markdown generates. We recommend checking the Remark plugin catalog first before considering rehype plugins, since most users want to transform their Markdown syntax instead. If HTML transforms are what you need, we encourage you to browse awesome-rehype for a full curated list of plugins!
To apply rehype plugins, use the rehypePlugins
configuration option like so:
// astro.config.mjs
import rehypeMinifyHtml from 'rehype-minify';
export default {
integrations: [mdx({
rehypePlugins: [rehypeMinifyHtml],
})],
}
Examples
- The Astro MDX example shows how to use MDX files in your Astro project.
Troubleshooting
For help, check out the #support-threads
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
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
Changelog
See CHANGELOG.md for a history of changes to this integration.