(text markdown)

Reference

Overview

Markdown: CommonMark 0.31.2 and the extensions of GitHub Flavored Markdown, read into a tree and written as HTML.

(import (text markdown))

(def doc (markdown-parse "# Title\n\nSome *text*.\n"))
(record-ref doc blocks)
;; [(MdHeading 1 [(MdText "Title")]) (MdParagraph [(MdText "Some ") (MdEmph [(MdText "text")]) (MdText ".")])]
(markdown->html doc)
;; "<h1>Title</h1>\n<p>Some <em>text</em>.</p>\n"

markdown-parse reads a document into an MdDocument: a tree of blocks (MdBlock) that contain inlines (MdInline). markdown->html writes the tree as HTML. A program that writes another format, or that reads the text of a document, uses the tree.

Extensions. These extensions are on by default. A keyword of markdown-parse turns each one off:

With all extensions off, the parser obeys CommonMark 0.31.2. All 652 examples of the spec give the spec’s HTML. With all extensions on and with #:tag-filter, all the extension examples of the GitHub Flavored Markdown spec give that spec’s HTML.

Errors. All text is a Markdown document, so markdown-parse does not fail. Text that does not make a construct is text: an unclosed * is a *.

Raw HTML. HTML in the document goes into the output as it is. Do not write the HTML of an untrusted document into a page without a sanitizer. #:tag-filter disables only some tags, such as <script>. It does not make the HTML safe.

See also: markdown-parse, markdown->html, MdDocument

Reference

Types

Functions

Types

MdDocument

record ↑ Types

A parsed document: its blocks, its footnotes and its front matter.

blocks
The blocks of the document, in order.
footnotes
The footnotes that have a reference, each with its number. The numbers are 1, 2, ..., in the order of the first reference to each footnote. A footnote without a reference is not in the list.
front-matter
The text between the lines of ---, without them, or None when the document does not start with front matter.

See also: markdown-parse, MdBlock

MdBlock

union ↑ Types

A block of a document.

MdParagraph
A paragraph, with its inlines.
MdHeading
A heading: its level, 1 to 6, and its inlines. An ATX heading (# text) and a setext heading (text with a line of = or - under it) are the same.
MdCodeBlock
A code block: its info string and its code. The info string is the text after the opening fence, or "" for none and for an indented code block. The code ends with a newline when it is not empty.
MdQuote
A block quote, with its blocks.
MdListBlock
A list.
MdRule
A thematic break: ***, --- or ___.
MdHtmlBlock
A block of raw HTML, as it is written.
MdTableBlock
A table.

See also: MdInline, MdList, MdTable

MdList

record ↑ Types

A list and its items.

ordered
#t for a numbered list, #f for a bullet list.
start
The number of the first item of a numbered list. 1 for a bullet list.
loose
#t when a blank line is between items, or between two blocks of an item. The paragraphs of a tight list's items have no <p> in HTML.
items
The items. Each item is a vec of blocks.

MdTable

record ↑ Types

A table. Each row has one cell for each column.

aligns
The alignment of each column, from the delimiter row.
head
The header row: the inlines of each cell.
body
The body rows. A row with fewer cells gets empty cells, and a row with more cells loses the extra cells.

MdAlign

union ↑ Types

The alignment of a table column.

MdAlignNone
---.
MdAlignLeft
:--.
MdAlignCenter
:-:.
MdAlignRight
--:.

MdInline

union ↑ Types

An inline: a part of the text of a paragraph, a heading or a table cell.

MdText
Text, with its escapes and entities resolved.
MdCode
A code span, without its backticks.
MdEmph
Emphasis: *text* or _text_.
MdStrong
Strong emphasis: **text** or __text__.
MdStrike
Strikethrough: ~~text~~.
MdLink
A link: its destination, its title ("" for none) and its text. An autolink is a link whose text is its address.
MdImage
An image: its source, its title ("" for none) and its description.
MdSoftBreak
A line break in the source. HTML shows it as a space.
MdHardBreak
A hard line break: two spaces or a \ at the end of a line.
MdHtml
Raw HTML in the text, as it is written.
MdCheckbox
The box of a task list item: #t for [x], #f for [ ]. It is the first inline of the item's first paragraph, and the text after it starts with the space after the box.
MdFootnoteRef
A reference to the footnote with this number in (record-ref doc footnotes).

See also: inlines->plain-text

Functions

markdown-parse

function ↑ Functions

(: markdown-parse (-> string (#:tables bool) (#:task-lists bool) (#:strikethrough bool) (#:autolinks bool) (#:footnotes bool) (#:front-matter bool) MdDocument))
(markdown-parse text #:tables #t #:task-lists #t #:strikethrough #t
                       #:autolinks #t #:footnotes #t #:front-matter #t)

Reads a Markdown document into a tree.

text
The document. A NUL character becomes U+FFFD.
#:tables
Tables. The default is #t.
#:task-lists
Task list items, [ ] and [x]. The default is #t.
#:strikethrough
Strikethrough, ~~text~~. The default is #t.
#:autolinks
Links without <>: www., http://, https:// and e-mail addresses. The default is #t.
#:footnotes
Footnotes. The default is #t. When off, [^label]: text is a link reference definition.
#:front-matter
YAML front matter. The default is #t. When off, the first --- is a thematic break.
returns
The document. With all keywords #f, the document is read as CommonMark 0.31.2.
(record-ref (markdown-parse "- [x] done\n- ~~old~~\n") blocks)
;; [(MdListBlock (MdList (ordered #f) (start 1) (loose #f) (items [[(MdParagraph [(MdCheckbox #t) (MdText " done")])] [(MdParagraph [(MdStrike [(MdText "old")])])]])))]

(record-ref (markdown-parse "---\ntitle: Notes\n---\nText.\n") front-matter)
;; (Some "title: Notes")

See also: markdown->html, MdDocument

markdown->html

function ↑ Functions

(: markdown->html (-> MdDocument (#:tag-filter bool) string))
(markdown->html doc #:tag-filter #f)

Writes a document as HTML.

doc
The document.
#:tag-filter
When #t, the < of these tags in raw HTML becomes &lt;: title, textarea, style, xmp, iframe, noembed, noframes, script and plaintext. The default is #f.
returns
The HTML, as the CommonMark spec's examples write it. Each block starts on a new line.
(markdown->html (markdown-parse "A note[^1].\n\n[^1]: The note.\n"))
;; "<p>A note<sup class=\"footnote-ref\"><a href=\"#fn-1\" id=\"fnref-1\" data-footnote-ref>1</a></sup>.</p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-1\">\n<p>The note. <a href=\"#fnref-1\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"1\" aria-label=\"Back to reference 1\">↩</a></p>\n</li>\n</ol>\n</section>\n"

The footnotes are a <section> at the end of the output. Each footnote has a link back to each reference to it. The links go into the footnote’s last paragraph. When a footnote has more than one reference, the ids of the references after the first are fnref-N-2, fnref-N-3, …

See also: markdown-parse

inlines->plain-text

function ↑ Functions

(: inlines->plain-text (-> (Vec MdInline) string))
(inlines->plain-text xs)

The text of inlines, without their markup.

xs
The inlines.
returns
The text. A line break becomes a newline, a footnote reference becomes its number, and a checkbox becomes "".
(inlines->plain-text [(MdText "a ") (MdStrong [(MdCode "b")])])
;; "a b"

markdown->html uses it for the alt text of an image. Use it also for the text of a heading in a table of contents.