rules_mdbook
Bazel rules for mdbook with mdbook-mermaid plugin support. Hermetic, sha256-pinned binaries; mdbook_book rule produces a packaged HTML tarball.
| Latest | 0.3.1 |
|---|---|
| Versions | 4 |
| Category | Bazel rules |
| Compat level | 1 |
| Maintainers | Matt Marshall |
| Registry | https://registry.tbzl.dev/modules/rules_mdbook/ |
| Source | github.com/tomato-bazel/rules_mdbook |
bazel_dep(name = "rules_mdbook", version = "0.3.1")
View source & releases on GitHub ↗
Bazel rules for mdbook. Fetches the
prebuilt mdbook binary (plus mdbook-mermaid) and orchestrates a hermetic
mdbook build from a Bazel-managed sandbox.
-
module extension:
mdbook— auto-creates@mdbook+@mdbook_mermaidexternal repos. See docs/extensions.md. -
toolchain:
mdbook_toolchain— wraps the mdbook binary; resolved via@rules_mdbook//mdbook:toolchain_type. See docs/toolchains.md. -
rules:
mdbook_book— runsmdbook buildover a staged source tree, packages HTML as.tar.gz.mdbook_serve—bazel run //path:serve→mdbook servewith watch + live reload over the live source tree.
See docs/defs.md.
Install
Add the registry to your .bazelrc:
common --registry=https://registry.fastverk.com/
common --registry=https://bcr.bazel.build/
In your MODULE.bazel:
bazel_dep(name = "rules_mdbook", version = "0.2.0")
mdbook = use_extension("@rules_mdbook//mdbook:extensions.bzl", "mdbook")
use_repo(mdbook, "mdbook", "mdbook_mermaid")
register_toolchains("@mdbook//:mdbook_toolchain_def")
Override versions if needed:
mdbook.toolchain(
mdbook_version = "0.5.2",
mermaid_version = "0.17.0",
)
Quick start
Standard mdbook layout in your repo:
docs/
├── BUILD.bazel
├── book.toml
└── src/
├── SUMMARY.md
├── intro.md
└── ...
In docs/BUILD.bazel:
load("@rules_mdbook//mdbook:defs.bzl", "mdbook_book")
mdbook_book(
name = "site",
book_toml = "book.toml",
srcs = glob(["src/**/*.md"]),
plugins = ["@mdbook_mermaid//:mdbook-mermaid"],
out = "site.tar.gz",
)
bazel build //docs:site produces bazel-bin/docs/site.tar.gz containing
the rendered HTML.
How it works
mdbook_book:
- Stages
book.toml+ every src file into a sandbox dir at their package-relative paths (minussrc_strip_prefix). - Copies the mdbook binary and each plugin into the sandbox’s
bin/under their bare filenames (so mdbook can resolve plugins by name onPATH). - Runs
mdbook buildfrom the sandbox root. - Tars
book/html/(orbook/, whichever the mdbook backend wrote) into the declaredout.
The MdbookSiteInfo provider also returned by the rule wraps the
tarball file — downstream rules (a mdbook_serve wrapper, a deploy
target, link-check gates) can consume sites programmatically without
re-running mdbook.
Hermeticity
| Layer | Pinned by |
|---|---|
| mdbook binary | sha256 in mdbook/private/known_versions.bzl per (version, platform) |
| mermaid plugin | same table |
| Source tree | Bazel’s normal file-tracking (srcs label_list) |
Unpinned versions download unverified (warning emitted). Add an entry
to known_versions.bzl to lock a new version — compute with
curl -fsSL <url> | shasum -a 256.
Scope and non-goals
This module intentionally stays small. It provides the generally
reusable piece — fetching mdbook + plugins + running mdbook build
hermetically. Project-specific bits stay in your repo:
- Custom mdbook preprocessors (RFC autolinking, frontmatter stripping, …)
— keep them as your own
cc_binary/rust_binary/sh_binaryand pass them via thepluginsattr. - Source-tree staging that reorganizes a non-standard layout into what
book.tomlexpects — keep as a project-local script invoked beforemdbook_book. - Linkcheck — pass
mdbook-linkcheck(ormdbook-linkcheck2) viapluginslike any other plugin.
Compatibility
- Bazel: 7.4+, bzlmod required.
- mdbook: 0.5.2 pinned by default. Bump via
known_versions.bzl. - Platforms:
darwin_aarch64,darwin_x86_64,linux_x86_64,windows_x86_64(per upstream release coverage).
Contributing
Reference docs (docs/defs.md, docs/extensions.md) are stardoc-generated
from the .bzl docstrings and committed to source. After editing a rule
docstring:
bazel run //docs:update
CI gates this via bazel test //docs/... (diff_test against the
committed output) and the smoke build in examples/smoke/.
License
MIT.
Usage#
Real usage, taken from the module’s examples/.
examples/smoke/BUILD.bazel
load("@rules_mdbook//mdbook:defs.bzl", "mdbook_book", "mdbook_serve")
mdbook_book(
name = "site",
srcs = glob(["src/**/*.md"]),
out = "site.tar.gz",
book_toml = "book.toml",
plugins = ["@mdbook_mermaid//:mdbook-mermaid"],
)
# `bazel run //examples/smoke:serve` -> mdbook serve at localhost:3000
# with watch + live reload over the live source tree.
mdbook_serve(
name = "serve",
plugins = ["@mdbook_mermaid//:mdbook-mermaid"],
)
examples/treesrc/BUILD.bazel
load("@rules_mdbook//mdbook:defs.bzl", "mdbook_book")
load(":gen_tree.bzl", "gen_tree")
# Upstream generator: emits a directory of chapters whose filenames aren't
# known statically. The dir is named "chapters" so it stages at $STAGE/chapters,
# matching book.toml's `src = "chapters"`.
gen_tree(
name = "chapters",
)
# mdbook_book consumes the directory directly (recursive copy) alongside the
# committed book.toml.
mdbook_book(
name = "site",
srcs = [":chapters"],
out = "site.tar.gz",
book_toml = "book.toml",
)Rules & providers#
Generated with Stardoc from the module's .bzl sources.
from docs/defs.md
User-facing Bazel rules for rules_mdbook.
Exports mdbook_book, which runs mdbook build over a staged source
tree and packages the rendered HTML into a tarball. Optional plugin
executables (e.g. mdbook-mermaid) are staged onto PATH so mdbook can
resolve them by their bare names.
Targets returning MdbookSiteInfo expose the site tarball
programmatically so future rules (a deploy step, a link checker, a
mdbook serve wrapper) can consume the output without re-running
mdbook.
mdbook_book
load("@rules_mdbook//mdbook:defs.bzl", "mdbook_book")
mdbook_book(name, srcs, out, book_toml, plugins, src_strip_prefix)
Run mdbook build over a staged source tree and produce an HTML tarball.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| srcs | All source files (Markdown, SUMMARY.md, theme assets, etc.). Each file is staged at its package-relative path minus src_strip_prefix. A directory (tree artifact produced by an upstream rule) is copied recursively into its computed relative path, so a rule that stages a generated chapter tree can feed it here directly. | List of labels | required | |
| out | The rendered site, packaged as a .tar.gz. | Label | required | |
| book_toml | The mdbook configuration file. Staged at the root of the build sandbox. | Label | required | |
| plugins | mdbook plugin executables (e.g. @mdbook_mermaid//:mdbook-mermaid). Staged onto PATH so mdbook can resolve them by bare name. | List of labels | optional | [] |
| src_strip_prefix | Prefix to strip from each src’s package-relative path before staging. Empty means files land at their package-relative paths. | String | optional | "" |
mdbook_serve
load("@rules_mdbook//mdbook:defs.bzl", "mdbook_serve")
mdbook_serve(name, plugins)
Run mdbook serve (with watch + live reload) against the live user source tree under $BUILD_WORKSPACE_DIRECTORY/<package>. Invoke via bazel run //path/to:target. The target’s package directory must contain the book.toml; mdbook’s own watch picks up edits without Bazel re-running.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| plugins | mdbook plugin executables, staged onto PATH so mdbook resolves them by bare name. Match the plugins listed in your book.toml. | List of labels | optional | [] |
MdbookSiteInfo
load("@rules_mdbook//mdbook:defs.bzl", "MdbookSiteInfo")
MdbookSiteInfo(tarball)
A rendered mdbook site.
FIELDS
| Name | Description |
|---|---|
| tarball | File: the gzipped tar of the rendered HTML tree. |
from docs/extensions.md
Module extension for rules_mdbook.
Auto-fetches prebuilt mdbook + mdbook-mermaid binaries for the host
platform. Versions are pinned by sha256 in
private/known_versions.bzl. Consumers can override the version per
tool via the toolchain tag class.
Default usage (pulls the default-pinned mdbook + mdbook-mermaid):
mdbook = use_extension("@rules_mdbook//mdbook:extensions.bzl", "mdbook")
use_repo(mdbook, "mdbook", "mdbook_mermaid")
Pin a specific version:
mdbook = use_extension("@rules_mdbook//mdbook:extensions.bzl", "mdbook")
mdbook.toolchain(mdbook_version = "0.5.2", mermaid_version = "0.17.0")
use_repo(mdbook, "mdbook", "mdbook_mermaid")
Release fetching is delegated to
@rules_github//github:repositories.bzl%github_binary_repository
so all our rules_* repos share one URL-shape + sha-pinning impl.
mdbook
mdbook = use_extension("@rules_mdbook//mdbook:extensions.bzl", "mdbook")
mdbook.toolchain(mdbook_version, mermaid_version)
Sets up @mdbook and @mdbook_mermaid as Bazel-fetched prebuilt binaries.
TAG CLASSES
toolchain
Attributes
from docs/toolchains.md
Toolchain rule for rules_mdbook.
mdbook_toolchain wraps a single mdbook binary as a Bazel toolchain.
Consumers (the mdbook_book and mdbook_serve rules) resolve mdbook
through @rules_mdbook//mdbook:toolchain_type, so users can register
custom mdbook binaries (locally-built fork, alternate version, …) via
register_toolchains(...) without modifying rule attributes.
The module extension at @rules_mdbook//mdbook:extensions.bzl generates
a default toolchain (@mdbook//:mdbook_toolchain_def) wrapping the
prebuilt binary. Users register it from their MODULE.bazel:
register_toolchains("@mdbook//:mdbook_toolchain_def")
mdbook_toolchain
load("@rules_mdbook//mdbook:toolchains.bzl", "mdbook_toolchain")
mdbook_toolchain(name, mdbook)
Declare an mdbook binary as a Bazel toolchain.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| mdbook | Path to the mdbook executable. | Label | required |
MdbookToolchainInfo
load("@rules_mdbook//mdbook:toolchains.bzl", "MdbookToolchainInfo")
MdbookToolchainInfo(mdbook)
The mdbook binary, resolved via a toolchain.
FIELDS
| Name | Description |
|---|---|
| mdbook | File: the mdbook executable. |
Conformance#
No gate findings. 8 contested atoms. See how gating works or the full report.
Contested atoms
Third-party modules where this module resolves a different version than others do. Not a violation of anything this module did — it is the actionable form of a registry-level convergence finding, and the sentence a maintainer can act on.
| Atom | Resolved here | Elsewhere |
|---|---|---|
apple_support | 1.24.2 | 2.2.0 ×1 |
bazel_skylib | 1.8.2 | 1.9.0 ×2 |
nlohmann_json | 3.6.1 | 3.12.0.bcr.1 ×1 |
protobuf | 33.4 | 34.0.bcr.1 ×2 |
rules_jvm_external | 6.7 | 6.8 ×4 |
rules_python | 1.7.0 | 2.0.1 ×1 |
rules_swift | 3.1.2 | 3.6.1 ×1 |
upb | 0.0.0-20220923-a547704 | 0.0.0-20230516-61a97ef ×1 |
Dependencies#
Depends on
Used by (2 in the registry)
Versions#
4 published versions, newest first. Each resolves to an immutable, integrity-checked archive.
| Version | Integrity (sha256) | Source archive |
|---|---|---|
0.3.1 latest | BFWmmucpYzhxunL3… | tag archive ↗ |
0.3.0 | lP3mpeRmH4ANEA40… | tag archive ↗ |
0.2.0 | EgLwuw7ECusp3KOD… | tag archive ↗ |
0.1.0 | 4w9VvcmELUKUQjtM… | tag archive ↗ |
Changelog#
All notable changes to rules_mdbook. The format is loosely Keep a Changelog — version headers mirror the published bazel-registry entries.
0.3.0 — Delegate release fetching to rules_github
- Delegate mdbook + mdbook-mermaid release fetching to
rules_githubinstead of hand-rolling the GitHub release download logic in-tree. - Update the install snippet to point at
fastverk/bazel-registry.
0.2.0 — Toolchain-driven resolution + mdbook_serve
- Switch mdbook resolution to a proper Bazel toolchain
(
mdbook_toolchain/@rules_mdbook//mdbook:toolchain_type) so consumers can override the binary cleanly. - Add the
mdbook_serverule:bazel run //path:serverunsmdbook servewith watch + live-reload against the live source tree.
0.1.0 — Initial release
- First public cut of
rules_mdbook:mdbookmodule extension that auto-creates@mdbook+@mdbook_mermaidexternal repos, and themdbook_bookrule that runs a hermeticmdbook buildover a staged source tree and packages the HTML output as a.tar.gz.