Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Markdown component

3m:40s

By now, you know that Markdown helps us convert text into HTML very easily.

file.md
<!-- Markdown input -->
Example of **some** markdown _content_
- It's easy to write in markdown
- It'll be transformed into HTML
<!-- HTML output -->
<p>Example of <strong> some </strong> markdown <em>content</em></p>
<ul>
<li>It’s easy to write in markdown</li>
<li>It’ll be transformed into HTML</li>
</ul>

So it goes without saying that a Markdown component would help us write less markup — we can omit h1-h6 tags, p, ul, li tags, and even strong and em!

It would be a godsend if Astro has this!

Astro had a Markdown component

Astro did have a Markdown component. But they deprecated it in Version 1 and completely removed it in Version 3 😢.

I was upset about that, but I decided to build a Markdown component for myself since I could see the benefit of having one.

Using Markdown in Astro

You can import the Markdown component I’ve built and use it directly.

index.astro
---
import { Markdown } from '@splendidlabz/astro'
---
<Markdown>
Example of **some** markdown _content_
- It's easy to write in markdown
- It'll be transformed into HTML
</Markdown>

This component respects indentation. It uses the first indentation of the first line of the Markdown text as a gauge — so you don’t have to worry about four spaces being converted into a <pre><code> block.

input.astro
<div>
<div>
<!-- prettier-ignore -->
<Markdown>
This is a paragraph
This is a second paragraph
</Markdown>
</div>
</div>
output.html
<div>
<div>
<p>This is a paragraph</p>
<p>This is a second paragraph</p>
</div>
</div>

Notice the <!--prettier-ignore --> comment?

I used it because Prettier doesn’t know the content contained within the Markdown component is markdown content. It’s a limitation of using this component.

An inline option

By default, Markdown will generate paragraph tags. But sometimes we do not want that, for example if we use it inside a h2. To prevent the markdown component from generating paragraph text, we can use the inline option.

input.astro
<h2 class="max-w-[12em]">
<Markdown inline> Ain't this cool? </Markdown>
</h2>
output.html
<h2 class="max-w-[12em]">Ain’t this cool?</h2>

Gotchas and Caveats

Unicode characters

This is a known limitation. Prettier messes up the <!-- prettier-ignore --> block if you have Unicode characters like emojis and em dashes before the block.

Here’s an example of how it screws up:

Before Saving
<!-- prettier-ignore -->
<Markdown>
Markdown block that contains Unicode characters 🤗
</Markdown>
<!-- prettier-ignore -->
<Markdown>
Second Markdown block.
</Markdown>
After saving
<!-- prettier-ignore -->
<Markdown>
Markdown block that contains unicode characters 🤗
</Markdown>
<!-- prettier-ignore -->
<arkdown>
Second Markdown block.
</

Unfortunately, we can’t do much about this limitation — other than using a separate md or mdx file.

HTML tags inside backticks

Astro parses the contents within a <Markdown> slot before the component runs, so it will try to trigger a real HTML element when you write backticks for tags like <video> or even <Markdown>.

The best way to handle this sort of content, is to pass the inforamtio into the content prop instead.

input.astro
---
import { Markdown } from '@splendidlabz/astro'
---
<Markdown content={'`<video>` works perfectly fine here.'} />

This also gets you out of the <!-- prettier-ignore --> dance, since the markdown is a plain string.

Setting compressHTML in Astro v7

Astro changed the default behavior of their HTML compression engine to jsx in version 7.

For the Markdown component to work, we need to set the compression engine back to true to preserve standard HTML compression rules.

astro.config.mjs
export default defineConfig({
// Astro 7 changed the default to JSX-style whitespace handling (inline
// elements can lose the space between them). This keeps pre-v7 behavior.
compressHTML: true,
})

Code snippets to copy paste

Markdown

---
import { Markdown } from '@splendidlabz/astro'
---
<!-- prettier-ignore-->
<Markdown>
Content goes here
</Markdown>

Markdown (inlined)

---
import { Markdown } from '@splendidlabz/astro'
---
<h2>
<Markdown inline> Content goes here </Markdown>
</h2>

Markdown with the content prop

---
import { Markdown } from '@splendidlabz/astro'
---
<!-- prettier-ignore-->
<Markdown content="Content goes here" />