Skip to main content

Crate yamd

Crate yamd 

Source
Expand description

YAMD - Yet Another Markdown Document (flavour)

Simplified version of CommonMark.

For formatting check YAMD struct documentation.

§Quick start

use yamd::deserialize;

let input = "# Hello\n\nA paragraph with **bold** text.";
let yamd = deserialize(input);

// Access the AST
assert_eq!(yamd.body.len(), 2);

// Round-trip back to markdown
assert_eq!(yamd.to_string(), input);

§Two APIs

  • deserialize returns a nested Yamd document — a tree of typed nodes, suitable for walking, pattern-matching, or round-tripping back to markdown via Display. The AST makes invalid nestings unrepresentable, and deserialize is fuzz-tested for panic-freedom and property-tested for round-trip fidelity.
  • parse returns a flat Vec<Op> of Start/End/Value events. to_yamd promotes an event stream to the tree form. Fuzz-tested for panic-freedom (transitively, via deserialize); the AST’s type-level invariants and round-trip property do not apply at this layer.

Rendering is out of scope; Yamd is an AST you walk and render however you like. With the serde feature enabled, the AST is also serde-serializable.

§Difference from CommonMark

YAMD reuses most of CommonMark’s syntax but diverges where CommonMark’s context-dependent rules would force special cases: every node is treated the same (no container/leaf distinction), and escaping is context-independent.

§Escaping

Escaping is recognized at the lexer level: \ forces the following character into the surrounding literal run instead of its usual meaning. The \ itself is stripped later, lazily, when the content is resolved to text (e.g. via Content::as_str, or transparently as part of deserialize).

Example:

YAMDHTML equivalent
\**foo**<p>**foo**</p>

§Precedence

CommonMark distinguishes container blocks from leaf blocks and gives container-block markers higher precedence. YAMD does not distinguish block types — every node is treated the same, so there are no precedence rules to remember.

Example:

YAMDHTML equivalent
- `one\n- two` <ol><li><code>one\n- two</code></li></ol>

To get two separate ListItems, escape the backticks:

YAMDHTML equivalent
- \`one\n- two\` <ol><li>`one</li><li>two`</li><ol>

The reasoning: issues like this should be caught by tooling such as linters or language servers — that tooling doesn’t exist yet.

§Nodes

See nodes for the full list of supported nodes and their formatting. Start with YAMD.

§MSRV

YAMD minimal supported Rust version is 1.87.

Re-exports§

pub use op::parse;
pub use op::to_yamd;
pub use op::UnbalancedOpStream;
pub use op::try_to_yamd;

Modules§

lexer
Lexer module for YAMD
nodes
The AST types produced by deserialize. Every node is a plain struct that implements Display for round-tripping back to markdown.
op
A flat, streaming alternative to deserialize: parse turns source text into a Vec<Op> of Start/End/Value events instead of a nested tree.

Structs§

Yamd
Yamd

Functions§

deserialize
Deserialize a string into a Yamd struct