Web Development

How to Supercharge Your Astro Site with a Custom Markdown Component

2026-05-02 01:27:04

Introduction

Working with Markdown inside Astro components can save you time and reduce clutter. Instead of wrapping each line in <p>, <strong>, or <em>, you can use a dedicated Markdown component that does the heavy lifting for you. This guide will show you how to install, configure, and use a Markdown component in your Astro project, so you can focus on writing clean content while still getting proper HTML output. We’ll also cover the inline option and how to keep Prettier from messing up your syntax.

How to Supercharge Your Astro Site with a Custom Markdown Component
Source: css-tricks.com

What You Need

Step-by-Step Instructions

Step 1: Understand What a Markdown Component Does

In Astro, you normally write HTML or JSX to render content. A Markdown component lets you drop raw Markdown inside your component template and have it automatically transformed into proper HTML. This means you can write headings, paragraphs, lists, links, and inline formatting without manually typing opening and closing tags. The component also converts typographic symbols (like straight apostrophes or quotes) into their curly equivalents automatically.

Originally, Astro had a built-in <Markdown> component, but it was moved to an external package in version 1 and removed entirely in version 3. The community-maintained @splendidlabz/astro package provides a drop-in replacement.

Step 2: Install the Markdown Component Package

Open your terminal, navigate to your Astro project root, and install the package using your preferred package manager:

npm install @splendidlabz/astro

or

yarn add @splendidlabz/astro

Once installed, the <Markdown> component will be available for import.

Step 3: Import and Use the Component

In any Astro component (.astro file), add the import at the top of the frontmatter:

---
import { Markdown } from '@splendidlabz/astro';
---

Then use it anywhere in your template. Surround your Markdown content with the opening and closing tags:

<div class="card">
  <Markdown>
    ## Card Title
    This is a paragraph with **strong** and *italic* text.
    Here's a [link](https://example.com).

    - Item 1
    - Item 2
  </Markdown>
</div>

The component will output standard HTML tags, making your content semantic and accessible without extra markup.

Step 4: Take Advantage of Automatic Indentation Handling

One of the best features is that the component respects the indentation of your Markdown. You can nest it inside deeply indented structures without worrying about unwanted <pre> or <code> tags. For example:

<section>
  <article>
    <Markdown>
      This is the first paragraph.

      This is the second paragraph.
    </Markdown>
  </article>
</section>

The output will be clean, with each paragraph wrapped in <p> tags and no unwanted extra indentation.

Step 5: Use the Inline Option for Short Snippets

Sometimes you only need to apply Markdown inline formatting — like bold or italic — inside a heading or a single element. Use the inline prop to suppress the automatic paragraph wrapping:

<h2 class="max-w-2xl">
  <Markdown inline> Ain't this cool? </Markdown>
</h2>

This outputs:

<h2 class="max-w-2xl">
  Ain't this cool?
</h2>

Notice that the apostrophe was converted to a curly right single quote automatically.

Step 6: Prevent Prettier from Ruining Your Markdown

Prettier, a popular code formatter, may try to reformat the contents inside the <Markdown> block, which can break intentional spacing or indentation. To avoid this, add an HTML comment <!-- prettier-ignore --> just before the opening <Markdown> tag:

<div>
  <!-- prettier-ignore -->
  <Markdown>
    This will stay exactly as I wrote it.

    - Not
    - Reformatted
  </Markdown>
</div>

This tells Prettier to skip formatting that block, preserving your Markdown structure.

Tips & Best Practices

Explore

Everything About Introducing Anthropic’s Claude Opus 4.7 model in Amazon Be... From Historical Drama to Futuristic Epic: The Transformation of For All Mankind Rivian Trims Georgia EV Factory Plans After DOE Cuts Loan to $4.5 Billion A Look at EtherRAT Distribution Spoofing Administrative Tools via GitHub Facades How to Evaluate AI Chatbot Accuracy: The Strawberry Letter Test and Beyond