
content-collections
by vanman2024
SKILL.md
name: content-collections description: Astro content collections setup, type-safe schemas, query patterns, and frontmatter validation. Use when building Astro sites, setting up content collections, creating collection schemas, querying content, validating frontmatter, or when user mentions Astro collections, content management, MDX content, type-safe content, or collection queries. allowed-tools: - Read
- Write
- Edit
- Bash
- Glob
- Grep
Content Collections Management for Astro
Complete content collections setup and management for Astro projects, including type-safe schemas, query patterns, frontmatter validation, and content organization.
Overview
This skill provides comprehensive support for Astro content collections:
- Schema definition with Zod validation
- Type-safe query patterns
- Frontmatter validation and testing
- Collection setup and configuration
- Content organization best practices
Instructions
1. Initial Setup
Run the setup script to initialize content collections in an Astro project:
bash scripts/setup-content-collections.sh [project-path]
This script:
- Creates
src/content/config.tsif not exists - Sets up collection directories
- Adds TypeScript types generation
- Configures content collection imports
2. Define Collection Schemas
Use schema templates to define type-safe collection schemas:
TypeScript Schema (Recommended):
# Read template for reference
Read: templates/schemas/blog-collection-schema.ts
Read: templates/schemas/docs-collection-schema.ts
Python Schema (for build scripts):
# For Python-based content generation
Read: templates/python/collection-schema.py
3. Generate TypeScript Types
Generate TypeScript types from your collection schemas:
bash scripts/generate-types.sh [project-path]
This creates:
src/content/config.tsexports- Type definitions in
.astro/types.d.ts - Auto-completion for collection queries
4. Query Content Collections
Use query builder patterns for type-safe content retrieval:
TypeScript Queries:
Read: templates/queries/basic-queries.ts
Read: templates/queries/advanced-queries.ts
Read: templates/queries/filtered-queries.ts
Python Queries (for build scripts):
Read: templates/python/query-patterns.py
5. Validate Frontmatter
Validate content frontmatter against schemas:
bash scripts/validate-frontmatter.sh [collection-name] [content-path]
This script:
- Checks frontmatter against schema
- Reports validation errors
- Suggests fixes for common issues
6. Build Query Patterns
Generate optimized query patterns:
bash scripts/query-builder.sh [collection-name] [query-type]
Query types:
all- Get all entriesfiltered- Filter by frontmatter fieldssorted- Sort by date/title/custom fieldpaginated- Paginate resultsrelated- Find related content
7. Test Collections
Run comprehensive collection tests:
bash scripts/test-collections.sh [project-path]
Tests include:
- Schema validation
- Type checking
- Query performance
- Frontmatter completeness
Scripts Reference
All scripts located in scripts/:
- setup-content-collections.sh - Initialize content collections structure
- validate-frontmatter.sh - Validate content against schemas
- generate-types.sh - Generate TypeScript types from schemas
- query-builder.sh - Build optimized query patterns
- test-collections.sh - Run comprehensive collection tests
Templates Reference
TypeScript Templates (templates/typescript/)
- blog-collection.ts - Blog post collection schema
- docs-collection.ts - Documentation collection schema
- basic-queries.ts - Common query patterns
- advanced-queries.ts - Complex filtering and sorting
- paginated-queries.ts - Pagination patterns
- related-content.ts - Related content finding
Python Templates (templates/python/)
- collection-schema.py - Python schema definitions
- query-patterns.py - Python query builders
- frontmatter-validator.py - Validation utilities
Schema Templates (templates/schemas/)
- blog-collection-schema.ts - Blog post schema with Zod
- docs-collection-schema.ts - Documentation schema
- product-collection-schema.ts - E-commerce product schema
- author-collection-schema.ts - Author profile schema
Query Templates (templates/queries/)
- basic-queries.ts - getAllEntries, getEntryBySlug
- advanced-queries.ts - Complex filtering
- filtered-queries.ts - Category, tag, date filtering
- sorted-queries.ts - Sorting patterns
- paginated-queries.ts - Pagination with prev/next
- related-queries.ts - Related content algorithms
Validation Templates (templates/validation/)
- schema-validator.ts - Zod schema validation
- frontmatter-checker.ts - Frontmatter completeness
- type-checker.ts - TypeScript type validation
Examples
See comprehensive examples in examples/:
- basic-usage.md - Getting started with content collections
- advanced-usage.md - Complex schemas and queries
- common-patterns.md - Typical content collection patterns
- error-handling.md - Common errors and solutions
- integration.md - Integration with MDX, images, and components
Common Patterns
Blog Collection Schema
import { defineCollection, z } from 'astro:content';
const blogCollection = defineCollection({
schema: z.object({
title: z.string()
description: z.string()
pubDate: z.date()
updatedDate: z.date().optional()
heroImage: z.string().optional()
tags: z.array(z.string()).default([])
author: z.string()
draft: z.boolean().default(false)
})
});
export const collections = { blog: blogCollection };
Type-Safe Queries
import { getCollection, getEntry } from 'astro:content';
// Get all published blog posts
const posts = await getCollection('blog', ({ data }) => {
return data.draft !== true;
});
// Get single post by slug
const post = await getEntry('blog', 'my-post-slug');
// Sort by date descending
const sortedPosts = posts.sort((a, b) =>
b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
Frontmatter Validation
import { z } from 'zod';
const blogSchema = z.object({
title: z.string().min(1, "Title required")
description: z.string().max(160, "Description too long")
pubDate: z.date()
});
// Validate frontmatter
const result = blogSchema.safeParse(frontmatter);
if (!result.success) {
console.error(result.error.format());
}
Requirements
- Astro 3.0+ or 4.0+ (content collections support)
- Node.js 18+
- TypeScript 5.0+ (for type generation)
- Zod 3.0+ (for schema validation)
- Python 3.8+ (optional, for Python templates)
Best Practices
-
Schema Design:
- Use specific types (date, enum) over strings
- Add descriptions for better auto-completion
- Set sensible defaults for optional fields
- Use unions for variant content types
-
Query Optimization:
- Filter in getCollection() not after
- Use getEntry() for single items
- Cache results when possible
- Minimize data fetched per query
-
Frontmatter Validation:
- Validate during build time
- Provide clear error messages
- Test with invalid data
- Document required fields
-
Content Organization:
- Group by collection type
- Use consistent slug patterns
- Separate drafts with boolean flag
- Version control all content
Troubleshooting
Common issues and solutions documented in examples/error-handling.md:
- Schema validation errors
- Type generation failures
- Query performance issues
- Frontmatter parsing errors
- Content not appearing
Related Skills
mdx-integration- MDX component usage in contentimage-optimization- Image handling in contentseo-optimization- SEO for content collectionsstatic-generation- Static site generation patterns
Skill Version: 1.0.0 Last Updated: 2025-10-28 Plugin: website-builder
スコア
総合スコア
リポジトリの品質指標に基づく評価
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
レビュー
レビュー機能は近日公開予定です