Skip to content
Jennifer Programming Language

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

NameWhat it is
bulletsRender an unordered list, one - item per line.
codeBlockRender a fenced code block around verbatim text.
headerRender an ATX heading.
linkRender an inline link [text](url).
numberedRender an ordered list, `1.
parseParse Markdown into a document tree, walked by the reader accessors (typeOf / children / text /...
styleWrap text in an inline emphasis span.
tableRender a GFM table.
tablePrettyReformat every GFM table in Markdown text so its source columns line up (padded cells, aligned delimiters),...
toAnsiRender Markdown to styled terminal text, blocks separated by a blank line.
toHtmlRender Markdown to an HTML string (block elements concatenated, no indentation).
toHtmlWithRender Markdown to HTML with explicit options.
toPdfRender a Markdown string to PDF bytes with the default options.
toPdfWithRender a Markdown string to PDF bytes with the given options.
toXhtmlRender Markdown to a well-formed XHTML string: void elements self-close (<hr />) and boolean attributes e...
HtmlOptionsstruct: Options for HTML rendering.

Functions

bullets

jennifer
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

jennifer
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

jennifer
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"
jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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.