Skip to main content

Crate docgen_bases

Crate docgen_bases 

Source
Expand description

docgen-bases — a faithful, dependency-light engine and static HTML renderer for Obsidian Bases.

A .base file is a YAML document describing filtered, sorted, grouped views over a vault’s notes, computed from note frontmatter, file metadata, and an expression language (functions, methods, operators, formulas, summaries). This crate parses that YAML, evaluates the expression language over a caller-built Corpus of Notes, and renders each view to self-contained HTML — no scripts, no runtime — for build-time inclusion in a static site.

The crate is pure: it performs no I/O and knows nothing about docgen. The host (docgen-build / docgen-core) constructs the Corpus from discovered docs and supplies a RenderOptions with the site base path.

§Quick start

use docgen_bases::{render_base_source, Corpus, Note, RenderOptions};

let mut note = Note::default();
note.slug = "books/dune".into();
note.basename = "Dune".into();
note.tags = vec!["book".into()];
let corpus = Corpus::new(vec![note]);

let yaml = "filters:\n  and:\n    - file.hasTag(\"book\")\nviews:\n  - type: table\n";
let html = render_base_source(yaml, &corpus, &RenderOptions::default());
assert!(html.contains("docgen-base-table"));

Re-exports§

pub use model::parse_base;
pub use model::BaseFile;
pub use note::parse_date;
pub use note::properties_from_yaml;
pub use note::value_from_yaml;
pub use note::Corpus;
pub use note::Note;
pub use render::error_block;
pub use render::render_base;
pub use render::render_base_source;
pub use render::RenderOptions;
pub use value::BaseDate;
pub use value::Value;

Modules§

ast
Expression AST for the Bases language. A Call whose callee is a Member is a method call (list.contains(x)); a Call on a bare Ident is a global function call (link("x")). The evaluator makes that distinction.
eval
The tree-walking evaluator. Turns an Expr into a Value against an EvalCtx (the current note, the corpus, and the base’s formulas). It never panics: unknown identifiers, type mismatches, and out-of-range indices all resolve to Value::Null, matching Obsidian’s forgiving evaluation.
filter
Compiles a Filter tree into a set of parsed predicate expressions and evaluates it against a note. Global and view filters are AND-combined by the caller. A leaf expression that fails to parse is treated as false (the row is excluded) — surfaced separately as a diagnostic by the renderer.
format
Date rendering: the default cell format plus a Moment.js format-string subset for date.format("YYYY-MM-DD"). Obsidian dates use Moment.js tokens; we implement the common subset and pass through any literal text (incl. [bracketed] literals).
functions
The Bases function library: global functions (link, date, if, list, …) and per-type methods (String/Number/Date/List/Link/File/Object/Any). All dispatch is name-based and total — an unknown name or a type mismatch yields Value::Null, never a panic.
lexer
Tokenizer for the Bases expression language. Produces a flat token stream the Pratt parser consumes. Tolerant: an unrecognized character becomes an Error token the parser surfaces, rather than panicking.
model
Serde model for a .base YAML file. Deserialization is deliberately tolerant: unknown keys are ignored, every section is optional, and the filter tree accepts either bare expression strings or nested and/or/not maps.
note
The corpus: the set of notes a base queries over. Each Note carries its frontmatter properties (as typed Values) plus file metadata (file.*). The caller (docgen-build / docgen-core) constructs these from discovered docs; this crate stays free of any filesystem or docgen-specific types.
parser
A Pratt (precedence-climbing) parser turning a token stream into an Expr. Tolerant: on a syntax error it returns Err(BaseError::Expr); callers render that as an inline error rather than failing the build.
render
Static HTML rendering of a base’s views. Produces a self-contained <div class="docgen-base"> styled by the shared docgen.css. Each view (table/cards/list) is filtered, sorted, grouped, and limited exactly as configured, then rendered to server-side HTML.
semver
Version-aware column sorting.
summary
Column summary functions for a table’s footer row. Built-in summaries operate over the set of a column’s cell values; a custom summary (defined in the base’s summaries: map) is an expression evaluated with values bound to the column’s list of values.
value
The typed value model shared by the evaluator, the function library, and the renderer. Mirrors Obsidian Bases’ runtime types: null, boolean, number, string, date, duration, list, object, and link.

Enums§

BaseError
Errors surfaced by the base engine. Parsing/eval degrade gracefully inside the renderer (producing error blocks / empty cells), so this is mostly for callers that want to parse a base explicitly.

Functions§

view_interactive_enabled
Whether the interactive island should be enabled for view within base.