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 layouts

1m:22s

Markdown layouts are the third way to use Markdown. It works with both md and mdx.

The best use of this is to create heavy content sites that use the same template.

For example, I have multiple pages that use the same one-column content. Things like this can include a 404 page and a contact page, or even others!

404 page
Contact page

Simple markdown layout

To let this page use the layout, I first create a common layout. I call it OneColumn in this case.

Then I add OneColumn as the layout property in both 404.md and contact.md

404.md
---
layout: '@/layouts/OneColumn.astro'
title: Page Not Found 😢
---
<!-- Content goes here -->
Contact.md
---
layout: '@/layouts/OneColumn.astro'
title: Contact
---
<!-- Content goes here -->

In OneColumn.astro:

  • We can access the frontmatter via the content prop.
  • The markdown content goes into the default slot.

So the simplest version of a markdown layout file looks like this:

OneColumn.astro
---
const props = Astro.props.content
---
<h1>{props.title}</h1>
<slot />

That’s all you need to use a markdown layout.

Hybrid layout

It doesn’t make sense to only create a markdown layout file, because we can make it into a hybrid one that accepts Astro pages very easily.

All we have to do is just let props fall back to Astro.props if the content prop is not present.

OneColumn.astro
---
// Accepts .astro, .md, and .mdx files
const props = Astro.props.content || Astro.props
---
<h1>{props.title}</h1>
<slot />

That’s it!