mdbook-gitinfo 0.1.2

An mdBook preprocessor to inject Git commit metadata into generated books
mdbook-gitinfo-0.1.2 is not a library.

An mdBook preprocessor that injects Git metadata (e.g., commit hash, date, tag) into your rendered book. This is useful for displaying build provenance or version information.

Features

  • Injects the latest Git commit information for each/per chapter/subchapter.
  • Fully configurable output format using template variables.
  • Supports date and time formatting.
  • Renders a styled footer below each chapter with Git metadata.
  • Supports the html renderer.

Installation

From package manager:

cargo install mdbook-gitinfo

Clone this repository and install it using Cargo:

cargo install --path .

Make sure the binary (mdbook-gitinfo) is in your PATH.

Configuration

Add the following to your book.toml:

[preprocessor.gitinfo]
enable = true
template = "Date: {{date}}{{sep}}branch: {{branch}}{{sep}}commit: {{hash}}"
separator = ""
font-size = "0.8em"
date-format = "%Y-%m-%d"
time-format = "%H:%M:%S"
branch = "main" # default is main, therefore optional

Example Output

With the above configuration, this footer will be injected:

<footer>
  <span class="gitinfo-footer" style="font-size:0.8em;...">
    Date: 2025-06-23 16:19:28 • branch: main • commit: 2160ec5
  </span>
</footer>

[!NOTE] date and time formatting use chrono format specifiers

Format Meaning Example
%Y Year with century 2025
%y Year without century (00-99) 25
%m Month number (01-12) 06
%b Abbreviated month name Jun
%B Full month name June
%d Day of month (01-31) 24
%e Day of month, space-padded 24
%a Abbreviated weekday Mon
%A Full weekday name Monday
%j Day of year (001–366) 176
Format Meaning Example
%H Hour (00-23) 14
%I Hour (01-12) 02
%p AM/PM PM
%M Minute (00-59) 05
%S Second (00-60, leap-sec aware) 09
%f Microseconds (000000-999999) 123456
%z +hhmm timezone offset +0100
%:z +hh:mm timezone offset +01:00
%Z Time zone name BST

Template Variables

You can use the following placeholders in the template string:

  • {{hash}}: Short commit hash (git rev-parse --short HEAD)

  • {{long}}: Full commit hash

  • {{tag}}: Git tag (from git describe)

  • {{date}}: Timestamp of the latest commit, formatted

  • {{sep}}: Separator (defaults to " • ")

.github/workflow/...

In order for mdbook-gitinfo to reference the correct commit whilst using actions/checkout@v4, set fetch-depth as 0:

...
jobs:
  deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
...

Compatibility

  • mdbook-gitinfo is tested with mdbook 0.4.x.

  • Only the html renderer is supported.

License

Apache-2.0

Author

CompEng0001