ndg-commonmark
High-performance, extensible Nixpkgs-flavored CommonMark processor designed specifically for ndg in order to document Nix, NixOS and Nixpkgs adjacent projects.
Features AST-based processing, syntax highlighting, and generous documentation.
Features
Core Functionality
- CommonMark Compliance: Full CommonMark specification support
- GitHub Flavored Markdown (GFM): Tables, strikethrough, task lists, and more via Comrak
- Extensible Syntax Highlighting: Modern tree-sitter based highlighting with Syntastica
- Memory Safe: Built in Rust with zero-cost abstractions
- High Performance: AST-based processing for optimal speed
Nix Ecosystem Support
- Nix Language Highlighting: First-class support for Nix syntax via Syntastica [^1]
- Role Markup: MySt-like roles such as:
- {command}
ls -la, - {file}
/etc/nixos/configuration.nix, - {var}
$XDG_CONFIG_HOME - {man}
nix.conf
- {command}
- Option References: Automatic linking to NixOS option documentation
- File Includes: Process
{=include=}blocks for modular documentation - Manpage Integration: Automatic linking to system manuals using a
{man}role and a manpage map.
[^1]: Nix syntax highlighting is available only if using Syntastica as the highlighting backend. Syntect requires an external parser collection, which we no longer support.
Other Markup Features
- Inline Anchors:
[]{#custom-id}syntax for precise linking - Admonitions: GitHub-style callouts and custom admonition blocks
- Figure Support: Structured figure blocks with captions
- Definition Lists: Technical documentation support
- Auto-linking: Automatic URL detection and conversion
Quick Start
Basic Usage
use ;
let processor = new;
let result = processor.render;
// The entire document
println!;
// Fine-grained components
println!;
println!;
With Syntax Highlighting
use ;
let mut options = default;
options.highlight_code = true;
options.highlight_theme = Some;
let processor = new;
let markdown = r#"
# Code Example
```rust
fn main() {
println!("Hello, world!");
}
```
```nix
{ pkgs, ... }: {
environment.systemPackages = with pkgs; [ vim git ];
}
```
"#;
let result = processor.render;
Feature Configuration
use MarkdownOptions;
let mut options = default;
// Enable GitHub Flavored Markdown (GFM)
options.gfm = true;
// Enable Nixpkgs-specific extensions
options.nixpkgs = true;
// Configure syntax highlighting
options.highlight_code = true;
options.highlight_theme = Some;
// Set manpage URL mappings
// XXX: this is required for {man} role to function correctly
options.manpage_urls_path = Some;
let processor = new;
Cargo Feature Flags
Some of ndg-commonmark's features are gated behind feature flags. This allows compile-time control over what will be made available. Namely you can decide which parts of the flavored CommonMark will be supported.
default: Enablesndg-flavoredndg-flavored: All NDG-specific features (gfm+nixpkgs+syntastica)gfm: GitHub Flavored Markdown extensionsnixpkgs: NixOS/Nixpkgs documentation featuressyntastica: Modern tree-sitter based syntax highlighting (recommended)syntect: Legacy, less robust and more lightweight syntax highlighting
[!NOTE] The
syntasticaandsyntectfeatures are mutually exclusive. Enable only one at a time.
[]
# Recommended: Modern syntax highlighting
= { = "1.0", = ["gfm", "nixpkgs", "syntastica"] }
# Alternative: Legacy syntax highlighting
= { = "1.0", = ["gfm", "nixpkgs", "syntect"] }
# No syntax highlighting
= { = "1.0", = ["gfm", "nixpkgs"] }
API Reference
MarkdownProcessor
The main processor class:
MarkdownOptions
Configuration options:
MarkdownResult
Processing result:
Syntax Highlighting API
For advanced syntax highlighting control:
use ;
let manager = create_default_manager?;
// Highlight code directly
let html = manager.highlight_code?;
// Language detection
if let Some = manager.highlighter.language_from_filename
// Available languages and themes
println!;
println!;
Examples
Basic Document Processing
use ;
let processor = new;
let markdown = r#"
# Documentation Example
This document demonstrates **ndg-commonmark** features.
## Code Highlighting
```rust
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
map.insert("key", "value");
println!("{:?}", map);
}
```
## NixOS Configuration
```nix
{ config, pkgs, ... }: {
services.nginx = {
enable = true;
virtualHosts."example.com" = {
enableACME = true;
forceSSL = true;
root = "/var/www/example.com";
};
};
}
```
Advanced Configuration
use ;
use HashMap;
let mut options = default;
options.gfm = true;
options.nixpkgs = true;
options.highlight_code = true;
options.highlight_theme = Some;
options.manpage_urls_path = Some;
let processor = new;
// Process multiple files
let files = ;
for file in files
Integration
Static Site Generators
use ;
use Path;
use WalkDir;
License
This project is licensed under the Mozilla Public License 2.0. See the LICENSE file for more details.
Acknowledgments
- CommonMark specification
- comrak for CommonMark parsing
- Syntastica for modern syntax highlighting
- tree-sitter for parsing technology
- The Nix community for inspiration and requirements