Standard library · Encoding and data

encoding.markdown

A Markdown parser and HTML renderer: CommonMark 0.31, GitHub Flavored Markdown and the common extensions.

import encoding.markdown

Markdown is the plain text format behind READMEs, wikis, blogs and notes. This package reads it into a tree and writes HTML from the tree. It is written in Salam, needs nothing else, and is checked against the official test suites: all 652 examples of the CommonMark 0.31.2 specification and all the extension examples of the GFM specification give exactly the HTML the specifications show.

"Markdown" is really a family of dialects, so Options says which one to read. The presets below are the usual choices.

PresetWhat it reads
CommonMarkThe CommonMark 0.31.2 specification, raw HTML allowed. This is also what the 2004 Markdown of John Gruber means once its open cases are settled.
OriginalThe same core with no extensions, for text written for Markdown.pl. The bugs of that script are not copied, so a few odd corners render the CommonMark way.
GFMGitHub Flavored Markdown 0.29: tables, strikethrough, task lists, extended autolinks, tag filter.
GitHubGFM plus footnotes, alerts, heading ids, math and emoji, as github.com shows them.
ExtraPHP Markdown Extra: tables, footnotes, definition lists, abbreviations, attributes, heading ids.
PandocThe common part of Pandoc Markdown: smart punctuation, front matter, math, sub and superscript, definition lists, footnotes, attributes, fenced divs.
FullEvery extension of this package at once, raw HTML off.
DefaultOptionsA friendly default for user text: GFM style extensions, footnotes, alerts, math and emoji, raw HTML off.
ArticleFor blog posts and lessons: front matter, attributes, heading ids, tables, task lists, strikethrough, autolinks, footnotes and alerts, raw HTML off.

Besides CommonMark itself, these are supported and each can be switched on its own: tables, strikethrough, task lists, extended autolinks, tag filter, footnotes (with inline notes and several references to one note), definition lists, abbreviations, heading ids, attributes {#id .class key=value}, math, highlight ==x==, insert ++x++, subscript, superscript, emoji shortcodes, alert callouts, fenced divs :::, wiki links, YAML and TOML front matter and smart punctuation.

Markdown to HTML in one call

ToHtml turns text into HTML. It uses DefaultOptions, which leaves raw HTML out, so it is safe to call on text typed by users. The heading got an id, the checkbox and the footnote came from the extensions.

encoding-markdown-guide-basics.salam
import encoding.markdown

func main:
    src := "# Release notes\n\nVersion **2.0** is *out*. It adds:\n\n- tables\n- task lists\n- [x] footnotes[^1]\n\n[^1]: Yes, really.\n"
    print markdown.ToHtml(src)
end
Output
<h1 id="release-notes">Release notes</h1>
<p>Version <strong>2.0</strong> is <em>out</em>. It adds:</p>
<ul>
<li>tables</li>
<li>task lists</li>
<li><input checked="" disabled="" type="checkbox"> footnotes<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup></li>
</ul>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>Yes, really. <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

CommonMark: the core

ToHtmlWith takes the options as a second argument. The CommonMark preset gives the standard rules: setext and ATX headings, reference links, loose and tight lists, block quotes, indented code, hard breaks, entities and raw HTML blocks.

encoding-markdown-guide-commonmark.salam
import encoding.markdown

func show(src: str):
    print markdown.ToHtmlWith(src, markdown.CommonMark())
    println "--"
end

func main:
    show("Title\n=====\n\nSee [the guide][g] or <https://example.com>.\n\n[g]: /guide \"The guide\"\n")
    show("1. one\n2. two\n\n   still two\n3. three\n")
    show("> quoted *text*\n> over two lines\n\n    indented code\n")
    show("hard  \nbreak and a backslash\\\nbreak, &copy; and \\*escaped\\*\n")
    show("<div>\n*raw html block*\n</div>\n")
end
Output
<h1>Title</h1>
<p>See <a href="/guide" title="The guide">the guide</a> or <a href="https://example.com">https://example.com</a>.</p>
--
<ol>
<li>
<p>one</p>
</li>
<li>
<p>two</p>
<p>still two</p>
</li>
<li>
<p>three</p>
</li>
</ol>
--
<blockquote>
<p>quoted <em>text</em>
over two lines</p>
</blockquote>
<pre><code>indented code
</code></pre>
--
<p>hard<br />
break and a backslash<br />
break, © and *escaped*</p>
--
<div>
*raw html block*
</div>
--

GitHub Flavored Markdown

Tables take their alignment from the colons in the second row, rows with too few cells are padded and extra cells dropped, and \| puts a pipe in a cell. Task lists, strikethrough and bare addresses become checkboxes, del elements and links.

encoding-markdown-guide-gfm.salam
import encoding.markdown

func main:
    src := "| Name | Qty | Price |\n|:-----|----:|:-----:|\n| Tea  | 2   | 3.50  |\n| Milk | 10  | 1.20  |\n\n~~old price~~ new price\n\n- [x] shipped\n- [ ] paid\n\nDocs live at www.example.com/docs and https://example.org.\n"
    print markdown.ToHtmlWith(src, markdown.GFM())
end
Output
<table>
<thead>
<tr>
<th align="left">Name</th>
<th align="right">Qty</th>
<th align="center">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Tea</td>
<td align="right">2</td>
<td align="center">3.50</td>
</tr>
<tr>
<td align="left">Milk</td>
<td align="right">10</td>
<td align="center">1.20</td>
</tr>
</tbody>
</table>
<p><del>old price</del> new price</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> shipped</li>
<li><input disabled="" type="checkbox"> paid</li>
</ul>
<p>Docs live at <a href="http://www.example.com/docs">www.example.com/docs</a> and <a href="https://example.org">https://example.org</a>.</p>

Footnotes, definitions, math and more

The Full preset turns everything on. Footnote definitions can be several paragraphs long and are collected at the end of the page with a back link for each reference. An alert is a block quote that starts with [!NOTE], [!TIP], [!IMPORTANT], [!WARNING] or [!CAUTION]. Math is only marked up as \(...\) and \[...\] inside a span or div with the class math, so KaTeX or MathJax on the page can draw it.

encoding-markdown-guide-extensions.salam
import encoding.markdown

func main:
    src := "Salam[^note] is typed.\n\n[^note]: A compiled language.\n\nTerm\n: Its definition\n\n*[SQL]: Structured Query Language\n\nUse SQL, or ==highlight==, H~2~O, x^2^ and :rocket:.\n\n> [!TIP]\n> Start small.\n\n## Install {#install .anchor}\n\nEuler: $e^{i\\pi} + 1 = 0$\n"
    print markdown.ToHtmlWith(src, markdown.Full())
end
Output
<p>Salam<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup> is typed.</p>
<dl>
<dt>Term</dt>
<dd>Its definition</dd>
</dl>
<p>Use <abbr title="Structured Query Language">SQL</abbr>, or <mark>highlight</mark>, H<sub>2</sub>O, x<sup>2</sup> and 🚀.</p>
<div class="markdown-alert markdown-alert-tip">
<p class="markdown-alert-title">Tip</p>
<p>Start small.</p>
</div>
<h2 id="install" class="anchor">Install</h2>
<p>Euler: <span class="math inline">\(e^{i\pi} + 1 = 0\)</span></p>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>A compiled language. <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

The same text under different presets

A dialect decides what the characters mean. The text below has strikethrough, a bare address, a task and a footnote; each preset reads it differently, and CommonMark leaves all of it as written.

encoding-markdown-guide-presets.salam
import encoding.markdown

func main:
    src := "~~old~~ www.example.com\n\n- [x] done\n\nNote[^1]\n\n[^1]: text\n"
    println "CommonMark:"
    print markdown.ToHtmlWith(src, markdown.CommonMark())
    println "GFM:"
    print markdown.ToHtmlWith(src, markdown.GFM())
    println "GitHub:"
    print markdown.ToHtmlWith(src, markdown.GitHub())
end
Output
CommonMark:
<p>~~old~~ www.example.com</p>
<ul>
<li>[x] done</li>
</ul>
<p>Note<a href="text">^1</a></p>
GFM:
<p><del>old</del> <a href="http://www.example.com">www.example.com</a></p>
<ul>
<li><input checked="" disabled="" type="checkbox"> done</li>
</ul>
<p>Note<a href="text">^1</a></p>
GitHub:
<p><del>old</del> <a href="http://www.example.com">www.example.com</a></p>
<ul>
<li><input checked="" disabled="" type="checkbox"> done</li>
</ul>
<p>Note<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup></p>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>text <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

Text from users

Markdown allows raw HTML, so a comment box that renders Markdown is a place for script injection. Keep unsafe_html off (the default of ToHtml, DefaultOptions, Full and Safe): raw tags become a comment, and links to javascript:, vbscript:, file: and data: (except small images) get an empty address. Nesting is limited, so a hostile text of a hundred thousand brackets or quotes cannot overflow the stack, and the time is linear in the size of the text.

encoding-markdown-guide-safe.salam
import encoding.markdown

func main:
    untrusted := "Hello <script>alert(1)</script> [click](javascript:alert(2)) <b>bold</b>\n"
    println "safe:"
    print markdown.ToHtml(untrusted)
    println "unsafe:"
    print markdown.ToHtmlWith(untrusted, markdown.GitHub())
    println "still hostile-input proof:"
    big := markdown.ToHtml("[[[[[[[[[[ ***a*** ![![![![")
    println big.Len() > 0
end
Output
safe:
<p>Hello <!-- raw HTML omitted -->alert(1)<!-- raw HTML omitted --> <a href="">click</a> <!-- raw HTML omitted -->bold<!-- raw HTML omitted --></p>
unsafe:
<p>Hello &lt;script>alert(1)&lt;/script> <a href="javascript:alert(2)">click</a> <b>bold</b></p>
still hostile-input proof:
true

The presets CommonMark, Original, GFM, GitHub, Extra and Pandoc allow raw HTML because the specifications they follow do. For untrusted text wrap them in Safe.

Blocks for a server rendered page

A server function can only send plain data to a page: numbers, booleans, text, structs and vectors. A Document is not that, so ToBlocks parses the text and returns a Vector<Block>. The list is flat: a heading, paragraph, code block or table cell is one Block, its inline text is a list of Span runs, and the content of a quote, list, item or table follows it with a larger depth. The front matter at the top of a post is the first block, with its keys in fields. With the default Article options raw HTML is left out and dangerous addresses are emptied.

encoding-markdown-guide-blocks.salam
import encoding.markdown
import encoding.json

func main:
    src := "---\ntitle: A first post\ndate: 2026-11-11\n---\n## Setup {#setup}\n\nRead the **[guide](/learn/)** first.\n"
    blocks := markdown.ToBlocks(src)
    println markdown.FrontMatterValue(blocks, "title")
    each b in blocks:
        println b.kind, b.level, "#" + b.id
        each s in b.spans:
            println "  span:", s.kind, s.address, "[" + s.text + "]"
        end
    end
    mut copy := Vector {} as Vector<markdown.Block>
    try:
        json.Unmarshal(json.Marshal(blocks), copy)
        println "over the wire:", copy.Len(), "blocks"
    catch e:
        println e.message
    end
    markdown.FreeBlocks(blocks)
end
Output
A first post
front_matter 0 #
heading 2 #setup
  span: text  [Setup]
paragraph 0 #
  span: text  [Read the ]
  span: link /learn/ [guide]
  span: text  [ first.]
over the wire: 3 blocks

Walking the tree

Parse gives a Document. Root, FirstChild, NextSibling and Kind walk it, Headings and Toc build a table of contents, FrontMatter returns the header block and ToText returns what the reader sees. Free the document with Free and the list of headings with FreeHeadings.

encoding-markdown-guide-tree.salam
import encoding.markdown

func main:
    src := "---\ntitle: Guide\n---\n# Intro\n\nText.\n\n## Setup\n\n### Linux\n\n## Usage\n"
    mut o := markdown.DefaultOptions()
    o.frontmatter = true
    d := markdown.Parse(src, o)
    println "front matter:", markdown.FrontMatter(d)
    hs := markdown.Headings(d)
    repeat hs.Len() in i:
        h := hs.Get(i)
        println h.level, h.id, h.text
    end
    print markdown.Toc(d, 2, 3)
    println markdown.ToText(d)
    markdown.FreeHeadings(hs)
    markdown.Free(d)
end
Output
front matter: title: Guide

1 intro Intro
2 setup Setup
3 linux Linux
2 usage Usage
<ul>
<li><a href="#setup">Setup</a>
<ul>
<li><a href="#linux">Linux</a></li>
</ul>
</li>
<li><a href="#usage">Usage</a></li>
</ul>
Intro
Text.
Setup
Linux
Usage

What people get wrong:

•

Feeding user text to CommonMark() or GitHub() and sending the result to a page. Those allow raw HTML; use ToHtml or Safe.

•

Expecting the same HTML as another library byte for byte. The text of each element is the same, but the layout of newlines and attributes follows the specifications, and extensions are not standardised.

•

Forgetting that the extension switches are separate: footnotes, tables and the rest are off in CommonMark(), and [^1] is then an ordinary reference link.

•

Reading from a Document after Free.

To escape plain text for HTML use encoding.html; for page templates see template.

Types

Span

#
struct Span

A run of inline text inside a heading, paragraph, term or table cell.

NameTypeDescription
kindstr = ""text, emph, strong, strike, code, link, image, break, footnote, math, display_math, html, highlight, insert, subscript, superscript or abbr. A text inside a link is always a link; the flags below keep the other styles.
textstr = ""The visible text; for an image its alternative text, for a footnote its number.
addressstr = ""Where a link goes, the source of an image, or #fn-1 for a footnote. A formula inside a link has the link address too. Empty for a dangerous address such as javascript: unless raw HTML is on.
titlestr = ""The title of a link or image, or the long form of an abbreviation.
linkstr = ""For an image inside a link, the address of that link.
strongbool = falseThe run is bold.
emphbool = falseThe run is italic.
strikebool = falseThe run is struck through.
codebool = falseThe run is inline code.
encoding-markdown-ToBlocks.salam
import encoding.markdown
import encoding.json

func main:
    src := "---\ntitle: A first post\ndate: 2026-11-11\n---\n## Setup {#setup}\n\nRead the **[guide](/learn/)** first.\n"
    blocks := markdown.ToBlocks(src)
    println markdown.FrontMatterValue(blocks, "title")
    each b in blocks:
        println b.kind, b.level, "#" + b.id
        each s in b.spans:
            println "  span:", s.kind, s.address, "[" + s.text + "]"
        end
    end
    mut copy := Vector {} as Vector<markdown.Block>
    try:
        json.Unmarshal(json.Marshal(blocks), copy)
        println "over the wire:", copy.Len(), "blocks"
    catch e:
        println e.message
    end
    markdown.FreeBlocks(blocks)
end
Output
A first post
front_matter 0 #
heading 2 #setup
  span: text  [Setup]
paragraph 0 #
  span: text  [Read the ]
  span: link /learn/ [guide]
  span: text  [ first.]
over the wire: 3 blocks

Field

#
struct Field

One top level key and value of the front matter.

NameTypeDescription
keystr = ""The key.
valuestr = ""The value on the same line, without surrounding quotes. Nested YAML lists and maps are not expanded and give an empty value.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

Block

#
struct Block

One block of a document. Nesting is given by depth: the blocks inside a container follow it with a larger depth.

NameTypeDescription
kindstr = ""front_matter, heading, paragraph, image, code, quote, alert, list, item, table, row, cell, rule, math, html, container, definition_list, term, description or footnote.
depthint = 0How many containers the block is inside; 0 at the top.
levelint = 0The level of a heading, 1 to 6.
idstr = ""The anchor of a heading, from {#id} or made from its text; fn-1 and so on for a footnote.
namestr = ""The type of an alert (note, tip, important, warning, caution) or the class of a fenced div.
languagestr = ""The language of a code block; yaml or toml for front matter.
textstr = ""The plain text of a heading, paragraph, term or cell; the source of a code, math or html block; the raw front matter.
addressstr = ""The source of an image block, a paragraph that holds only an image.
spansVector<Span>The inline runs of a heading, paragraph, image, term or cell.
fieldsVector<Field>The keys and values of the front matter block.
orderedbool = falseThe list or item is numbered.
numberint = 0The first number of an ordered list, the number of an ordered item or of a footnote.
tightbool = falseThe list has no blank lines between its items, so their paragraphs are shown without space.
taskbool = falseThe item is a task list item.
checkedbool = falseThe task is done.
headerbool = falseThe row or cell belongs to the table header.
alignstr = ""left, center, right or empty, for a table cell.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

Options

#
struct Options

Everything that decides how Markdown is read and written. Start from a preset (DefaultOptions, CommonMark, GFM, GitHub, Extra, Pandoc, Full) and switch single fields.

NameTypeDescription
unsafe_htmlbool = falsePass raw HTML in the text through to the output. Off by default so text from users cannot inject markup.
hardbreaksbool = falseRender every line break in a paragraph as a br tag.
nobreaksbool = falseRender soft line breaks as a space.
smartbool = falseCurly quotes, en and em dashes and ellipses.
tablesbool = falseGFM pipe tables.
strikethroughbool = false~~text~~ becomes a del element.
tasklistsbool = false- [ ] and - [x] items become checkboxes.
autolinksbool = falseBare www. addresses, http(s):// and ftp:// addresses and email addresses become links.
tagfilterbool = falseEscape the dangerous tags the GFM specification lists (script, style, iframe and a few more) in raw HTML.
footnotesbool = false[^label] references, [^label]: definitions and ^[inline notes].
deflistsbool = falseTerm lines followed by : definition lines.
heading_idsbool = falseGive every heading a GitHub style id, made unique with -1, -2 and so on.
mathbool = false$inline$, $$display$$ and $$ blocks.
highlightbool = false==text== becomes a mark element.
insertbool = false++text++ becomes an ins element.
subscriptbool = false~text~ becomes a sub element.
superscriptbool = false^text^ becomes a sup element.
emojibool = false:name: shortcodes become emoji.
alertsbool = false> [!NOTE] style callouts.
attributesbool = false{#id .class key=value} after headings, links, images and code fence info strings.
frontmatterbool = falseRead a --- (YAML) or +++ (TOML) block at the very top into FrontMatter instead of the page.
abbreviationsbool = false*[ABBR]: title definitions.
containersbool = false::: name fenced divs.
wikilinksbool = false[[Page]] and [[Page|label]] links.
code_class_prefixstr = "language-"Prefix of the class given to fenced code, language- by default.
id_prefixstr = ""Text put in front of every generated heading id.
encoding-markdown-Options.salam
import encoding.markdown

func main:
    src := "~~old~~ www.example.com\n\n- [x] done\n\nNote[^1]\n\n[^1]: text\n"
    println "CommonMark:"
    print markdown.ToHtmlWith(src, markdown.CommonMark())
    println "GFM:"
    print markdown.ToHtmlWith(src, markdown.GFM())
    println "GitHub:"
    print markdown.ToHtmlWith(src, markdown.GitHub())
end
Output
CommonMark:
<p>~~old~~ www.example.com</p>
<ul>
<li>[x] done</li>
</ul>
<p>Note<a href="text">^1</a></p>
GFM:
<p><del>old</del> <a href="http://www.example.com">www.example.com</a></p>
<ul>
<li><input checked="" disabled="" type="checkbox"> done</li>
</ul>
<p>Note<a href="text">^1</a></p>
GitHub:
<p><del>old</del> <a href="http://www.example.com">www.example.com</a></p>
<ul>
<li><input checked="" disabled="" type="checkbox"> done</li>
</ul>
<p>Note<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup></p>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>text <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

Document

#
struct Document

A parsed Markdown document: a tree of nodes stored in flat arrays. Read it with the accessor functions, not the fields.

NameTypeDescription
kindVector<int>Internal storage of the document tree; use the accessor functions instead.
parentVector<int>Internal storage of the document tree; use the accessor functions instead.
fstVector<int>Internal storage of the document tree; use the accessor functions instead.
lstVector<int>Internal storage of the document tree; use the accessor functions instead.
nxtVector<int>Internal storage of the document tree; use the accessor functions instead.
prvVector<int>Internal storage of the document tree; use the accessor functions instead.
stVector<int>Internal storage of the document tree; use the accessor functions instead.
numVector<int>Internal storage of the document tree; use the accessor functions instead.
n2Vector<int>Internal storage of the document tree; use the accessor functions instead.
n3Vector<int>Internal storage of the document tree; use the accessor functions instead.
n4Vector<int>Internal storage of the document tree; use the accessor functions instead.
lsVector<int>Internal storage of the document tree; use the accessor functions instead.
lcVector<int>Internal storage of the document tree; use the accessor functions instead.
depVector<int>Internal storage of the document tree; use the accessor functions instead.
hgtVector<int>Internal storage of the document tree; use the accessor functions instead.
s1Vector<str>Internal storage of the document tree; use the accessor functions instead.
s2Vector<str>Internal storage of the document tree; use the accessor functions instead.
s3Vector<str>Internal storage of the document tree; use the accessor functions instead.
piecesVector<str>Internal storage of the document tree; use the accessor functions instead.
ownVector<str>Internal storage of the document tree; use the accessor functions instead.
ref_destHashMap<str, str>Internal storage of the document tree; use the accessor functions instead.
ref_titleHashMap<str, str>Internal storage of the document tree; use the accessor functions instead.
fn_defHashMap<str, int>Internal storage of the document tree; use the accessor functions instead.
fn_numHashMap<str, int>Internal storage of the document tree; use the accessor functions instead.
fn_usesHashMap<str, int>Internal storage of the document tree; use the accessor functions instead.
fn_orderVector<str>Internal storage of the document tree; use the accessor functions instead.
abbr_keyVector<str>Internal storage of the document tree; use the accessor functions instead.
abbr_valVector<str>Internal storage of the document tree; use the accessor functions instead.
metastrInternal storage of the document tree; use the accessor functions instead.
meta_kindstrInternal storage of the document tree; use the accessor functions instead.
rootintInternal storage of the document tree; use the accessor functions instead.
optsOptionsInternal storage of the document tree; use the accessor functions instead.
encoding-markdown-Parse.salam
import encoding.markdown

func main:
    src := "---\ntitle: Guide\n---\n# Intro\n\nText.\n\n## Setup\n\n### Linux\n\n## Usage\n"
    mut o := markdown.DefaultOptions()
    o.frontmatter = true
    d := markdown.Parse(src, o)
    println "front matter:", markdown.FrontMatter(d)
    hs := markdown.Headings(d)
    repeat hs.Len() in i:
        h := hs.Get(i)
        println h.level, h.id, h.text
    end
    print markdown.Toc(d, 2, 3)
    println markdown.ToText(d)
    markdown.FreeHeadings(hs)
    markdown.Free(d)
end
Output
front matter: title: Guide

1 intro Intro
2 setup Setup
3 linux Linux
2 usage Usage
<ul>
<li><a href="#setup">Setup</a>
<ul>
<li><a href="#linux">Linux</a></li>
</ul>
</li>
<li><a href="#usage">Usage</a></li>
</ul>
Intro
Text.
Setup
Linux
Usage

Heading

#
struct Heading

One heading of a document.

NameTypeDescription
levelint = 0Heading level from 1 to 6.
textstr = ""The plain text of the heading.
idstr = ""The id the heading gets in the HTML.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Constants and variables

kind_block_quote

#
const kind_block_quote: i32 = 1

Node kind: blockquote.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_paragraph

#
const kind_paragraph: i32 = 4

Node kind: paragraph.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_thematic_break

#
const kind_thematic_break: i32 = 6

Node kind: thematic.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_code_block

#
const kind_code_block: i32 = 7

Node kind: codeblock.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_html_block

#
const kind_html_block: i32 = 8

Node kind: htmlblock.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_table_row

#
const kind_table_row: i32 = 10

Node kind: tablerow.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_table_cell

#
const kind_table_cell: i32 = 11

Node kind: tablecell.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_footnote_def

#
const kind_footnote_def: i32 = 12

Node kind: footnotedef.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_math_block

#
const kind_math_block: i32 = 16

Node kind: mathblock.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_container

#
const kind_container: i32 = 17

Node kind: container.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_soft_break

#
const kind_soft_break: i32 = 31

Node kind: softbreak.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_line_break

#
const kind_line_break: i32 = 32

Node kind: linebreak.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_html_inline

#
const kind_html_inline: i32 = 34

Node kind: htmlinline.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_math_inline

#
const kind_math_inline: i32 = 41

Node kind: mathinline.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_highlight

#
const kind_highlight: i32 = 42

Node kind: highlight.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_inline_note

#
const kind_inline_note: i32 = 46

Node kind: inlinenote.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

kind_wiki_link

#
const kind_wiki_link: i32 = 47

Node kind: wikilink.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Functions

Article

#
func Article(): Options

The options for a blog post or a lesson: front matter, {#id} attributes, heading ids, tables, strikethrough, task lists, autolinks, footnotes and alerts, with raw HTML turned off. ToBlocks uses these.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

Blocks

#
func Blocks(d: Document): Vector<Block>

Turns a parsed Document into a flat list of plain structs that can be returned by a server function. The list owns copies of every text, so the Document can be freed at once. Release the list with FreeBlocks.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

ToBlocksWith

#
func ToBlocksWith(src: str, opts: Options): Vector<Block>

Parses Markdown with the options you give and returns its blocks.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

ToBlocks

#
func ToBlocks(src: str): Vector<Block>

Parses Markdown with the Article options and returns its blocks.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

FrontMatterValue

#
func FrontMatterValue(blocks: Vector<Block>, key: str): str

The value of one top level key of the front matter, or an empty text when there is no such key.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

FreeBlocks

#
func FreeBlocks(blocks: Vector<Block>)

Releases a list returned by Blocks, ToBlocks or ToBlocksWith, with every text in it.

The example for this item is shown with the ones above: encoding-markdown-ToBlocks.salam

DefaultOptions

#
func DefaultOptions(): Options

The options most programs want: CommonMark plus tables, strikethrough, task lists, autolinks, footnotes, heading ids, alerts, math and emoji, with raw HTML turned off.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

CommonMark

#
func CommonMark(): Options

Strict CommonMark 0.31.2 with raw HTML allowed, exactly what the specification and its 652 examples describe.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Original

#
func Original(): Options

The original Markdown of John Gruber: the CommonMark core with raw HTML allowed and no extensions. Details where the old Markdown.pl differs are in the guide.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

GFM

#
func GFM(): Options

GitHub Flavored Markdown as the GFM specification defines it: CommonMark plus tables, strikethrough, task lists, extended autolinks and the tag filter.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

GitHub

#
func GitHub(): Options

GFM plus what github.com adds on top: footnotes, alerts, heading ids, math and emoji.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Extra

#
func Extra(): Options

PHP Markdown Extra: tables, footnotes, definition lists, abbreviations, attributes and heading ids.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Pandoc

#
func Pandoc(): Options

The commonly used part of Pandoc Markdown: smart punctuation, front matter, math, sub and superscript, definition lists, footnotes, attributes and fenced divs.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Full

#
func Full(): Options

Every extension switched on (and raw HTML off). Useful for notes and documentation tools.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Safe

#
func Safe(o: Options): Options

Returns a copy of the options with raw HTML turned off, so a preset can be made safe for untrusted text.

The example for this item is shown with the ones above: encoding-markdown-Options.salam

Parse

#
func Parse(src: str, opts: Options): Document

Parses Markdown into a Document tree. Reference links, footnotes, abbreviations and front matter are resolved. Free the document with Free when done.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Free

#
func Free(d: Document)

Releases a Document and everything it owns.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Render

#
func Render(d: Document): str

Renders a parsed Document as HTML. Free the returned text.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

ToHtmlWith

#
func ToHtmlWith(src: str, opts: Options): str

Parses and renders in one call with the options you give.

encoding-markdown-ToHtml.salam
import encoding.markdown

func main:
    src := "# Release notes\n\nVersion **2.0** is *out*. It adds:\n\n- tables\n- task lists\n- [x] footnotes[^1]\n\n[^1]: Yes, really.\n"
    print markdown.ToHtml(src)
end
Output
<h1 id="release-notes">Release notes</h1>
<p>Version <strong>2.0</strong> is <em>out</em>. It adds:</p>
<ul>
<li>tables</li>
<li>task lists</li>
<li><input checked="" disabled="" type="checkbox"> footnotes<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup></li>
</ul>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>Yes, really. <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

ToHtml

#
func ToHtml(src: str): str

Converts Markdown to HTML with DefaultOptions, which keeps raw HTML out, so it is a safe default for text from users.

The example for this item is shown with the ones above: encoding-markdown-ToHtml.salam

FrontMatter

#
func FrontMatter(d: Document): str

The raw text between the --- or +++ lines at the very top of the document. Empty when there is none or the frontmatter option is off.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

FrontMatterKind

#
func FrontMatterKind(d: Document): str

"yaml" for --- front matter, "toml" for +++, empty otherwise.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Headings

#
func Headings(d: Document): Vector<Heading>

Lists the headings of a document with their level, plain text and id. Release it with FreeHeadings.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

FreeHeadings

#
func FreeHeadings(h: Vector<Heading>)

Releases the list returned by Headings.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Toc

#
func Toc(d: Document, minlevel: int, maxlevel: int): str

Builds a nested HTML list of links to the headings between two levels, for a table of contents.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

ToText

#
func ToText(d: Document): str

The document as plain text: no markup, one block per line.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

StripMarkdown

#
func StripMarkdown(src: str): str

Removes the Markdown from a text and returns what a reader sees, for search indexes and previews.

The example for this item is shown with the ones above: encoding-markdown-ToHtml.salam

Dump

#
func Dump(d: Document): str

The tree as indented text, one node per line, for tests and debugging.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Root

#
func Root(d: Document): int

The id of the root node of a document.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Kind

#
func Kind(d: Document, nd: int): int

The kind of a node, one of the kind_ constants.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

FirstChild

#
func FirstChild(d: Document, nd: int): int

The first child of a node, or -1.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

NextSibling

#
func NextSibling(d: Document, nd: int): int

The next node with the same parent, or -1.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

ParentOf

#
func ParentOf(d: Document, nd: int): int

The parent of a node, or -1 for the root.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Literal

#
func Literal(d: Document, nd: int): str

The text of a text, code, HTML, code block or math node.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Destination

#
func Destination(d: Document, nd: int): str

The address of a link or image node.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Title

#
func Title(d: Document, nd: int): str

The title of a link or image node, or empty.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam

Level

#
func Level(d: Document, nd: int): int

The level of a heading node, or the start number of a list.

The example for this item is shown with the ones above: encoding-markdown-Parse.salam