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, heading, html_entities (the one decoder for text that arrives HTML-escaped), and inert_regions (the one answer to “which byte ranges of this markdown are code or comment, and therefore not live syntax?”, shared by every pre-parse scanner in moss).

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 heading 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 — string_slice plus unwrap_used/expect_used, all denied outside tests — each with a per-site escape-hatch rule.

Re-exports§

pub use heading::extract_headings;
pub use heading::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 for fuzzy path resolution.
contract
moss HTML/CSS contract surface.
csv_table
Pure CSV/TSV → HTML table renderer.
date
Publish-date resolution for moss content.
dep_graph
Forward and backward link/embed edges between pages (moss#922 Stage 4).
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, byline, colophon).
heading
Everything moss knows about a heading, in one place.
home
Home file detection for the moss content model.
html_entities
Turn the HTML entities that real text arrives wrapped in back into characters.
inert_regions
The one answer to “which parts of this markdown are NOT live syntax?”
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.