Module ztcl::markdown
ztcl::markdown reads Markdown and gives its HTML, its events, or its tree. The syntax is CommonMark 0.31.2 with the extensions of GitHub Flavored Markdown: tables, task lists, strike text, extended autolinks, and the filter of raw HTML tags. The reader passes each of the 652 examples of the CommonMark spec and each example of the GFM extensions. References lists the sources.
package require ztcl::markdown
puts [ztcl::markdown html "# Launch\n\nThe pad is *clear*.\n\n- \[x\] fuel\n- \[ \] crew\n"]
<h1>Launch</h1> <p>The pad is <em>clear</em>.</p> <ul> <li><input checked="" disabled="" type="checkbox"> fuel</li> <li><input disabled="" type="checkbox"> crew</li> </ul>
The module needs no capability, and its output depends only on its arguments.
Synopsis
package require ztcl::markdown
ztcl::markdown html TEXT ?-gfm VALUE? ?-html escape|keep? ?-ids BOOLEAN? ?-fence CMD? ?-link CMD? ?-heading CMD?
ztcl::markdown events TEXT ?-gfm VALUE? ?-positions chars|bytes|lines? ?-ids BOOLEAN?
ztcl::markdown tree TEXT ?-gfm VALUE? ?-positions chars|bytes|lines? ?-ids BOOLEAN?
Commands
html
Gives the HTML of TEXT in the form of the reference implementation of CommonMark. Raw HTML in the text goes out as text, and -html keep writes it as HTML.
| Option | Value |
|---|---|
-gfm |
1 (the default) for each GFM extension, 0 for plain CommonMark, or a list of the extensions to turn on: table, tasklist, strike, autolink, and tagfilter. |
-html |
escape (the default) or keep. |
-ids |
1 gives each heading an id: the slug of its text, with -1, -2, and so on after a slug that came before. The rule is the rule of the anchors of this site. |
-fence CMD |
Runs CMD with the info string and the text of each fenced code block. A result other than the empty string replaces the HTML of the block. |
-link CMD |
Runs CMD with the URL of each link and image. The result is the URL to write. |
-heading CMD |
Runs CMD with the level, the text, and the id of each heading. The result is the id of the heading. |
A hook is a command prefix. It runs in the interpreter during the render, with the compute units of the script, and an error of a hook is the error of html.
package require ztcl::markdown
proc diagram {info text} {
if {$info ne "diagram"} { return "" }
return "<figure>[string length $text] bytes</figure>"
}
puts [ztcl::markdown html "```diagram\nA -> B\n```\n" -fence diagram]
puts [ztcl::markdown html {See [the pad](pad.md).} -link {string map {.md .html}}]
puts [ztcl::markdown html "## Fuel and crew\n\n## Fuel and crew\n" -ids 1]
<figure>7 bytes</figure> <p>See <a href="pad.html">the pad</a>.</p> <h2 id="fuel-and-crew">Fuel and crew</h2> <h2 id="fuel-and-crew-1">Fuel and crew</h2>
events
Gives the blocks and the inlines of TEXT as a flat list of events, in the order of the text. An event has one of four forms:
| Event | What it is |
|---|---|
enter KIND START END ATTRIBUTES |
The start of a block or an inline, with its range in the source and a dict of its attributes. |
exit KIND |
Its end. |
text START END ?BYTES? |
Text. BYTES is there when the text is not the bytes of its range: an escape, a character reference, or the text of a code block without the prefixes of its containers. |
break START END soft|hard |
A line break. |
START is the index of the first character and END the index after the last one, so string range $text $START [expr {$END - 1}] is the source of the event. -positions bytes gives offsets of the UTF-8 bytes of TEXT in their place, and -positions lines a list of the line and the column, both from 1, where the column counts characters.
The character indexes cost little: one scan finds whether the text is ASCII, and then each index is the byte offset. For other text the scan keeps a count of the characters at each 64 bytes.
package require ztcl::markdown
foreach e [ztcl::markdown events "## Pad *A*\n"] { puts $e }
enter document 0 11 {}
enter heading 0 10 {level 2}
text 3 7
enter emphasis 7 10 {}
text 8 9
exit emphasis
exit heading
exit document
The kinds and their attributes:
| Kind | Attributes |
|---|---|
document, quote, item, paragraph, rule, table, emphasis, strong, strike, codespan, htmlspan |
none |
heading |
level, and id with -ids |
list |
type (bullet or ordered), marker (-, +, *, ., or )), start of an ordered list, and tight |
task |
checked: the marker [ ] or [x] of an item of a task list, before the first block of the item |
code |
fenced, and info of a fenced block |
html |
none: an HTML block |
row |
head: 1 for the head row of a table |
cell |
align: none, left, center, or right |
link, image |
url and title |
A code block, an HTML block, a code span, and raw HTML in a paragraph (htmlspan) have their text as one text between their enter and their exit.
tree
Gives the same data as events as nested lists: the document node. A node is a list of five elements: the kind, the dict of attributes, the start, the end, and the list of its children. A text and a break have the form of their event. The options are those of events.
package require ztcl::markdown
puts [ztcl::markdown tree {Go & [see](/pad)}]
document {} 0 20 {{paragraph {} 0 20 {{text 0 9 {Go & }} {link {url /pad title {}} 9 20 {{text 10 13}}}}}}
Limits
A read spends one compute unit for each 256 bytes of the text in each of its two passes, and one for each step of the work of the delimiters and the brackets of the inlines. The time of a read is linear in the size of the text, also for the inputs that make some readers slow: long runs of *, many [, and many backticks of different lengths.
A read counts in the memory limit, and the memory of a read goes free at its end.
The blocks and the inlines can nest as deep as the nesting limit of scripts (2,000 by default). Past it, a read stops with the error code ZTCL MARKDOWN NESTING.
Errors
| Command | Message |
|---|---|
ztcl::markdown html x -html raw |
bad value "raw": must be escape or keep |
ztcl::markdown events x -positions words |
bad value "words": must be chars, bytes, or lines |
ztcl::markdown html x -gfm {table nope} |
bad extension "nope": must be table, tasklist, strike, autolink, or tagfilter |
ztcl::markdown events x -html keep |
bad option "-html": must be -gfm, -positions, or -ids |
ztcl::markdown html [string repeat "> " 3000]x |
markdown nesting is deeper than 2000 levels |
Platforms
Each target, and WebAssembly. -Dbatteries=core leaves the module out of the build.
In Zig
@import("ztcl").markdown is the reader for a Zig program: events gives the events of a text in an arena, toHtml gives its HTML, and html.render writes the HTML of a list of events with the hooks of the embedder.