markdown API reference
A CommonMark reader. parse turns Markdown into a doctree document tree (doctree.Node), which the doctree module walks and renders to HTML, XHTML, styled terminal text and PDF; toHtml / toXhtml / toAnsi / toPdf here are shortcuts from Markdown text. It also authors Markdown text (header / style / link / list / codeBlock / table) and aligns table source (tablePretty).
The parser follows CommonMark's block and inline algorithms: block quotes and list items are containers holding any blocks, with lazy continuation and tight / loose lists; ATX and setext headings; indented and fenced code; HTML blocks; thematic breaks; link reference definitions; emphasis by the delimiter rules; code spans; inline, reference and autolinks; images; backslash escapes; entities; hard and soft line breaks. Extensions: GFM tables, task lists and ~~strikethrough~~; ~sub~, ^sup^, ==mark==; definition lists (term then : definition); GitHub alerts (> [!NOTE]) as admonitions; {#id .class key="v"} attribute lists on headings, links and images. Raw inline HTML is recognised but kept as text. Pure Jennifer.
Import with import "markdown.j" as markdown;. See the markdown guide for prose and examples.
Contents
| Name | What it is |
|---|---|
bullets | Render an unordered list, one - item per line. |
codeBlock | Render a fenced code block around verbatim text. |
header | Render an ATX heading. |
link | Render an inline link [text](url). |
numbered | Render an ordered list, `1. |
parse | Parse Markdown into a document tree, walked by the reader accessors (typeOf / children / text /... |
style | Wrap text in an inline emphasis span. |
table | Render a GFM table. |
tablePretty | Reformat every GFM table in Markdown text so its source columns line up (padded cells, aligned delimiters),... |
toAnsi | Render Markdown to styled terminal text, blocks separated by a blank line. |
toHtml | Render Markdown to an HTML string (block elements concatenated, no indentation). |
toHtmlWith | Render Markdown to HTML with explicit options. |
toPdf | Render a Markdown string to PDF bytes with the default options. |
toPdfWith | Render a Markdown string to PDF bytes with the given options. |
toXhtml | Render Markdown to a well-formed XHTML string: void elements self-close (<hr />) and boolean attributes e... |
HtmlOptions | struct: Options for HTML rendering. |
Functions
bullets
markdown.bullets(items as list of string)Render an unordered list, one - item per line.
items- the list items
- Returns
string - the Markdown bullet list
codeBlock
markdown.codeBlock(text as string)Render a fenced code block around verbatim text.
text- the verbatim code
- Returns
string - the fenced Markdown code block
header
markdown.header(level as string, text as string)Render an ATX heading.
level- the heading depth, "h1".."h6"
text- the heading text
- Returns
string - the Markdown heading line
- Throws
Error - when level is not "h1".."h6"
link
markdown.link(text as string, url as string)Render an inline link [text](url).
text- the link text
url- the link target
- Returns
string - the Markdown link
numbered
markdown.numbered(items as list of string)Render an ordered list, 1. item upward.
items- the list items
- Returns
string - the Markdown numbered list
parse
markdown.parse(md as string)Parse Markdown into a document tree, walked by the reader accessors (typeOf / children / text / level / attr / get / findAll / has) - so a caller can inspect or transform a document (pull the headings for a table of contents, rewrite links, convert to another format) before rendering it. The returned node is the document root (its typeOf is "document"); its children are the top-level blocks. A source over 4194304 characters, a document whose blocks or inline markup nest too deep, or one that holds over 200000 nodes (counted while it is parsed) is a catchable "limit" error.
md- the Markdown source
- Returns
doctree.Node - the document root node
style
markdown.style(kind as string, text as string)Wrap text in an inline emphasis span.
kind- the emphasis, "bold" / "italic" / "code"
text- the text to wrap
- Returns
string - the emphasised Markdown span
- Throws
Error - when kind is not "bold" / "italic" / "code"
table
markdown.table(
headings as list of string,
aligns as list of string,
rows as list of list of string)Render a GFM table. Columns follow headings: a short row is padded with empty cells, extra cells are dropped. Pipes and newlines in a cell are made safe.
headings- the column headings
aligns- the per-column alignment ("left" / "right" / "center" / "none";
[]for all-default) rows- the data rows, each a list of cell strings
- Returns
string - the GFM table source
- Throws
Error - when an align value is not "left" / "right" / "center" / "none"
tablePretty
markdown.tablePretty(md as string)Reformat every GFM table in Markdown text so its source columns line up (padded cells, aligned delimiters), leaving all other lines exactly as written. The handcraft-then-prettify workflow, in one call.
md- the Markdown source
- Returns
string - the source with its tables aligned
toAnsi
markdown.toAnsi(md as string)Render Markdown to styled terminal text, blocks separated by a blank line.
md- the Markdown source
- Returns
string - the rendered terminal text
toHtml
markdown.toHtml(md as string)Render Markdown to an HTML string (block elements concatenated, no indentation). Safe by default: raw HTML in the source (an HTML block or an inline tag) is escaped and rendered as literal text, so passing untrusted Markdown (a README, a user comment) cannot inject a <script> or an event-handler attribute. Everything the renderer builds is already escaped (text, link URLs via an allow-list, image attributes). To pass raw HTML through for trusted input, use toHtmlWith with allowRawHtml: true.
md- the Markdown source
- Returns
string - the rendered HTML
toHtmlWith
markdown.toHtmlWith(md as string, opts as HtmlOptions)Render Markdown to HTML with explicit options. The only option today is allowRawHtml: set it true to emit raw HTML (blocks and inline tags) verbatim for trusted Markdown, or false (the default toHtml behavior) to escape it.
md- the Markdown source
opts- the render options
- Returns
string - the rendered HTML
toPdf
markdown.toPdf(md as string)Render a Markdown string to PDF bytes with the default options.
md- the Markdown source
- Returns
bytes - the PDF document
toPdfWith
markdown.toPdfWith(md as string, opts as PdfOptions)Render a Markdown string to PDF bytes with the given options.
md- the Markdown source
opts- the page geometry and fonts
- Returns
bytes - the PDF document
toXhtml
markdown.toXhtml(md as string)Render Markdown to a well-formed XHTML string: void elements self-close (<hr />) and boolean attributes expand, so the output is an XHTML / EPUB content document. Raw HTML in the source is escaped, exactly as toHtml; otherwise identical.
md- the Markdown source
- Returns
string - the rendered XHTML
Structs
HtmlOptions
Options for HTML rendering.
def struct HtmlOptions {
allowRawHtml as bool
};allowRawHtml- pass raw HTML blocks through verbatim (trusted input only). Default (zero value) is false: raw HTML is escaped, so untrusted Markdown cannot inject scripts or event handlers.