Mastering Content Collections in Astro

July 28, 2025
#astro #content #markdown #guide

Managing content in Astro using the @astro/content module is a game-changer for structured sites.

πŸ” Overview

Astro provides a powerful and type-safe way to manage your content with:

  • Collections
  • Schemas
  • Markdown/MDX parsing
  • Static typing (Zod)

πŸ› οΈ Getting Started

To set up content collections:

// src/content/config.ts
import { defineCollection, z } from "astro:content";

export const collections = {
  blog: defineCollection({
    type: "content",
    schema: z.object({
      title: z.string(),
      slug: z.string(),
      description: z.string(),
      tags: z.array(z.string()),
      pubDate: z.date(),
    }),
  }),
};

βœ… Features

  1. Schema Validation Ensures consistent metadata across posts.

  2. Type Inference IntelliSense in your IDE.

  3. Custom Fields You can extend the schema to support authors, reading time, or cover images.


πŸ“Έ Embedding Images

You can use standard markdown syntax:

Astro Logo


πŸ“Š Tables

FeatureStatus
MDX Supportβœ…
Schema Typedβœ…
RSS Integrationβœ…

🧠 Bonus Tips

Always define a unique slug to prevent route conflicts.


πŸš€ Deploying Your Content

Once everything is set up:

npm run build && npm run preview

You’ll now have statically generated pages based on your Markdown.


πŸ”— Resources

My funny logo

0xws

Welcome !

πŸ‘‹ I’m ws (/vaΙͺs/), a DevSecOps engineer. This site is the place to gather my projects and thoughts on everything I like. From IT to anything related to cars and / or electronics.

I’m mostly working with Typescript, Python, Rust and Zig as daily drivers. These help me to perform my tasks in full-stack web development, system development, gamedev and plenty of other tasks.

On the infrastructure side, I’m mostly using Terraform/OpenTofu, K8s and NixOS.





HackTheBox Badge TryHackMe Badge Contact Me

πŸ™‹ Father of useless web apps. I like programming, cybersecurity and electronics.

πŸ§‘β€πŸ’» Aiming at becoming a Cloud security architect. I work with Typescript, Python and Rust.

Otherwise, I like race cars. β˜•

PGP: DDF4 D46C DAAA 62C0 D71B 24EE 84E0 D9E9 1805 69EA