mdbook-content-loader
Zero-runtime-fetch content collections for mdBook
Injects your content-collections.json (generated by
mdbook-content-collections)
directly into every HTML page as window.CONTENT_COLLECTIONS — no network
request, no path hacks, works offline.
By default, the data is injected on your landing page (index.md), which is
usually where your “Latest posts” or home layout lives. You can optionally
enable the old behavior and inject it on every page via a config flag.
This gives you true Astro/Zola-style content collections inside mdBook: instant, reliable access to posts, blog entries, notes, changelogs — anywhere in your theme.
Why this exists (and why you want it)
The original mdbook-content-collections example used
fetch('content-collections.json') in the browser.
That works… until it doesn’t:
| Problem | With fetch() |
With mdbook-content-loader |
|---|---|---|
| Extra HTTP request | Yes | No |
Broken in file:// / offline mode |
Yes | Works perfectly |
| Fragile URL construction | Yes | No path logic needed |
| Flash of empty content | Yes | Instant |
| Fails silently on 404 | Yes | Build fails early |
| Works on GitHub Pages / custom domains | Sometimes | Always |
This crate fixes all of that — permanently.
What you get
A single global, available immediately on every page:
window.CONTENT_COLLECTIONS = ;
Each entry has the full shape from mdbook-content-collections:
{
path: "blog/my-post.md",
title: "My Great Post",
date: "2025-11-27T00:00:00Z",
author?: string,
description?: string,
collection?: string,
tags: string[],
draft: false,
preview_html: "<p>First paragraph <strong>rendered</strong>...</p>"
}
-
Drafts are filtered out automatically
-
Everything is sorted newest -> oldest
-
No runtime parsing, fetching, or error handling needed
Installation
# If not installed, install mdbook-content-collections
# cargo install mdbook-content-collections
Setup
Add the following to your book.toml:
[]
# Generates content-collections.json
[]
= "mdbook-content-loader"
= ["content-collections"] # make sure JSON is generated first
# Behavior:
# - default: inject window.CONTENT_COLLECTIONS only into index.md
# - set inject_all = true to inject into every chapter
# inject_all = true
The after key ensures the JSON exists before this loader runs, and
inject_all is an optional boolean that restores the old “every page” behavior.
mdBook exposes this table under [preprocessor.content-loader] to the
preprocessor via PreprocessorContext, which is where this flag is read.
Usage in your theme
Drop this anywhere in theme/index.hbs (or any .hbs file):
Want a sidebar? Tag cloud? Search index? Related posts? Just read from
window.CONTENT_COLLECTIONS.
Common patterns
// Latest 5 blog posts
window.CONTENT_COLLECTIONS..?.;
// All posts (any collection)
window.CONTENT_COLLECTIONS.;
// Posts with tag "rust"
window.CONTENT_COLLECTIONS..;
Data Shape
Same as mdbook-content-collections, plus:
-
collections: grouped + sorted by collection name -
generated_at: when the data was build -
All
draft: trueentries removed
See: mdbook-content-collections
License
Apache 2.0