Expand description
mdt_core is the core library for the mdt template engine. It provides the lexer, parser, project scanner, and template engine for processing markdown template tags. Content defined once in provider blocks can be distributed to consumer blocks across markdown files, code documentation comments, READMEs, and more.
§Processing Pipeline
Markdown / source file
→ Lexer (tokenizes HTML comments into TokenGroups)
→ Pattern matcher (validates token sequences)
→ Parser (classifies groups, extracts names + transformers, matches open/close into Blocks)
→ Project scanner (walks directory tree, builds provider→content map + consumer list)
→ Engine (matches consumers to providers, applies transformers, replaces content)§Modules
config— Configuration loading frommdt.toml, including data source mappings, exclude/include patterns, and template paths.project— Project scanning and directory walking. Discovers provider and consumer blocks across all files in a project.- [
source_scanner] — Source file scanning for consumer tags inside code comments (Rust, TypeScript, Python, Go, Java, etc.).
§Key Types
Block— A parsed template block (provider or consumer) with its name, type, position, and transformers.Transformer— A pipe-delimited content filter (e.g.,trim,indent,linePrefix) applied during injection.ProjectContext— A scanned project together with its loaded template data, ready for checking or updating.MdtConfig— Configuration loaded frommdt.toml.CheckResult— Result of checking a project for stale consumers.UpdateResult— Result of computing updates for consumer blocks.
§Data Interpolation
Provider content supports minijinja template variables populated from project files. The mdt.toml config maps source files to namespaces:
[data]
pkg = "package.json"
cargo = "Cargo.toml"Then in provider blocks: {{ pkg.version }} or {{ cargo.package.edition }}.
Supported formats: JSON, TOML, YAML, and KDL.
§Quick Start
use mdt_core::project::scan_project_with_config;
use mdt_core::{check_project, compute_updates, write_updates};
use std::path::Path;
let ctx = scan_project_with_config(Path::new(".")).unwrap();
// Check for stale consumers
let result = check_project(&ctx).unwrap();
if !result.is_ok() {
eprintln!("{} stale consumer(s) found", result.stale.len());
}
// Update all consumer blocks
let updates = compute_updates(&ctx).unwrap();
write_updates(&updates).unwrap();Re-exports§
Modules§
Structs§
- Block
- Check
Result - Result of checking a project for stale consumers.
- Ordered
Float - A float wrapper that implements
Eqvia approximate comparison, allowingArgumentto derivePartialEqcleanly. - Point
- One place in a source file. This is taken from the [unist] crate with the
Copytrait added. - Position
- Location of a node in a source file. This is taken from the
unistcrate with theCopytrait added. - Render
Error - A template render error associated with a specific consumer block.
- Stale
Entry - A consumer entry that is out of date.
- Transformer
- Update
Result - Result of updating a project.
Enums§
- Argument
- Block
Type - MdtError
- Parse
Diagnostic - A diagnostic produced during parsing. These are issues that don’t prevent parsing from completing but indicate problems in the source content.
- Transformer
Type
Functions§
- apply_
transformers - Apply a sequence of transformers to content.
- build_
blocks_ from_ groups - Build blocks from already-tokenized groups. This is the shared logic used by both markdown parsing and source file scanning.
- build_
blocks_ from_ groups_ lenient - Like
build_blocks_from_groups, but silently discards unclosed blocks instead of returning an error. Used for source files where HTML comments may appear in string literals without matching close tags. - build_
blocks_ from_ groups_ with_ diagnostics - Build blocks from token groups, collecting diagnostics instead of hard-erroring. Unknown transformers and unclosed blocks are reported as diagnostics rather than causing parse failure.
- check_
project - Check whether all consumer blocks in the project are up to date. Consumer blocks that reference non-existent providers are silently skipped. Template render errors are collected rather than aborting, so the check reports all problems in a single pass.
- compute_
updates - Compute the updated file contents for all consumer blocks.
- extract_
html_ comments - Extract HTML comments (
<!-- ... -->) from raw text content, returningHtmlnodes with correct byte positions. This is used for source files where the markdown AST parser won’t find HTML comments inside code comments. - get_
html_ nodes - parse
- Parse markdown content and return all blocks (provider and consumer) found within it.
- parse_
source - Parse source code content (non-markdown) for mdt blocks by extracting HTML comments directly from the raw text.
- parse_
source_ with_ diagnostics - Parse source code content and return blocks together with diagnostics.
When
filteris enabled, HTML comments inside fenced code blocks (within doc comments) are excluded from scanning. - parse_
with_ diagnostics - Parse markdown content and return blocks together with diagnostics.
Unlike
parse(), this does not error on unclosed blocks — instead they are collected as diagnostics. - render_
template - Render provider content through minijinja using the given data context. If data is empty or the content has no template syntax, returns the content unchanged.
- validate_
transformers - Validate that all transformer arguments are well-formed. Returns an error for the first invalid transformer found.
- write_
updates - Write the updated contents back to disk.