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 (
parse_source) — 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 std::path::Path;
use mdt_core::check_project;
use mdt_core::compute_updates;
use mdt_core::project::scan_project_with_config;
use mdt_core::write_updates;
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
- A parsed template block representing either a provider or consumer.
- 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. - Stale
Entry - A consumer entry that is out of date.
- Transformer
- A content transformer applied to provider content during injection into a consumer block.
- Update
Result - Result of updating a project.
Enums§
- Argument
- An argument value passed to a
Transformer. - 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.
- 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 - pad_
content - Ensure content has a leading newline and a trailing newline so it never runs directly into the opening or closing tag.
- 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.
- 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.