Skip to main content

Crate markdoc

Crate markdoc 

Source
Expand description

Markdoc for Rust: a port of Markdoc v0.5.11, the JavaScript original by Stripe, onto a port of markdown-it 15.0.1.

From the same source and the same configuration, every output upstream exposes is upstream’s: tokens, AST, validate’s errors, the render tree, HTML, and what a step throws. tests/conformance.rs grades upstream’s own conformance corpus (105 of 105), and CONFORMANCE.md lists every difference.

use markdoc::parser::ParserArgs;
use markdoc::renderers::html;
use markdoc::schema::Config;

let ast = markdoc::parse("# Hello {% .greeting %}\n", &ParserArgs::default()).unwrap();
let config = Config::default();
assert!(markdoc::validate(&ast, &config).unwrap().is_empty());
let tree = markdoc::transform(&ast, &config).unwrap();
assert_eq!(
    html::render(&tree).unwrap(),
    r#"<article><h1 class="greeting">Hello </h1></article>"#
);

The pipeline is upstream’s index.ts: parse (or parse_with a tokenizer::Tokenizer of the caller’s, parse_tokens from tokens), validate, transform (a schema::value::Value, as upstream returns it) and renderers::html::render. The modules are upstream’s: parser, tokenizer, schema, tags, functions, transformer, validator, renderers (html, react, reactStatic), transforms and formatter (Markdoc.format), plus the tag grammar (grammar), the AST (ast), the values (types), the built-in objects a value reaches (realm) and the model of node’s call stack (depth). Upstream’s Ast namespace (fromJSON, isAst, getAstValues, …) is next::ast until the physical move gives it a root path (the root’s ast is the crate’s AST types).

The crate’s own extensions are in ext, every one off until a caller turns it on: parse switches and a TOML schema bundle with overlays, control flow, partials from files, cross-references, tag occurrences, more functions, variable bindings, a frontmatter helper. So are the parser’s own: this crate’s positions (ast::Node::span, 1-based lines, UTF-16 columns and byte offsets, on nodes and errors) are found only with parser::ParserArgs::spans on (they cost about half of a parse); a tool that reports positions, such as a linter, turns them on (ParserArgs { spans: true, ..Default::default() }, or ext::ParseOptions::with_spans). Upstream’s own lines and location are there either way.

next is the path all of this had while the old pipeline was being replaced; it is kept, deprecated, for one release.

Re-exports§

pub use ast::Function;
pub use ast::Node;
pub use ast::PathSegment;
pub use ast::Variable;
pub use indexmap;

Modules§

ast
depth
Call stack depth: where upstream’s recursion is bounded only by V8’s stack, the port’s stops where node’s does.
ext
Extensions to upstream Markdoc: layers this crate keeps beside the conformant core.
formatter
Port of upstream Markdoc v0.5.11’s src/formatter.ts: format writes an AST back out as Markdoc source, as Markdoc.format(ast, options) does.
functions
Port of upstream Markdoc v0.5.11’s src/functions/index.ts.
grammar
The parser for the content of a Markdoc tag: everything between {% and %}.
index
Port of upstream Markdoc v0.5.11’s index.ts.
markdown_it
A port of markdown-it 15.0.1, patched with Markdoc’s allowIndentation option.
next
Deprecated: the old path of the crate’s API, kept for one release. Use the root paths.
parser
Port of upstream Markdoc v0.5.11’s src/parser.ts.
positions
Positions in a source text.
realm
The JavaScript realm upstream’s values live in: the built-in objects a value reaches by reading properties, and what calling one of them does.
renderers
Port of upstream Markdoc v0.5.11’s src/renderers/index.ts.
schema
Port of upstream Markdoc v0.5.11’s src/schema.ts and src/schema-types/*.
tags
Port of upstream Markdoc v0.5.11’s src/tags/index.ts.
tokenizer
Port of upstream Markdoc v0.5.11’s src/tokenizer/index.ts.
transformer
Port of upstream Markdoc v0.5.11’s src/transformer.ts.
transforms
Port of upstream Markdoc v0.5.11’s src/transforms/index.ts.
types
validator
Port of upstream Markdoc v0.5.11’s src/validator.ts.

Structs§

IndexMap
The indexmap crate this crate’s maps are made of, so that a consumer names the same version (attributes, slots, Scalar::Object and every schema map are IndexMaps). A hash table where the iteration order of the key-value pairs is independent of the hash values of the keys.
Markdoc
new Markdoc(config), upstream’s default export: parse and the config-bound resolve, transform and validate (Markdoc.transform(content) is transform(content, this.config)). Its statics are this module’s functions and the crate’s modules (Markdoc.nodes is default_nodes, Markdoc.format is crate::next::formatter::format, …).

Functions§

create_element
createElement(name, attributes, ...children): {name, attributes, children} (a plain object, not a tag). name is any value (a string, or an object such as a React component).
default_tokenizer
new Tokenizer(), made once: the tokenizer parse uses (upstream’s module-level tokenizer).
merge_config
mergeConfig(config): the config with the default tags, nodes and functions under its own.
parse
parse(content, args): the AST of content, tokenized by default_tokenizer.
parse_tokens
parse(tokens, args): the AST of a token stream, which a tokenizer made from source (read for positions, and for the frontmatter with args.parse_frontmatter; the tokens are taken as they are, args.strip_bom aside).
parse_with
parse with tokenizer: parse(tokenizer.tokenize(content), args). With args.strip_bom, a byte-order mark that starts content is not tokenized; the nodes’ positions are still offsets into content.
resolve
resolve(content, config): the document with its variables and function calls resolved.
resolve_all
resolve(content, config) of a list of nodes: each one resolved (content.flatMap((child) => child.resolve(config)), and a node resolves to one node).
thrown
What an Err of next says upstream throws: JavaScript’s String(error), Name: message (see the module’s documentation).
transform
transform(node, config): the render tree of a document (an article tag), or of any node, as upstream returns it: JavaScript values, which crate::next::renderers::html renders. Its JSON is crate::next::schema::value::json_stringify (what JSON.stringify writes) or, as a crate::types::RenderableTreeNode, Value::to_render; JSON leaves out what it cannot hold, which rendering can see (an object member that is undefined), so render the tree, not its JSON.
transform_all
transform(nodes, config) of a list of nodes: each one’s render tree, an array a node renders to spliced in.
validate
validate(node, config): every node’s errors, in document order.