0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/mdx
Erika 1f92d64ea3
Drop Node 14 support (#5782)
* chore: Update engines field

* fix(deps): Remove node-fetch

* feat(polyfills): Remove node-fetch for undici

* feat(webapi): Remove node-fetch from the webapis polyfills for undici

* feat(core): Remove node-fetch for undici in Astro core

* feat(telemetry): Remove node-fetch for undici

* feat(node): Remove node-fetch for undici in node integration

* feat(vercel): Remove node-fetch for undici in Vercel integration

* chore: update lockfile

* chore: update lockfile

* chore: changeset

* fix(set): Fix set directives not streaming correctly on Node 16

* Try another approach

* Debugging

* Debug fetch

* Use global fetch if there is one

* changeset for lit

* Remove web-streams-polyfill

* Remove web-streams-polyfill license note

* Update .changeset/stupid-wolves-explain.md

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
2023-01-09 16:59:20 -05:00
..
src Add SmartyPants flag (#5769) 2023-01-06 09:26:02 -05:00
test Add SmartyPants flag (#5769) 2023-01-06 09:26:02 -05:00
CHANGELOG.md [ci] release (beta) (#5742) 2023-01-06 13:00:12 -05:00
package.json Drop Node 14 support (#5782) 2023-01-09 16:59:20 -05:00
README.md Fix docs links in MDX README (#5741) 2023-01-04 11:46:05 +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 configure how your MDX is rendered with the following options:

Options inherited from Markdown config

All markdown configuration options except drafts can be configured separately in the MDX integration. This includes remark and rehype plugins, syntax highlighting, and more. Options will default to those in your Markdown config (see the extendMarkdownConfig option to modify this).

:::note There is no separate MDX configuration for including pages marked as draft in the build. This Markdown setting will be respected by both Markdown and MDX files and cannot be overriden for MDX files specifically. :::

// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import remarkToc from 'remark-toc';
import rehypeMinifyHtml from 'rehype-minify-html';

export default defineConfig({
  integrations: [
    mdx({
      syntaxHighlight: 'shiki',
      shikiConfig: { theme: 'dracula' },
      remarkPlugins: [remarkToc],
      rehypePlugins: [rehypeMinifyHtml],
      remarkRehype: { footnoteLabel: 'Footnotes' },
      gfm: false,
    })
  ]
})

:::caution MDX does not support passing remark and rehype plugins as a string. You should install, import, and apply the plugin function instead. :::

📚 See the Markdown Options reference for a complete list of options.

extendMarkdownConfig

  • Type: boolean
  • Default: true

MDX will extend your project's existing Markdown configuration by default. To override individual options, you can specify their equivalent in your MDX configuration.

For example, say you need to disable GitHub-Flavored Markdown and apply a different set of remark plugins for MDX files. You can apply these options like so, with extendMarkdownConfig enabled by default:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';

export default defineConfig({
  markdown: {
    syntaxHighlight: 'prism',
    remarkPlugins: [remarkPlugin1],
    gfm: true,
  },
  integrations: [
    mdx({
      // `syntaxHighlight` inherited from Markdown

      // Markdown `remarkPlugins` ignored,
      // only `remarkPlugin2` applied.
      remarkPlugins: [remarkPlugin2],
      // `gfm` overridden to `false`
      gfm: false,
    })
  ]
});

You may also need to disable markdown config extension in MDX. For this, set extendMarkdownConfig to false:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';

export default defineConfig({
  markdown: {
    remarkPlugins: [remarkPlugin1],
  },
  integrations: [
    mdx({
      // Markdown config now ignored
      extendMarkdownConfig: false,
      // No `remarkPlugins` applied
    })
  ]
});

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.