An mdBook preprocessor that injects Git metadata (commit hash, full hash, tag, date/time, branch) into each chapter — as a header, a footer, or both — with flexible templates, alignment, and CSS-style margins.
Features
- Injects the latest Git commit information per chapter (and subchapters).
- Header and/or footer placement with independent configuration.
- Message templating via
message.header/message.footer/message.both. - Split alignment for header/footer or a single legacy value.
- CSS-style margins (TRBL) with per-placement overrides and shorthand.
- Timezone-aware date/time rendering (local, UTC, source, or fixed offset).
- Optional hyperlinks for commit and branch to your remote provider.
- Branch verification with graceful fallback to
"main". - Only the
htmlrenderer is supported.
Installation
From crates.io:
From source (in this repo):
Ensure the mdbook-gitinfo binary is on your PATH.
Quick start
Add to book.toml:
[]
= true
# choose placement(s)
= false # default: false
= true # default: true
# common formatting
= "0.9em"
= " • "
= "%Y-%m-%d"
= "%H:%M:%S"
= "main"
= true # make hash/branch clickable when possible
# optional timezone handling
= "local" # "local" (default) | "utc" | "source" | "fixed:+02:00"
[]
= "Built {{date}}{{sep}}commit: {{hash}}"
Configuration via dotted keys (with table equivalents)
You can configure options either with dotted keys under [preprocessor.gitinfo] or with nested tables like [preprocessor.gitinfo.message]. Use one style consistently for readability; both work and merge as expected.
Timezone
The new timezone option controls how commit timestamps are rendered.
| Value | Description |
|---|---|
local (default) |
Convert to system local time. |
utc |
Convert to Coordinated Universal Time (UTC). |
source |
Use the commit’s recorded timezone offset (as authored). |
fixed:+HH:MM or fixed:-HH:MM |
Force a specific fixed offset. |
| anything else | Emits a warning and falls back to local. |
[!NOTE] The offset is always applied, but not shown unless you include
%z,%:z, or%Zin your time-format
Message templates
Placeholders: {{hash}}, {{long}}, {{tag}}, {{date}}, {{sep}}, {{branch}}
Precedence (per placement): message.header/footer ➝ message.both ➝ legacy header_message/footer_message ➝ legacy template.
If a placement-specific template is set (
message.headerormessage.footer),message.bothis ignored for that placement.
Dotted keys:
[]
= "Last updated: <em>{{date}}</em>"
= "branch: {{branch}}{{sep}}commit: {{hash}}"
= "<em>{{date}}</em>{{sep}}branch: {{branch}}"
Table form (equivalent):
[]
= "Last updated: <em>{{date}}</em>"
= "branch: {{branch}}{{sep}}commit: {{hash}}"
= "<em>{{date}}</em>{{sep}}branch: {{branch}}"
Align
Values: "left" | "center" | "right" (default center for both).
Resolution: align.header and/or align.footer override align.both.
If neither is set, both default to "center".
Dotted keys:
[]
= "left"
= "right"
= "center" # used only for any placement not explicitly set
Table form (equivalent):
[]
= "center"
= "left"
= "right"
Margin (TRBL)
Margins accept CSS-style Top Right Bottom Left values. Units can be px, em, etc., or unitless (0).
You can provide:
- a single value (applies to all sides),
- an array with 1–4 items (CSS shorthand),
- or named sides (
top/right/bottom/left).
Resolution: margin.header / margin.footer override margin.both per placement.
Defaults (when unset):
- Header:
["0", "0", "2em", "0"](space below the header block) - Footer:
["2em", "0", "0", "0"](space above the footer block) - Legacy
margin-top(if present) is treated as footer top spacing.
Dotted keys (array shorthand):
[]
= ["0", "0", "1.25em", "0"] # T R B L
= ["2em", "0", "0", "0"] # T R B L
= "1em" # all sides = 1em unless overridden
Dotted keys (named sides):
[]
= "5em"
= "2em"
= "0.5em"
Table form (equivalent):
[]
= ["1em"] # all sides = 1em
= ["0", "0", "1.25em", "0"] # T R B L
= { = "2em" } # named sides form
Placeholders
Use these tokens inside your message templates:
{{hash}}— short commit hash{{long}}— full commit hash{{tag}}— nearest tag{{date}}— commit date and time (combined using yourdate-formatandtime-format){{sep}}— the configured separator (e.g.," • "){{branch}}— branch name
Example output
With the configuration above, a footer will be injected similar to:
branch: main • commit: 9296b47
The preprocessor inserts blank lines around injected blocks so Markdown headings/paragraphs render correctly.
Formatting & Git options
font-size— e.g.,"0.8em"separator— string used by{{sep}}date-format,time-format— chrono formatting strings (examples below)branch— default"main". If the branch isn’t found, the preprocessor falls back to"main"with a warning.hyperlink— whentrue,{{hash}}and{{branch}}are linked to your provider (derived from CI env vars likeGITHUB_SERVER_URL/GITHUB_REPOSITORY,CI_SERVER_URL/CI_PROJECT_PATH, Bitbucket vars, orremote.origin.url).
Common chrono format specifiers
For DateTime format specifiers refer to chrono::format:
CI note (GitHub Actions)
When using actions/checkout@v4, set fetch-depth: 0 so the plugin can access commit history:
jobs:
deploy:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# … your build & deploy steps
Compatibility
- Tested with mdBook 0.4.x.
- Renderer support: html only.