tomato·bazeldocs v0 · latest
Docs/Reference/Modules/rules_mdbook

rules_mdbook

Bazel rules for mdbook with mdbook-mermaid plugin support. Hermetic, sha256-pinned binaries; mdbook_book rule produces a packaged HTML tarball.

Latest0.3.1
Versions4
CategoryBazel rules
Compat level1
MaintainersMatt Marshall
Registryhttps://registry.tbzl.dev/modules/rules_mdbook/
Sourcegithub.com/tomato-bazel/rules_mdbook
MODULE.bazelstarlark
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_mermaid external 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 — runs mdbook build over a staged source tree, packages HTML as .tar.gz.
    • mdbook_servebazel run //path:servemdbook serve with 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:

  1. Stages book.toml + every src file into a sandbox dir at their package-relative paths (minus src_strip_prefix).
  2. Copies the mdbook binary and each plugin into the sandbox’s bin/ under their bare filenames (so mdbook can resolve plugins by name on PATH).
  3. Runs mdbook build from the sandbox root.
  4. Tars book/html/ (or book/, whichever the mdbook backend wrote) into the declared out.

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

LayerPinned by
mdbook binarysha256 in mdbook/private/known_versions.bzl per (version, platform)
mermaid pluginsame table
Source treeBazel’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_binary and pass them via the plugins attr.
  • Source-tree staging that reorganizes a non-standard layout into what book.toml expects — keep as a project-local script invoked before mdbook_book.
  • Linkcheck — pass mdbook-linkcheck (or mdbook-linkcheck2) via plugins like 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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcsAll 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 labelsrequired
outThe rendered site, packaged as a .tar.gz.Labelrequired
book_tomlThe mdbook configuration file. Staged at the root of the build sandbox.Labelrequired
pluginsmdbook plugin executables (e.g. @mdbook_mermaid//:mdbook-mermaid). Staged onto PATH so mdbook can resolve them by bare name.List of labelsoptional[]
src_strip_prefixPrefix to strip from each src’s package-relative path before staging. Empty means files land at their package-relative paths.Stringoptional""

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
pluginsmdbook plugin executables, staged onto PATH so mdbook resolves them by bare name. Match the plugins listed in your book.toml.List of labelsoptional[]

MdbookSiteInfo

load("@rules_mdbook//mdbook:defs.bzl", "MdbookSiteInfo")

MdbookSiteInfo(tarball)

A rendered mdbook site.

FIELDS

NameDescription
tarballFile: 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

NameDescriptionTypeMandatoryDefault
mdbook_versionOverride mdbook version. Defaults to the value in known_versions.bzl.Stringoptional""
mermaid_versionOverride mdbook-mermaid version. Defaults to the value in known_versions.bzl.Stringoptional""

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
mdbookPath to the mdbook executable.Labelrequired

MdbookToolchainInfo

load("@rules_mdbook//mdbook:toolchains.bzl", "MdbookToolchainInfo")

MdbookToolchainInfo(mdbook)

The mdbook binary, resolved via a toolchain.

FIELDS

NameDescription
mdbookFile: 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.

AtomResolved hereElsewhere
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#

rules_mdbook in the registry graph — what it depends on (left) and what depends on it (right).

Depends on

platforms0.0.10bazel_skylib1.7.1rules_github0.1.1rules_shell0.4.1devstardoc0.7.2dev

Used by (2 in the registry)

Versions#

4 published versions, newest first. Each resolves to an immutable, integrity-checked archive.

VersionIntegrity (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_github instead 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_serve rule: bazel run //path:serve runs mdbook serve with watch + live-reload against the live source tree.

0.1.0 — Initial release

  • First public cut of rules_mdbook: mdbook module extension that auto-creates @mdbook + @mdbook_mermaid external repos, and the mdbook_book rule that runs a hermetic mdbook build over a staged source tree and packages the HTML output as a .tar.gz.

← All modules