@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 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
Section titled InstallationAstro includes an astro add
command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.
Run one of the following commands in a new terminal window.
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:
Then, apply the integration to your astro.config.*
file using the integrations
property:
Editor Integration
Section titled Editor IntegrationFor editor support in VS Code, install the official MDX extension.
For other editors, use the MDX language server.
Usage
Section titled UsageWith 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.
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
Section titled ConfigurationOnce 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
Section titled Options inherited from Markdown configAll markdown
configuration options 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).
MDX does not support passing remark and rehype plugins as a string. You should install, import, and apply the plugin function instead.
extendMarkdownConfig
Section titled 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:
You may also need to disable markdown
config extension in MDX. For this, set extendMarkdownConfig
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.
optimize
Section titled optimize- Type:
boolean | { ignoreElementNames?: string[] }
This is an optional configuration setting to optimize the MDX output for faster builds and rendering via an internal rehype plugin. This may be useful if you have many MDX files and notice slow builds. However, this option may generate some unescaped HTML, so make sure your site’s interactive parts still work correctly after enabling it.
This is disabled by default. To enable MDX optimization, add the following to your MDX integration configuration:
ignoreElementNames
Section titled ignoreElementNames- Type:
string[]
أُضيفت في:
@astrojs/mdx@3.0.0
Previously known as customComponentNames
.
An optional property of optimize
to prevent the MDX optimizer from handling certain element names, like custom components passed to imported MDX content via the components prop.
You will need to exclude these components from optimization as the optimizer eagerly converts content into a static string, which will break custom components that needs to be dynamically rendered.
For example, the intended MDX output of the following is <Heading>...</Heading>
in place of every "<h1>...</h1>"
:
To configure optimization for this using the ignoreElementNames
property, specify an array of HTML element names that should be treated as custom components:
Note that if your MDX file configures custom components using export const components = { ... }
, then you do not need to manually configure this option. The optimizer will automatically detect them.
Examples
Section titled Examples- The Astro MDX starter template shows how to use MDX files in your Astro project.