Custom Admonitions & Callouts
Relevant source files
The following files were used as context for generating this wiki page:
Custom Admonitions and Callouts in the WHK project provide a standardized way to highlight specific information, such as notes, warnings, tips, and collapsible details, within Markdown documentation. The project has migrated from older syntaxes to a unified "code-block style" syntax that is processed via a Lua filter during the build process to ensure consistent rendering across different output formats.
This system is a core component of the project's technical stack, leveraging Pandoc filters to transform standard Markdown code blocks into styled HTML elements or interactive foldables. It supports advanced features like embedded mathematical formulas (KaTeX) and nested Markdown formatting within titles and body content.
Sources: README.md:16-18, AGENTS.md:65-67
Architecture and Syntax
The custom admonition system identifies specifically formatted Markdown code blocks and transforms them into high-level UI components. This transformation is handled primarily by filters/admonition.lua during the document conversion process.
Standard Syntax Format
The system uses md or markdown code blocks with specific attributes wrapped in curly braces. This ensures the raw content remains readable in standard editors while allowing the Pandoc engine to apply custom logic.
```md {admonition="type" title="Block Title"}
Body content with Markdown support.
Sources: [AGENTS.md:69-74](https://github.com/raineblog/whk/blob/HEAD/AGENTS.md#L69-L74), [filters/admonition.lua:2-5](https://github.com/raineblog/whk/blob/HEAD/filters/admonition.lua#L2-L5)
### Component Types
The system supports two primary categories of callouts:
| Attribute Value | Category | Description |
| :--- | :--- | :--- |
| `note`, `warning`, `tip`, etc. | **Standard Admonition** | High-lighted blocks with specific colors and icons based on type. |
| `details` | **Collapsible Block** | A foldable section that is collapsed by default, requiring a user click to expand. |
Sources: [AGENTS.md:76-80](https://github.com/raineblog/whk/blob/HEAD/AGENTS.md#L76-L80), [filters/admonition.lua:42-43](https://github.com/raineblog/whk/blob/HEAD/filters/admonition.lua#L42-L43)
## Processing Logic (Lua Filter)
The `admonition.lua` script acts as a Pandoc AST (Abstract Syntax Tree) filter. It intercepts `CodeBlock` elements that have the `md` or `markdown` class and contain an `admonition` attribute.
### Transformation Workflow
The diagram shows how the Lua filter identifies, parses, and replaces standard code blocks with custom HTML structures.
Sources: filters/admonition.lua:1-100
Key Logic Features
- Markdown Recursion: Both the
titleattribute and the internal text of the code block are re-parsed as Markdown usingpandoc.read. This allows for bold text, inline code, and mathematical formulas inside admonition titles. - Default Handling: If a
detailsblock is provided without a title, the filter automatically assigns "Details" as the default summary text. - Inline Conversion: The filter utilizes
pandoc.utils.blocks_to_inlinesto ensure that titles extracted from Markdown blocks are properly flattened into inline elements for use in HTML headers or<summary>tags.
Sources: filters/admonition.lua:14-38, filters/admonition.lua:51-54
Configuration and Extensions
The admonition system is integrated into the MkDocs build pipeline via the materialx theme and specific Markdown extensions.
Markdown Extensions
The project uses the pymdownx extension suite to handle block-level processing before or alongside the Lua filters.
markdown_extensions:
- admonition
- pymdownx.blocks.admonition
- pymdownx.blocks.details
- pymdownx.superfences
Sources: scripts/template.serve.yml:85-115
Technical Constraints
To maintain rendering stability, the following constraints are enforced by the project's documentation standards:
- Standard Key-Value Pairs: Attributes must use the
key="value"format within the curly braces. - Quote Escaping: Double quotes within titles should be replaced with single quotes or Chinese double quotes to avoid breaking the attribute parser.
- Spacing: A blank line must be maintained above and below the code block to ensure the Markdown parser correctly identifies the block structure.
Sources: AGENTS.md:94-105
Usage Examples
Collapsible Detail Block
Used for deep derivations or supplementary information that should not clutter the main view.
```md {admonition="details" title="Click to view derivation of $E=mc^2$"}
1. Mass and energy are equivalent.
2. Derivation follows...
Sources: [AGENTS.md:88-92](https://github.com/raineblog/whk/blob/HEAD/AGENTS.md#L88-L92), [filters/admonition.lua:47-65](https://github.com/raineblog/whk/blob/HEAD/filters/admonition.lua#L47-L65)
### Warning Admonition
Used for critical alerts or important constraints.
```markdown
```md {admonition="warning" title="Caution: Do not divide by $0$"}
Ensure the denominator is non-zero to avoid `ZeroDivisionError`.
Sources: [AGENTS.md:82-86](https://github.com/raineblog/whk/blob/HEAD/AGENTS.md#L82-L86), [filters/admonition.lua:70-85](https://github.com/raineblog/whk/blob/HEAD/filters/admonition.lua#L70-L85)
## Summary
Custom Admonitions & Callouts in the WHK project provide a robust, flexible, and visually consistent method for content highlighting. By utilizing a Lua-based Pandoc filter, the system allows for rich Markdown content—including complex KaTeX formulas—to be embedded within both the titles and bodies of notes and collapsible detail blocks. This architecture ensures that documentation remains both highly readable and technically precise.
Sources: [README.md:16-18](https://github.com/raineblog/whk/blob/HEAD/README.md#L16-L18), [filters/admonition.lua:1-5](https://github.com/raineblog/whk/blob/HEAD/filters/admonition.lua#L1-L5)