Build System & Task Runner
Relevant source files
The following files were used as context for generating this wiki page:
The Build System and Task Runner for the WHK project is a multi-stage pipeline designed to transform raw Markdown notes into a highly optimized, production-ready static website. It leverages a combination of Python for site generation, Bun for high-performance asset post-processing, and Node.js for metadata management and quality assurance.
The system is architected to handle complex requirements such as server-side KaTeX rendering, automated PDF exportation, and AI-driven metadata generation. It integrates several specialized tools including mkdocs with the materialx theme, PostCSS for style optimization, and terser for script minification.
Architecture Overview
The build process follows a tiered approach where configuration is dynamically generated before the static site engine is invoked. Once the site is generated, a post-processing layer optimizes the resulting artifacts.
This diagram illustrates the sequential flow from configuration generation to the final optimized asset output.
Sources: README.md:120-143, scripts/generate.py:101-140
Core Components
1. Configuration Generator (scripts/generate.py)
This Python script serves as the primary entry point for the build lifecycle. It performs pre-build checks and dynamically generates the mkdocs (or mkpandocs) configuration file from templates.
- Pre-build Validation: It checks for social plugin dependencies like
cairosvgandpillow. If missing, it automatically disables the social features to prevent build failures. - Dynamic Context: It loads project metadata from
project.jsonand navigation structures fromnav.jsonto populate a Jinja2 template. - Environment Management: It supports both
uvand standard Python environments, executing commands viasubprocess.
Sources: scripts/generate.py:38-60, scripts/generate.py:101-140
2. Asset Optimizer (build.ts)
A high-performance Bun script that performs "in-place" optimization of the generated static files. It targets HTML, CSS, and JS files specifically.
| Feature | Implementation | Description |
|---|---|---|
| HTML Optimization | posthtml | Performs DOM-level restructuring, reordering <head> elements, and removing redundant attributes. |
| CSS Minification | PostCSS + cssnano | Compresses styles and adds vendor prefixes using autoprefixer. |
| JS Compression | terser | Performs deep compression (2 passes) and mangling while maintaining source maps. |
| Head Reordering | reorderHead | Reorders <head> elements (charset > viewport > title > meta > styles > scripts) for optimal browser parsing. |
Sources: build.ts:1-20, build.ts:192-230, build.ts:316-435
3. Metadata Engine (scripts/frontmatter/generate_frontmatter.js)
This Node.js script utilizes OpenAI/NVIDIA NIM APIs to automate the generation of SEO-optimized Frontmatter for Markdown files.
- Atomic Tagging: Generates 15-25 tags across five dimensions (Subject, Core Term, Detail, Application, Target).
- SEO Optimization: Enforces specific character counts for titles (20-25 chars) and descriptions (100-200 chars).
- Concurrency Control: Uses
Bottleneckto manage API rate limits (30 RPM).
Sources: scripts/frontmatter/generate_frontmatter.js:15-55, scripts/frontmatter/generate_frontmatter.js:130-165
Build Lifecycle & Commands
The project supports multiple ways to trigger the build system, catering to local development and CI/CD environments.
Command Execution Flow
This sequence shows the interaction between the Python generator, the static site generator, and the Bun optimizer.
Sources: scripts/generate.py:112-145, build.ts:511-535
Key Build Commands
| Command | Purpose | Source |
|---|---|---|
make serve | Starts a Docker-based preview server. | README.md:104 |
uv run scripts/generate.py --build | Local Python-driven build process. | README.md:120 |
bun run build.ts --dir site | Optimizes assets in the specified directory. | build.ts:30 |
make export | Triggers PDF generation via Puppeteer and LaTeX. | README.md:112 |
Asset Optimization Logic
The build.ts script implements sophisticated DOM manipulation to improve PageSpeed and SEO metrics.
HTML Head Optimization
The reorderHead function categorizes and sorts <head> children to ensure the browser discovers critical resources as early as possible.
const CATEGORIES = {
charset: 0,
viewport: 1,
title: 2,
meta: 3,
preconnect: 4,
preload: 5,
stylesheet: 6,
style: 7,
script_module: 8,
script_defer: 9,
script_async: 10,
script_blocking: 11,
other: 12,
};
Sources: build.ts:192-230
Post-Processing Pipeline
The HTML processing involves a multi-plugin pipeline:
- Deduplication: Removes duplicate meta tags, canonical links, and structured data (
application/ld+json). - Merging: Combines multiple
<style>tags into a single block to reduce parsing overhead. - Accessibility: Automatically generates
altattributes for<img>tags based on filenames if they are missing.
Sources: build.ts:74-90, build.ts:133-160, build.ts:258-290
Dependencies and Runtime
The build system utilizes different runtimes based on the task:
- Python (>=3.14): Handles the primary site logic using
mkdocs-materialx,mkpandocs, andjinja2. - Bun: Used for
build.tsto take advantage of fast file I/O and built-in SQLite/Glob support. - Node.js: Required for specific scripts like
generate_frontmatter.jsandreview.js.
Sources: pyproject.toml:1-15, build.ts:1-15
Conclusion
The WHK build system is a specialized pipeline that prioritizes both developer experience (via Python-based generation) and end-user performance (via Bun-based post-processing). By decoupling site generation from asset optimization, the project achieves high-quality output while maintaining a flexible, modular architecture.