beetlebug▾
/

ztcl::markdown tree

Gives the blocks and the inlines of a Markdown text as nested lists.

ztcl::markdown tree TEXT ?-gfm VALUE? ?-positions chars|bytes|lines? ?-ids BOOLEAN?

Description

Gives 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 in events. The options are those of events.

Arguments

ArgumentTypeDefaultDescription
TEXTstringrequiredThe Markdown text.

Options

OptionTypeDefaultDescription
-gfmboolean or list1Turns the GFM extensions on or off, as -gfm of html.
-positionsstringcharsGives each position as a character index, a byte offset, or a line and a column, as -positions of events.
-idsboolean0Gives each heading the attribute id, as -ids of html.

-gfm VALUE

Turns the GFM extensions on or off, as -gfm of html.

A true boolean turns on each extension, a false one gives plain CommonMark, and a list of names turns on only those: table, tasklist, strike, autolink, and tagfilter.

% lindex [ztcl::markdown tree "~~a~~" -gfm strike] 4 0 4 0 0
strike
% ztcl::markdown tree x -gfm x
bad extension "x": must be table, tasklist, strike, autolink, or tagfilter
-errorcode TCL LOOKUP INDEX value

-positions chars|bytes|lines

Gives each position as a character index, a byte offset, or a line and a column, as -positions of events.

With chars, a position is an index of the characters of the text. With bytes, it is an offset of the UTF-8 bytes of the text, and with lines a list of the line and the column, both from 1.

% lrange [ztcl::markdown tree "é" -positions lines] 2 3
{1 1} {1 2}
% ztcl::markdown tree x -positions words
bad value "words": must be chars, bytes, or lines
-errorcode TCL LOOKUP INDEX value

-ids BOOLEAN

Gives each heading the attribute id, as -ids of html.

The id is the slug of the text of the heading, with -1, -2, and so on after a slug that came before.

% lindex [ztcl::markdown tree "# Go" -ids 1] 4 0 1
level 1 id go
% ztcl::markdown tree x -ids maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER

Returns

A nested list.

Examples

Each example of this page runs after package require ztcl::markdown.

% 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}}}}}}

Errors

% ztcl::markdown tree
wrong # args: should be "ztcl::markdown tree text ?-gfm value? ?-positions chars|bytes|lines? ?-ids bool?"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.8
Compute cost
One compute unit for each 256 bytes of the text in each of the two passes, and one for each step of the work of the delimiters and the brackets.

See also