Frontmatter Automation
Relevant source files
The following files were used as context for generating this wiki page:
Frontmatter Automation in the WHK project is a sophisticated system designed to automatically generate SEO-optimized metadata for Markdown documents. Using high-level Large Language Models (LLMs)—specifically NVIDIA NIM APIs—the system analyzes content structures to produce titles, descriptions, summaries, and extensive tag sets. This ensures that the scientific and technical knowledge base remains discoverable and professionally indexed.
The automation is primarily handled by the generate_frontmatter.js script, which operates in an "Expert Mode" to ensure deep analysis of the project's science core notes. It integrates with the broader technical stack, which includes MkDocs for static site generation and various AI-assisted review tools described in AI Review Rules.
Sources: scripts/frontmatter/generate_frontmatter.js:14-25, README.md:58-62, AGENTS.md:12-15
System Architecture
The automation pipeline follows a modular path: scanning the filesystem, extracting structural cues from Markdown files, and communicating with an external LLM to generate the final YAML metadata block.
Logic Flow and Components
The core script, generate_frontmatter.js, employs a multi-step process to transform raw Markdown into metadata-enriched files:
- Scanner: Identifies files based on CLI flags (e.g., specific files, new files only, or files changed within a Git timeframe).
- Structural Extractor: Parses the H1 title and the first 1000 lines of headings to provide the LLM with context without overloading the token window.
- LLM Integrator: Uses the
OpenAIclient pointing to NVIDIA's NIM API to process the content. - Injector: Rewrites the Markdown file, prepending the new Frontmatter while preserving the original body content.
The diagram above illustrates the decision-making process for file selection and the subsequent metadata generation workflow.
Sources: scripts/frontmatter/generate_frontmatter.js:63-125, scripts/skills/read/dir_h1.js:37-65
Metadata Specifications
The system enforces strict constraints on the generated data to maintain consistency across the blog. The LLM is instructed via a system prompt to act as a "Senior Document Architect and SEO Strategy Expert."
Frontmatter Fields
| Field | Requirement | Purpose |
|---|---|---|
title | 22 ± 3 Chinese characters | SEO creative title following "[Core] Detailed: [Mechanism] and [Scenario]" |
description | 100-200 characters | Technical route or physical/chemical mechanism description |
summary | 50-100 characters | Minimalist summary for card display |
tags | 15-25 items | Dense tags covering Subject, Parent Term, Child Term, Scenario, and Target Group |
Sources: scripts/frontmatter/generate_frontmatter.js:154-170
Structural Extraction Logic
The system uses specific helper functions to feed the LLM accurate context. For example, extractHeadings targets only the first 1000 lines to maintain efficiency.
function extractHeadings(content) {
const lines = content.split('\n');
const headings = [];
for (let i = 0; i < Math.min(lines.length, 1000); i++) {
const line = lines[i].trim();
if (line.match(/^#+\s/)) headings.push(`[L${i + 1}] ${line}`);
}
return headings.join('\n');
}
Sources: scripts/frontmatter/generate_frontmatter.js:63-71, scripts/skills/read/file_headings.js:31-45
Operation Modes and Configuration
The script provides several operational modes to handle different stages of the project's lifecycle.
Command Line Interface (CLI) Options
| Option | Alias | Description |
|---|---|---|
--new | -n | Only generate data for files currently missing Frontmatter |
--clean-new | -c | Delete all existing Frontmatter and regenerate everything |
--days <n> | -d | Process files modified in Git logs within the last n days |
--file <path> | -f | Directly process a specific file |
Sources: scripts/frontmatter/generate_frontmatter.js:27-37
Concurrency and Rate Limiting
To ensure stability when interacting with APIs, the system uses the Bottleneck library.
- Concurrency: 5 simultaneous requests.
- RPM Limit: 30 requests per minute.
- Min Time: 2000ms per request.
Sources: scripts/frontmatter/generate_frontmatter.js:51-61
Execution Sequence
The following sequence diagram outlines the interaction between the local script, the filesystem, and the remote API.
This diagram demonstrates the loop process used to batch-update files while respecting API limits.
Sources: scripts/frontmatter/generate_frontmatter.js:127-160, scripts/skills/read/SKILL.md:65-80
Integration with Reading Skills
The automation leverages logic similar to the project's read skills to understand document hierarchy. The dir_h1.js and file_headings.js tools provide fallback mechanisms or manual verification for the automation's output.
- H1 Scanning: Used to identify the primary subject if the YAML title is missing.
- Heading Extraction: Used to map the logical structure of a document for the LLM prompt.
Sources: scripts/skills/read/dir_h1.js:37-50, scripts/skills/read/file_headings.js:31-45
Conclusion
Frontmatter Automation is a critical infrastructure component of the WHK blog. By combining structural Markdown parsing with high-performance LLM APIs, it automates the tedious task of metadata creation while ensuring high-quality SEO and categorization across the science-focused documentation repository.