astro/packages/integrations/mdx
Ben Holmes e2019be6ff
Change frontmatter injection ordering (#5687)
* feat: make user frontmatter accessible in md

* test: new frontmatter injection

* refactor: move injection utils to remark pkg

* fix: add dist/internal to remark exports

* feat: update frontmater injection in mdx

* tests: new mdx injection

* chore: changeset

* chore: simplify frontmatter destructuring

* fix: remove old _internal references

* refactor: injectedFrontmatter -> remarkPluginFrontmatter

* docs: add content collections change

* chore: changeset heading levels
2023-01-03 16:31:19 -05:00
..
src Change frontmatter injection ordering (#5687) 2023-01-03 16:31:19 -05:00
test Change frontmatter injection ordering (#5687) 2023-01-03 16:31:19 -05:00
CHANGELOG.md [ci] release (#5645) 2022-12-27 11:52:24 -05:00
package.json Upgrade to Vite 4 (#5685) 2023-01-03 13:24:49 -05:00
README.md Fix link in MDX integration README (#5586) 2022-12-12 14:14:24 +01:00
tsconfig.json MDX support (#3706) 2022-06-30 13:09:09 -05:00

@astrojs/mdx 📝

This Astro integration enables the usage of MDX components and allows you to create pages as .mdx files.

Why MDX?

MDX allows you to use variables, JSX expressions and components within Markdown content in Astro. If you have existing content authored in MDX, this integration allows you to bring those files to your Astro project.

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
pnpm astro add mdx

If you run into any issues, feel free to report them to us on 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()],
});

Editor Integration

VS 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.

"files.associations": {
    "*.mdx": "markdown"
}

Usage

With the Astro MDX integration, you can add MDX pages to your project by adding .mdx files within your src/pages/ directory. You can also import .mdx files into .astro files.

Astro's MDX integration adds extra features to standard MDX, including Markdown-style frontmatter. This allows you to use most of Astro's built-in Markdown features like a special frontmatter layout property and a property for marking a page as a draft.

See how MDX works in Astro with examples in our Markdown & MDX guide.

Visit the MDX docs to learn about using standard MDX features.

Configuration

Once the MDX integration is installed, no configuration is necessary to use .mdx files in your Astro project.

You can extend how your MDX is rendered by adding remark, rehype and recma plugins.

extendPlugins

You can customize how MDX files inherit your projects existing Markdown configuration using the extendPlugins option.

markdown (default)

Astro's MDX files will inherit all markdown options in your Astro configuration file, which includes the GitHub-Flavored Markdown and Smartypants plugins by default.

Any additional plugins you apply in your MDX config will be applied after your configured Markdown plugins.

astroDefaults

Astro's MDX files will apply only Astro's default plugins, without inheriting the rest of your Markdown config.

This example will apply the default GitHub-Flavored Markdown and Smartypants plugins alongside remark-toc to your MDX files, while ignoring any markdown.remarkPlugins configuration:

// astro.config.mjs
import remarkToc from 'remark-toc';

export default {
  markdown: {
    remarkPlugins: [/** ignored */]
  },
  integrations: [mdx({
    remarkPlugins: [remarkToc],
    // Astro defaults applied
    extendPlugins: 'astroDefaults',
  })],
}

false

Astro's MDX files will not inherit any markdown options, nor will any Astro Markdown defaults be applied:

// astro.config.mjs
import remarkToc from 'remark-toc';

export default {
  integrations: [mdx({
    remarkPlugins: [remarkToc],
    // Astro defaults not applied
    extendPlugins: false,
  })],
}

remarkRehype

Markdown content is transformed into HTML through remark-rehype which has a number of options.

You can set remark-rehype options in your config file:

// astro.config.mjs
export default {
  integrations: [mdx({
    remarkRehype: {
      footnoteLabel: 'Catatan kaki',
      footnoteBackLabel: 'Kembali ke konten',
    },
  })],
};

This inherits the configuration of markdown.remarkRehype. This behavior can be changed by configuring extendPlugins.

remarkPlugins

Browse awesome-remark for a full curated list of remark plugins to extend your Markdown's capabilities.

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.

// astro.config.mjs
import remarkToc from 'remark-toc';

export default {
  integrations: [mdx({
    remarkPlugins: [remarkToc],
  })],
}

rehypePlugins

Browse awesome-rehype for a full curated list of Rehype plugins to transform the HTML that your Markdown generates.

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-accessible-emojis plugin to .mdx files. To customize plugin inheritance from your Markdown config or Astro's defaults, see the extendPlugins option.

// astro.config.mjs
import rehypeAccessibleEmojis from 'rehype-accessible-emojis';

export default {
  integrations: [mdx({
    rehypePlugins: [rehypeAccessibleEmojis],
  })],
}

recmaPlugins

These 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.

Examples

Troubleshooting

For 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

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.