Expand description
Typed content collection API — get_collection / get_entry (issue #456).
Typed content collection API (issue #456).
Mirrors the ergonomics of Astro’s getCollection / getEntry
and Eleventy’s collection helpers, but with compile-time type
safety via serde. Authors define a struct that derives
serde::Deserialize, then load every Markdown file under a
directory as Vec<Entry<T>> with one call.
§Quick start
use serde::Deserialize;
use ssg::collections::{get_collection, Entry};
#[derive(Debug, Deserialize)]
struct BlogPost {
title: String,
date: String,
description: Option<String>,
#[serde(default)]
tags: Vec<String>,
}
let posts: Vec<Entry<BlogPost>> =
get_collection("content/blog")?;
for post in posts {
println!("{} ({})", post.data.title, post.slug);
}§Why typed?
Hand-rolling frontmatter access via serde_yml::Value or string
lookups produces stringly-typed code that fails at runtime when a
field is renamed or its type changes. The typed API surfaces the
mismatch as a compile error or a clean Result::Err at load
time, with the file path in the error chain.
§Loading semantics
- Walks recursively under the given directory.
- Markdown only (
.md,.markdown). Other files are skipped. - Skips files without frontmatter silently — they’re treated as plain pages outside the collection.
- Returns parse errors with context: each error carries the absolute path of the file that failed.
- Slug derivation: the slug is the file’s
stem(filename without extension).index.mdfiles in subdirectories use the subdirectory name as the slug. - Deterministic ordering: entries are returned sorted by slug so consumers that hash the result (e.g. for golden tests or perf benchmarks) get stable output.
§Single-entry access
[get_entry] loads exactly one file by slug, returning
Ok(None) if no matching .md is found. Use this when a page
references another by its known slug (sidebar layouts, related
posts).
Structs§
- Entry
- One parsed entry from a content collection.
Functions§
- get_
collection - Loads every Markdown file under
dirwhose frontmatter matchesT. Returns entries sorted by slug. - get_
entry - Loads a single entry from
dirwhose slug matchesslug.