By now, you know that Markdown helps us convert text into HTML very easily.
<!-- 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.
---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.
<div> <div> <!-- prettier-ignore --> <Markdown> This is a paragraph
This is a second paragraph </Markdown> </div></div><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.
<h2 class="max-w-[12em]"> <Markdown inline> Ain't this cool? </Markdown></h2><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:
<!-- prettier-ignore --><Markdown> Markdown block that contains Unicode characters 🤗</Markdown>
<!-- prettier-ignore --><Markdown> Second Markdown block.</Markdown><!-- 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.
---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.
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" />Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson