myst API reference
A MyST (Markedly Structured Text) reader. MyST is CommonMark plus the directives and roles of reStructuredText, as Sphinx and Jupyter Book write it. parse turns MyST source into a doctree document tree (doctree.Node), the tree markdown.parse and rst.parse build, so the same renderers serve it: toHtml / toXhtml / toAnsi here, and doctree.render / doctree.renderPdf / doctree.findAll on the tree.
Everything markdown reads (CommonMark, GFM tables, task lists, strikethrough, footnotes, ...) plus: directives in backtick fences (```{note}) or colon fences (:::{note}), with :option: lines or a --- options block; roles ({sub}`2` ); targets ((label)=) and references to them ({ref}`label` , {ref}`text <label>` , {numref}, {eq}, {doc}); % comments; +++ block breaks; front matter; and dollar math ($x$, $$ ... $$). Directives: the admonitions, code / code-block, math, image, figure, topic, sidebar, rubric, epigraph, container, raw, table, list-table, csv-table, contents and eval-rst (rST content, read by the rst module).
Parsing is lenient: an unknown directive is kept as a literal block, an unknown role as text, and check reports both as Diagnostics; Options{strict: true} turns the first problem into a catchable "myst" error. There is no file I/O: include and :file: options are refused.
Import with import "myst.j" as myst;. See the myst guide for prose and examples.
Contents
| Name | What it is |
|---|---|
check | The problems in a document, in source order: unknown directives, roles, options and reference targets, and ... |
frontMatter | The front matter of a document: the YAML between the --- lines that open it, without those lines, or "" w... |
parse | Parse MyST into a document tree: a doctree.Node whose typeOf is "document", walked with... |
parseWith | Parse MyST with explicit options. |
toAnsi | Render MyST to styled terminal text. |
toHtml | Render MyST to HTML. |
toHtmlWith | Render MyST to HTML with explicit options. |
toXhtml | Render MyST to well-formed XHTML (void elements self-close). |
Diagnostic | struct: One problem found in a document. |
Options | struct: Options for parseWith and toHtmlWith. |
Functions
check
myst.check(src as string)The problems in a document, in source order: unknown directives, roles, options and reference targets, and directives that could not be built. An empty list means the document is clean.
src- the MyST source
- Returns
list of Diagnostic - the problems found
frontMatter
myst.frontMatter(src as string)The front matter of a document: the YAML between the --- lines that open it, without those lines, or "" when it has none. Decode it with yaml.
src- the MyST source
- Returns
string - the front matter's YAML text
parse
myst.parse(src as string)Parse MyST into a document tree: a doctree.Node whose typeOf is "document", walked with doctree.findAll / doctree.text and friends and rendered with doctree.render / doctree.renderPdf. Problems are recovered from (see check); a source over 4194304 characters, a document over markdown's node budget and directives nested too deep are a catchable kind "limit" error.
src- the MyST source
- Returns
doctree.Node - the document root node
parseWith
myst.parseWith(src as string, opts as Options)Parse MyST with explicit options.
src- the MyST source
opts- the parse options
- Returns
doctree.Node - the document root node
- Throws
Error - kind
"myst"on the first problem whenopts.strictis set
toAnsi
myst.toAnsi(src as string)Render MyST to styled terminal text.
src- the MyST source
- Returns
string - the rendered text, with ANSI styling
toHtml
myst.toHtml(src as string)Render MyST to HTML. Safe by default: raw HTML is escaped and link targets go through doctree's URL filter.
src- the MyST source
- Returns
string - the rendered HTML
toHtmlWith
myst.toHtmlWith(src as string, opts as Options)Render MyST to HTML with explicit options.
src- the MyST source
opts- the parse and render options
- Returns
string - the rendered HTML
- Throws
Error - kind
"myst"on the first problem whenopts.strictis set
toXhtml
myst.toXhtml(src as string)Render MyST to well-formed XHTML (void elements self-close).
src- the MyST source
- Returns
string - the rendered XHTML
Structs
Diagnostic
One problem found in a document.
def struct Diagnostic {
severity as string,
line as int,
message as string
};severity"warning"(recovered from, likely unintended) or"error"(markup that could not be read as written)line- the 1-based source line it was found on; 0 when not known
message- what went wrong
Options
Options for parseWith and toHtmlWith. The zero value is the default.
def struct Options {
strict as bool,
allowRawHtml as bool,
noDollarMath as bool,
noColonFence as bool
};strict- throw the first warning or error as a
"myst"error instead of recovering from it allowRawHtml- pass raw HTML through when rendering (trusted input only); escaped by default
noDollarMath- read
$...$and$$...$$as plain text rather than math noColonFence- read
:::lines as plain text rather than directive fences