0
0
AstroHow-ToBeginner ยท 4 min read

How to Render Markdown Content in Astro: Simple Guide

In Astro, you can render markdown content by importing .md files directly as components or using the Markdown component from @astrojs/markdown-remark. This lets you include markdown content in your pages easily with automatic HTML conversion.
๐Ÿ“

Syntax

Astro supports importing markdown files as components or using markdown rendering components. You can import a markdown file like a component with import Post from './post.md' and then use it in your template. Alternatively, use the Markdown component to render markdown strings.

Key parts:

  • import Post from './file.md': imports markdown as a component
  • <Post />: renders the markdown content as HTML
  • <Markdown>...</Markdown>: renders markdown from a string
astro
---
import Post from './post.md';
---

<Post />
Output
Renders the HTML content converted from the markdown file 'post.md'.
๐Ÿ’ป

Example

This example shows how to import a markdown file and render it inside an Astro page. The markdown file contains simple content, and Astro converts it to HTML automatically.

astro
---
import Content from '../content/example.md';
---

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Markdown in Astro</title>
  </head>
  <body>
    <main>
      <h1>Markdown Content Rendered</h1>
      <Content />
    </main>
  </body>
</html>
Output
<h1>Markdown Content Rendered</h1> <h2>Welcome to Astro</h2> <p>This is a paragraph from markdown.</p>
โš ๏ธ

Common Pitfalls

Common mistakes when rendering markdown in Astro include:

  • Not installing or configuring the markdown integration like @astrojs/markdown-remark.
  • Trying to render markdown as plain text without importing it as a component.
  • Forgetting to use the --- frontmatter separator in markdown files, which Astro requires.

Always ensure your markdown files have valid frontmatter and that your Astro config includes markdown support.

astro
/* Wrong: Trying to render markdown as plain text */
---
const mdContent = '# Hello World';
---

<div>{mdContent}</div>

/* Right: Use Markdown component */
---
import { Markdown } from '@astrojs/markdown-remark';
const mdContent = '# Hello World';
---

<Markdown>{mdContent}</Markdown>
Output
<div># Hello World</div> <h1>Hello World</h1>
๐Ÿ“Š

Quick Reference

Tips for rendering markdown in Astro:

  • Import markdown files directly as components.
  • Use <Markdown> component for markdown strings.
  • Ensure markdown files have frontmatter (even empty) to be processed.
  • Install @astrojs/markdown-remark integration for advanced features.
โœ…

Key Takeaways

Import markdown files as components to render them easily in Astro.
Use the Markdown component to render markdown strings dynamically.
Always include frontmatter in markdown files for Astro to process them.
Install and configure markdown integrations like @astrojs/markdown-remark for best results.