Astro provides powerful content management capabilities through Content Collections, allowing you to organize, validate, and query your content with full TypeScript support.
Content Collections are the recommended way to manage content in Astro. They provide type-safety, validation, and optimized queries for your Markdown, MDX, and data files.
---title: 'My First Blog Post'description: 'This is my first post on my new Astro blog.'pubDate: 2024-01-15author: 'Jane Doe'tags: ['astro', 'blogging', 'web development']---This is the content of my blog post...
import { defineCollection, z } from 'astro:content';import { glob } from 'astro/loaders';const blog = defineCollection({ loader: glob({ base: './src/content/blog', pattern: '**/*.md' }), schema: z.object({ title: z.string().min(10, 'Title must be at least 10 characters'), draft: z.boolean().default(false), pubDate: z.coerce.date(), }).refine( (data) => data.draft || data.pubDate <= new Date(), { message: 'Published posts must have a past publish date' } ),});export const collections = { blog };
---import { getCollection } from 'astro:content';// Get all published postsconst allPosts = await getCollection('blog', ({ data }) => { return data.draft !== true;});// Sort by date (newest first)const sortedPosts = allPosts.sort( (a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());// Filter by tagconst tagFilter = 'astro';const filteredPosts = allPosts.filter((post) => post.data.tags?.includes(tagFilter));// Get only the 5 most recentconst recentPosts = sortedPosts.slice(0, 5);---
---title: 'Using MDX'pubDate: 2024-01-20---import CustomComponent from '../../components/CustomComponent.astro';import { Card } from '../../components/Card.jsx';# Hello from MDX!<CustomComponent message="This is an Astro component" /><Card title="Interactive Card"> This card component works inside my MDX content!</Card>