This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
mdBook Integration
Loading…
mdBook Integration
Relevant source files
Purpose and Scope
This page documents how the system integrates with mdBook and mdbook-mermaid to generate the final HTML documentation. It covers the configuration generation process, automatic table of contents creation, mermaid diagram support installation, and the build execution. For information about the overall three-phase pipeline, see Three-Phase Pipeline. For template injection specifics, see Template Injection.
mdBook and mdbook-mermaid Tools
The system uses two Rust-based tools compiled during the Docker build:
| Tool | Purpose | Installation Location |
|---|---|---|
mdbook | Static site generator for documentation from Markdown | /usr/local/bin/mdbook |
mdbook-mermaid | mdBook preprocessor for rendering Mermaid diagrams | /usr/local/bin/mdbook-mermaid |
Both tools are compiled from source in the Rust builder stage of the Docker image and copied to the final Python-based runtime container for size optimization.
mdBook Build Pipeline
graph TB
subgraph "Input_Preparation"
WikiDir["/workspace/wiki/\nEnhanced markdown files"]
Templates["Processed templates\nHEADER_HTML, FOOTER_HTML"]
end
subgraph "Configuration_Generation"
BookToml["book.toml generation\n[scripts/build-docs.sh:102-119]"]
SummaryGen["SUMMARY.md generation\n[scripts/build-docs.sh:124-186]"]
EnvVars["Environment variables:\nBOOK_TITLE, BOOK_AUTHORS\nGIT_REPO_URL"]
end
subgraph "Structure_Creation"
BookDir["/workspace/book/\nmdBook project root"]
SrcDir["/workspace/book/src/\nMarkdown source"]
CopyFiles["Copy wiki files to src/\n[scripts/build-docs.sh:237]"]
InjectTemplates["Inject header/footer\n[scripts/build-docs.sh:239-261]"]
end
subgraph "mdBook_Processing"
MermaidInstall["mdbook-mermaid install\n[scripts/build-docs.sh:266]"]
MdBookBuild["mdbook build\n[scripts/build-docs.sh:271]"]
Preprocessor["mdbook-mermaid preprocessor\nConverts ```mermaid blocks"]
end
subgraph "Output"
BookHTML["/workspace/book/book/\nBuilt HTML documentation"]
OutputCopy["Copy to /output/book/\n[scripts/build-docs.sh:279]"]
end
EnvVars --> BookToml
EnvVars --> SummaryGen
WikiDir --> CopyFiles
Templates --> InjectTemplates
BookToml --> BookDir
SummaryGen --> SrcDir
CopyFiles --> SrcDir
InjectTemplates --> SrcDir
BookDir --> MermaidInstall
MermaidInstall --> MdBookBuild
SrcDir --> MdBookBuild
MdBookBuild --> Preprocessor
Preprocessor --> BookHTML
BookHTML --> OutputCopy
Sources: scripts/build-docs.sh:95-295
Configuration Generation (book.toml)
The book.toml configuration file is dynamically generated at scripts/build-docs.sh:102-119 using environment variables.
book.toml Structure
Sources: scripts/build-docs.sh:102-119
Configuration Sections
| Section | Key | Value Source | Purpose |
|---|---|---|---|
[book] | title | $BOOK_TITLE | Documentation title displayed in UI |
[book] | authors | $BOOK_AUTHORS | Author names shown in metadata |
[book] | language | "en" (hardcoded) | Content language for HTML lang attribute |
[book] | src | "src" (hardcoded) | Markdown source directory relative to book root |
[output.html] | default-theme | "rust" (hardcoded) | Visual theme (rust, light, navy, ayu, coal) |
[output.html] | git-repository-url | $GIT_REPO_URL | Enables “Edit on GitHub” link in top bar |
[preprocessor.mermaid] | command | "mdbook-mermaid" | Specifies mermaid preprocessor executable |
[output.html.fold] | enable | true | Enables collapsible sidebar sections |
[output.html.fold] | level | 1 | Sidebar sections collapsed by default at depth 1 |
The git-repository-url setting automatically adds an “Edit on GitHub” button to the top-right of each page, pointing to the repository specified in $GIT_REPO_URL (defaults to https://github.com/$REPO).
Sources: scripts/build-docs.sh:102-119
SUMMARY.md Generation
The table of contents is automatically generated by analyzing the file structure in /workspace/wiki/. The algorithm at scripts/build-docs.sh:124-186 creates a hierarchical navigation structure.
SUMMARY.md Generation Algorithm
graph TB
subgraph "File_Discovery"
WikiDir["/workspace/wiki/"]
ListFiles["ls *.md\n[scripts/build-docs.sh:135]"]
FilterNumeric["grep -E '^[0-9]'\n[scripts/build-docs.sh:150]"]
NumericSort["sort -t- -k1 -n\n[scripts/build-docs.sh:151]"]
end
subgraph "Overview_Detection"
OverviewFile["Find non-numeric file\n[scripts/build-docs.sh:138]"]
ExtractTitle["head -1 /sed 's/^# //' [scripts/build-docs.sh:140]"]
WriteOverview["Write [Title] filename [scripts/build-docs.sh:141]"]
end subgraph "Main_Page_Processing" IteratePages["Iterate main pages [scripts/build-docs.sh:158-185]"]
ExtractPageTitle["Extract '# Title' from file [scripts/build-docs.sh:163]"]
CheckSubsections["Check section-N directory [scripts/build-docs.sh:166-169]"]
end subgraph "Subsection_Handling" ListSubsections["ls section-N/*.md [scripts/build-docs.sh:174]"]
SortSubsections["sort -t- -k1 -n [scripts/build-docs.sh:174]"]
ExtractSubTitle["Extract '# Title' from subfile [scripts/build-docs.sh:178]"]
WriteSubsection["Write ' - [Title] section-N/file ' [scripts/build-docs.sh:179]"]
end subgraph "Output" SummaryMd["/workspace/book/src/SUMMARY.md"]
end
WikiDir --> ListFiles
ListFiles --> OverviewFile
OverviewFile --> ExtractTitle
ExtractTitle --> WriteOverview
ListFiles --> FilterNumeric
FilterNumeric --> NumericSort
NumericSort --> IteratePages
IteratePages --> ExtractPageTitle
ExtractPageTitle --> CheckSubsections
CheckSubsections -->|Has subsections|ListSubsections
ListSubsections --> SortSubsections
SortSubsections --> ExtractSubTitle
ExtractSubTitle --> WriteSubsection
CheckSubsections -->|No subsections| WriteStandalone["Write '- [Title](file)'"]
WriteOverview --> SummaryMd
WriteSubsection --> SummaryMd
WriteStandalone --> SummaryMd
Sources: scripts/build-docs.sh:124-186
SUMMARY.md Structure
The generated SUMMARY.md follows this format:
Generation Logic
-
Overview Detection scripts/build-docs.sh:136-144: Finds the first non-numeric markdown file and writes it as the top-level overview link.
-
Main Page Sorting scripts/build-docs.sh:147-155: Extracts numeric prefixes (e.g.,
1,2,10) and sorts numerically usingsort -t- -k1 -n, ensuring10-file.mdcomes after2-file.md. -
Subsection Detection scripts/build-docs.sh:166-180: For each main page with numeric prefix
N, checks if directorysection-N/exists. If found, lists and sorts subsection files, writing them indented with two spaces. -
Title Extraction scripts/build-docs.sh:163-178: Reads the first line of each markdown file and removes the
#prefix usingsed 's/^# //'.
Sources: scripts/build-docs.sh:124-186
mdbook-mermaid Installation
Before building the book, the mermaid preprocessor assets must be installed. This is performed by mdbook-mermaid install at scripts/build-docs.sh266
mdbook-mermaid Installation Process
Sources: scripts/build-docs.sh:263-266
The mdbook-mermaid install command:
- Downloads the mermaid.js library (version compatible with Mermaid 11)
- Creates initialization scripts to configure mermaid rendering
- Adds CSS stylesheets for diagram theming
- Modifies the HTML templates to include the necessary
<script>tags
During the subsequent mdbook build, the mermaid preprocessor:
- Detects code blocks with ````mermaid` fence
- Wraps them in
<pre class="mermaid">tags - Leaves the mermaid syntax intact for client-side rendering
Sources: scripts/build-docs.sh:263-271
Build Execution
The actual mdBook build occurs at scripts/build-docs.sh271 with a simple mdbook build command.
mdBook Build Process
Sources: scripts/build-docs.sh:268-271
Build Steps
-
Configuration Parsing : mdBook reads
book.tomlto load title, authors, theme, and preprocessor configuration. -
Chapter Loading : mdBook parses
SUMMARY.mdto determine navigation structure and file order. -
Markdown Processing : Each markdown file is read from the
src/directory. -
Preprocessor Execution : The
mdbook-mermaidpreprocessor transforms mermaid code blocks into render-ready HTML. -
Theme Application : The
rusttheme provides CSS styling, JavaScript functionality, and HTML templates. -
Search Index : mdBook generates a searchable index from all content, enabling the built-in search feature.
-
Output Generation : HTML files are written to the
book/subdirectory within the project root.
Sources: scripts/build-docs.sh:268-271
Output Structure
After the build completes, the system copies outputs to /output/ at scripts/build-docs.sh:274-295
Output Directory Structure
Sources: scripts/build-docs.sh:274-295
Output Artifacts
| Path | Content | Purpose |
|---|---|---|
/output/book/ | Complete HTML documentation | Servable website with search, navigation, and rendered diagrams |
/output/markdown/ | Enhanced markdown files | Source files with injected headers/footers and diagrams |
/output/raw_markdown/ | Pre-enhancement markdown | Debug artifact showing initial conversion from HTML |
/output/book.toml | mdBook configuration | Reference for reproduction or customization |
The book/ directory contains:
index.html- Main entry point with navigation sidebarprint.html- Single-page version for printing*.html- Individual chapter pagessearchindex.json- Search index datasearchindex.js- Search functionality- CSS and JavaScript assets for theming and interactivity
Sources: scripts/build-docs.sh:274-295 README.md:53-58
Local Serving
The generated HTML can be served locally using Python’s built-in HTTP server, as documented in README.md26:
This serves the documentation at http://localhost:8000, providing:
- Full-text search functionality
- Interactive navigation sidebar
- Rendered Mermaid diagrams (client-side rendering)
- Theme switching (light/dark/rust/coal/navy/ayu)
- Print-friendly single-page view
Sources: README.md:26-29 scripts/build-docs.sh:307-308
Dismiss
Refresh this wiki
Enter email to refresh