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:formatwrites an AST back out as Markdoc source, asMarkdoc.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
allowIndentationoption. - 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.tsandsrc/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§
- Index
Map - The
indexmapcrate this crate’s maps are made of, so that a consumer names the same version (attributes, slots,Scalar::Objectand every schema map areIndexMaps). 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:parseand the config-boundresolve,transformandvalidate(Markdoc.transform(content)istransform(content, this.config)). Its statics are this module’s functions and the crate’s modules (Markdoc.nodesisdefault_nodes,Markdoc.formatiscrate::next::formatter::format, …).
Functions§
- create_
element createElement(name, attributes, ...children):{name, attributes, children}(a plain object, not a tag).nameis any value (a string, or an object such as a React component).- default_
tokenizer new Tokenizer(), made once: the tokenizerparseuses (upstream’s module-leveltokenizer).- merge_
config mergeConfig(config): the config with the default tags, nodes and functions under its own.- parse
parse(content, args): the AST ofcontent, tokenized bydefault_tokenizer.- parse_
tokens parse(tokens, args): the AST of a token stream, which a tokenizer made fromsource(read for positions, and for the frontmatter withargs.parse_frontmatter; the tokens are taken as they are,args.strip_bomaside).- parse_
with parsewithtokenizer:parse(tokenizer.tokenize(content), args). Withargs.strip_bom, a byte-order mark that startscontentis not tokenized; the nodes’ positions are still offsets intocontent.- 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
Errofnextsays upstream throws: JavaScript’sString(error),Name: message(see the module’s documentation). - transform
transform(node, config): the render tree of a document (anarticletag), or of any node, as upstream returns it: JavaScript values, whichcrate::next::renderers::htmlrenders. Its JSON iscrate::next::schema::value::json_stringify(whatJSON.stringifywrites) or, as acrate::types::RenderableTreeNode,Value::to_render; JSON leaves out what it cannot hold, which rendering can see (an object member that isundefined), 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.