Markdown & MDX
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
Markdown is commonly used to author text-heavy content like blog posts and documentation. Astro includes built-in support for standard Markdown files that can also include frontmatter YAML to define custom metadata such as a title, description, and tags.
With the @astrojs/mdx integration installed, Astro also supports MDX (.mdx
) files which bring added features like support for JavaScript expressions and components in your Markdown content.
Use either or both types of files to write your Markdown content!
Markdown and MDX Pages
Section titled Markdown and MDX PagesContent collections
Section titled Content collectionsYou can manage your Markdown and MDX files in Astro in a special src/content/
folder. Content collections help you organize your content, validate your frontmatter, and provide automatic TypeScript type-safety while working with your content.
Directorysrc/content/
Directorynewsletter/
- week-1.md
- week-2.md
Directoryauthors/
- grace-hopper.md
- alan-turing.md
See more about using content collections in Astro.
File-based Routing
Section titled File-based RoutingAstro treats any .md
(or alternative supported extension) or .mdx
file inside of the /src/pages/
directory as a page.
Placing a file in this directory, or any sub-directory, will automatically build a page route using the pathname of the file.
Markdown Features
Section titled Markdown FeaturesAstro provides some extra, built-in Markdown features available when using Markdown and MDX files.
Frontmatter layout
Section titled Frontmatter layoutAstro provides Markdown and MDX pages with a special frontmatter layout
property that can specify a relative path (or alias) to an Astro layout component.
Specific properties are then available to the layout component through Astro.props
. For example, you can access frontmatter properties through Astro.props.frontmatter
:
You can also style your Markdown in your layout component.
Heading IDs
Section titled Heading IDsUsing headings in Markdown and MDX will automatically give you anchor links so you can link directly to certain sections of your page.
Astro generates heading id
s based on github-slugger
. You can find more examples in the github-slugger documentation.
Escaping special characters
Section titled Escaping special charactersCertain characters have a special meaning in Markdown and MDX. You may need to use a different syntax if you want to display them. To do this, you can use HTML entities for these characters instead.
For example, to prevent <
being interpreted as the beginning of an HTML element, write <
. Or, to prevent {
being interpreted as the beginning of a JavaScript expression in MDX, write {
.
MDX-only Features
Section titled MDX-only FeaturesAdding the Astro MDX integration enhances your Markdown authoring with JSX variables, expressions and components.
It also adds extra features to standard MDX, including support for Markdown-style frontmatter in MDX. This allows you to use most of Astro’s built-in Markdown features like a frontmatter layout
property.
.mdx
files must be written in MDX syntax rather than Astro’s HTML-like syntax.
Using Exported Variables in MDX
Section titled Using Exported Variables in MDXMDX supports using export
statements to add variables to your MDX content. These variables are accessible both in the template itself and as named properties when importing the file somewhere else.
For example, you can export a title
field from an MDX page or component to use as a heading with {JSX expressions}
:
Using Frontmatter Variables in MDX
Section titled Using Frontmatter Variables in MDXThe Astro MDX integration includes support for using frontmatter in MDX by default. Add frontmatter properties just as you would in Markdown files, and these variables are accessible to use in the template, in its layout
component, and as named properties when importing the file somewhere else.
Using Components in MDX
Section titled Using Components in MDXAfter installing the MDX integration, you can import and use both Astro components and UI framework components in MDX (.mdx
) files just as you would use them in any other Astro component.
Don’t forget to include a client:directive
on your UI framework components, if necessary!
See more examples of using import and export statements in the MDX docs.
Assigning Custom Components to HTML elements
Section titled Assigning Custom Components to HTML elementsWith MDX, you can map Markdown syntax to custom components instead of their standard HTML elements. This allows you to write in standard Markdown syntax, but apply special component styling to selected elements.
Import your custom component into your .mdx
file, then export a components
object that maps the standard HTML element to your custom component:
Visit the MDX website for a full list of HTML elements that can be overwritten as custom components.
Importing Markdown
Section titled Importing MarkdownYou can import Markdown and MDX files directly into your Astro files. This gives you access to their Markdown content, as well as other properties such as frontmatter values that can be used within Astro’s JSX-like expressions.
You can import one specific page with an import
statement, or multiple pages with Astro.glob()
.
When you import Markdown and MDX files in an Astro component, you get an object containing their exported properties.
In MDX files, you can access properties from both frontmatter and export
statements:
You can optionally provide a type for the frontmatter
variable using a TypeScript generic:
Exported Properties
Section titled Exported PropertiesSee the properties exported to an Astro layout component when using Astro’s special frontmatter layout.
The following properties are available to a .astro
component when using an import
statement or Astro.glob()
:
file
- The absolute file path (e.g./home/user/projects/.../file.md
).url
- If it’s a page, the URL of the page (e.g./en/guides/markdown-content
).frontmatter
- Contains any data specified in the file’s YAML frontmatter.getHeadings
- An async function that returns an array of all headings (i.e.h1 -> h6
elements) in the file. Each heading’sslug
corresponds to the generated ID for a given heading and can be used for anchor links. This list follows the type:{ depth: number; slug: string; text: string }[]
.Content
- A component that returns the full, rendered contents of the file.- (Markdown only)
rawContent()
- A function that returns the raw Markdown document as a string. - (Markdown only)
compiledContent()
- A function that returns the Markdown document compiled to an HTML string. Note this does not include layouts configured in your frontmatter! Only the markdown document itself will be returned as HTML. - (MDX only) - MDX files can also export data with an
export
statement.
The Content
Component
Section titled The Content ComponentImport Content
to render a component that returns the full rendered contents of a Markdown or MDX file:
Example: Dynamic page routing
Section titled Example: Dynamic page routingInstead of putting your Markdown/MDX files in the src/pages/
directory to create page routes, you can generate pages dynamically.
To access your Markdown content, pass the <Content/>
component through the Astro page’s props
. You can then retrieve the component from Astro.props
and render it in your page template.
MDX-only Exports
Section titled MDX-only ExportsMDX files can also export data with an export
statement.
For example, you can export a title
field from an MDX page or component.
This title
will be accessible from import
and Astro.glob() statements:
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.
Custom components defined and exported in an MDX file must be imported and then passed back to the <Content />
component via the components
property.
Configuring Markdown and MDX
Section titled Configuring Markdown and MDXMarkdown support in Astro is powered by remark, a powerful parsing and processing tool with an active ecosystem. Other Markdown parsers like Pandoc and markdown-it are not currently supported.
Astro applies the GitHub-flavored Markdown and SmartyPants plugins by default. This brings some niceties like generating clickable links from text, and formatting for quotations and em-dashes.
You can customize how remark parses your Markdown in astro.config.mjs
. See the full list of Markdown configuration options.
Markdown Plugins
Section titled Markdown PluginsAstro supports adding third-party remark and rehype plugins for Markdown and MDX. These plugins allow you to extend your Markdown with new capabilities, like auto-generating a table of contents, applying accessible emoji labels, and styling your Markdown.
We encourage you to browse awesome-remark and awesome-rehype for popular plugins! See each plugin’s own README for specific installation instructions.
This example applies remark-toc
and rehype-accessible-emojis
to both Markdown and MDX files:
Note that by default, remarkToc
requires a “ToC” or “Table of Contents” heading (case-insensitive) on the page to show the table of contents.
Heading IDs and plugins
Section titled Heading IDs and pluginsAstro injects an id
attribute into all heading elements (<h1>
to <h6>
) in Markdown and MDX files and provides a getHeadings()
utility for retrieving these IDs in Markdown exported properties.
You can customize these heading IDs by adding a rehype plugin that injects id
attributes (e.g. rehype-slug
). Your custom IDs, instead of Astro’s defaults, will be reflected in the HTML output and the items returned by getHeadings()
.
By default, Astro injects id
attributes after your rehype plugins have run. If one of your custom rehype plugins needs to access the IDs injected by Astro, you can import and use Astro’s rehypeHeadingIds
plugin directly. Be sure to add rehypeHeadingIds
before any plugins that rely on it:
Customizing a plugin
Section titled Customizing a pluginIn order to customize a plugin, provide an options object after it in a nested array.
The example below adds the heading option to the remarkToc
plugin to change where the table of contents is placed, and the behavior
option to the rehype-autolink-headings
plugin in order to add the anchor tag after the headline text.
Modifying frontmatter programmatically
Section titled Modifying frontmatter programmaticallyIf you are using content collections, please see “Modifying Frontmatter with Remark”.
You can add frontmatter properties to all of your Markdown and MDX files by using a remark or rehype plugin.
-
Append a
customProperty
to thedata.astro.frontmatter
property from your plugin’sfile
argument:जोड़ा गया: astro@2.0.0
data.astro.frontmatter
contains all properties from a given Markdown or MDX document. This allows you to modify existing frontmatter properties, or compute new properties from this existing frontmatter. -
Apply this plugin to your
markdown
ormdx
integration config:or
Now, every Markdown or MDX file will have customProperty
in its frontmatter, making it available when importing your markdown and from the Astro.props.frontmatter
property in your layouts.
Extending Markdown config from MDX
Section titled Extending Markdown config from MDXAstro’s MDX integration will extend your project’s existing Markdown configuration by default. To override individual options, you can specify their equivalent in your MDX configuration.
The following example disables GitHub-Flavored Markdown and applies a different set of remark plugins for MDX files:
To avoid extending your Markdown config from MDX, set the extendMarkdownConfig
option (enabled by default) to false
:
Syntax Highlighting
Section titled Syntax HighlightingAstro comes with built-in support for Shiki (via Shikiji) and Prism. This provides syntax highlighting for:
- all code fences (```) used in a Markdown or MDX file.
- content within the built-in
<Code />
component (powered by Shiki). - content within the
<Prism />
component (powered by Prism).
Shiki is enabled by default, preconfigured with the github-dark
theme. The compiled output will be limited to inline style
s without any extraneous CSS classes, stylesheets, or client-side JS.
Shiki configuration
Section titled Shiki configurationShiki is our default syntax highlighter. You can configure all options via the shikiConfig
object like so:
Light and Dark Mode
Section titled Light and Dark ModeTo make it reactive to your site’s theme, you need to add a short CSS snippet:
The class name used in shiki is
.shiki
, here you need to use.astro-code
.
Adding your own theme
Section titled Adding your own themeInstead of using one of Shiki’s predefined themes, you can import a custom theme from a local file.
We also suggest reading Shiki’s own theme documentation to explore more about themes, light vs dark mode toggles, or styling via CSS variables.
Change Default Syntax Highlighting Mode
Section titled Change Default Syntax Highlighting ModeIf you’d like to switch to 'prism'
by default, or disable syntax highlighting entirely, you can use the markdown.syntaxHighlighting
config object:
Prism configuration
Section titled Prism configurationIf you opt to use Prism, Astro will apply Prism’s CSS classes instead. Note that you need to bring your own CSS stylesheet for syntax highlighting to appear!
- Choose a premade stylesheet from the available Prism Themes.
- Add this stylesheet to your project’s
public/
directory. - Load this into your page’s
<head>
in a layout component via a<link>
tag. (See Prism basic usage.)
You can also visit the list of languages supported by Prism for options and usage.
Fetching Remote Markdown
Section titled Fetching Remote MarkdownAstro was primarily designed for local Markdown files that could be saved inside of your project directory. However, there may be certain cases where you need to fetch Markdown from a remote source. For example, you may need to fetch and render Markdown from a remote API when you build your website (or when a user makes a request to your website, when using SSR).
Astro does not include built-in support for remote Markdown! To fetch remote Markdown and render it to HTML, you will need to install and configure your own Markdown parser from npm. This will not inherit from any of Astro’s built-in Markdown and MDX settings that you have configured. Be sure that you understand these limitations before implementing this in your project.
Learn