Markdown Files
Markdown is commonly used to author text-heavy content like blog posts and documentation. Astro supports standard Markdown files (.md) with GitHub-flavored Markdown and SmartyPants plugins applied by default.
Organizing Files
Local Markdown files can be kept anywhere within yoursrc/ directory. Files in src/pages/ will automatically become pages on your site, while files elsewhere can be imported or queried using content collections.
Using Frontmatter
Markdown files support YAML frontmatter to define metadata:Importing Markdown
You can import Markdown files directly in.astro files using ESM import syntax:
Available Properties
When importing Markdown, you have access to these properties:frontmatter- Contains all YAML frontmatter datafile- The absolute file pathurl- The URL of the page (if insrc/pages/)Content- A component that renders the full contentrawContent()- Returns the raw Markdown stringcompiledContent()- Returns the compiled HTML stringgetHeadings()- Returns an array of all headings
MDX Integration
For additional functionality like using components inside Markdown, install the MDX integration:.mdx) allow you to import and use Astro components alongside Markdown syntax:
Markdown Plugins
Astro uses remark and rehype plugins to extend Markdown functionality. You can add plugins in yourastro.config.mjs:
Customizing Plugins
You can pass options to plugins using nested arrays:Modifying Frontmatter Programmatically
You can add frontmatter properties to all Markdown files using a plugin:-
Create a plugin that modifies
file.data.astro.frontmatter: -
Apply the plugin to your config:
Heading IDs
Markdown headings automatically get anchor links usinggithub-slugger:
rehypeHeadingIds plugin.
Markdown Pages
Files insrc/pages/ automatically become pages. Astro will render the Markdown to HTML at that route.
Layout Property
Use the speciallayout frontmatter property to wrap your Markdown in a layout:
Astro.props.frontmatter:
Remote Markdown
Astro’s built-in Markdown support doesn’t process remote Markdown. For remote content, use:- A content collections loader with
renderMarkdown()function - Your own Markdown parser from npm (like
marked)