Skip to main content

Crate moss_core

Crate moss_core 

Source
Expand description

moss-core is the pure-Rust content engine behind moss, a desktop publishing app. It owns every transformation that turns a folder of markdown into a website: parsing, wikilink resolution, HTML rendering, frontmatter typing, and schema validation.

Everything is data in, data out — strings and structs in; parsed ASTs, diagnostics, and rendered HTML out. Zero I/O, zero async, no global state. The filesystem, the network, and the async runtime all live one layer up, in moss’s host; this crate never touches them. That makes it deterministic, trivially unit-testable, and embeddable in any Rust program — not just moss.

§How it’s laid out

The modules cluster into four areas, plus the contract surface:

  • Parse & renderast turns markdown into a typed tree (over pulldown-cmark) and renders it back to HTML through interceptable hooks; render emits media HTML (image/video/audio/iframe/pdf) for embeds.
  • Frontmatter & schemafrontmatter parses YAML while preserving the body byte-for-byte; frontmatter_typed is the canonical FrontMatter struct; schema_fields is the single source of truth for built-in fields; validation produces LSP-style diagnostics against a schema.
  • Links & content modelresolve is the one place wikilinks and embeds ([[...]]) become ordinary markdown links; content_graph does the Obsidian-style fuzzy path matching underneath.
  • Utilities — small stateless helpers the editor and build share: slug, date, sort, home, page_kind, and extract_headings.

Plus contract: the design surface (W3C design tokens + the moss-* HTML class table) that theme authors and codegen depend on.

§Getting started

Every entry point is a free function — pick the module and call it:

use moss_core::frontmatter;

let raw = "---\ntitle: Hello\n---\n\nBody text";
let doc = frontmatter::parse(raw);
assert_eq!(doc.frontmatter.get("title").and_then(|v| v.as_str()), Some("Hello"));
assert_eq!(doc.body.trim(), "Body text"); // body preserved verbatim

From there: ast for the body tree, resolve to flatten wikilinks, validation to lint frontmatter, and extract_headings for anchors.

§Guarantees

Total functions: bad input degrades to a best-effort value, never an Err or a panic. No unsafe (#![forbid(unsafe_code)]). Schema problems are reported out-of-band as validation diagnostics, not return values.

moss ships this crate in a host built with panic = "abort" (release profile), so a panic on user input crashes the whole desktop app (see the date.rs fix for the editor-mount panic on Chinese filenames). The lint attributes below enforce the panic-free contract — deny(clippy::string_slice) plus deny(clippy::unwrap_used/expect_used) outside tests — each with a per-site escape-hatch rule.

Re-exports§

pub use extract_headings::extract_headings;
pub use extract_headings::HeadingInfo;
pub use page_kind::PageKind;
pub use resolved::Resolved;
pub use resolved::ResolvedOrigin;

Modules§

asset_paths
Pure URL/path transform helpers — no filesystem access, no env lookups. Used by moss-core’s render functions (see crate::render::*) and by upstream src-tauri call sites.
asset_snapshot
Pre-fetched asset metadata available to Stage 1 + Stage 2.
ast
Typed body AST.
content_graph
In-memory index of content files, headings, and block IDs.
contract
moss HTML/CSS contract surface.
csv_table
Pure CSV/TSV → HTML table renderer.
date
Publish-date resolution for moss content.
extract_headings
Pure extraction of a document’s headings (text + slug + level) for the editor’s [[Page#Heading]] autocomplete. Reuses parse() (which runs assign_heading_id_suffixes) so the returned slugs are byte-identical to the rendered <hN id="..."> attributes — the keystone invariant.
frontmatter
YAML frontmatter parsing with body preservation.
frontmatter_typed
Typed frontmatter structs for the build pipeline.
frontmatter_union
Normalization for union-typed frontmatter fields (children, series).
heading
Article heading rule — single source of truth for the auto-injected <h1 class="moss-article-title"> and the editor’s pinned heading element.
heading_anchor
Obsidian-compatible heading anchor generation.
home
Home file detection for the moss content model.
link_candidates
Link resolution candidate generation.
link_completions
Pure ranker for wikilink autocomplete completions.
media
Unified media reference resolution and display attributes.
page_kind
Classification of moss page/source-file kinds.
render
HTML synthesizers for moss-emitted markdown content.
resolve
Centralized link resolution — ALL wikilink handling (body AND frontmatter) happens here.
resolved
A value paired with its origin. Lets downstream consumers decide whether to honor the value as author intent (explicit) or override it with an inferred default (auto-detected).
schema
Content model types driven by the schema.
schema_fields
Builtin frontmatter field definitions.
shortcode_tokens
Tokenizer for shortcode opening, closing, and divider lines.
slug
URL-safe slug generation for moss-core.
sort
Folder-listing sort: types and inference cascade.
validation
Schema-driven frontmatter validation.