The Zuri Standard Library Zuri

Text and Markup

markdown.node

import markdown.node
Zuri

markdown lifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelled markdown.node.* needs import markdown.node.

The tree that markdown.parse() builds.

Every node is a Node, and its type says what kind of node it is. A node only has the fields that its type uses, and the rest are nil. For example, a heading has a level, a link has a destination and a title, and a paragraph has neither.

import markdown

var doc = markdown.parse('# Title\n\nSome *emphasis*.')

doc.walk(@(node) {
  if node.type == 'heading' {
    echo '${node.level}: ${node.text_content()}'
  }
})
Zuri

Block types

Type Fields
document metadata
paragraph
heading level, attributes
thematic_break
block_quote
alert kind
list ordered, start, delimiter, bullet, tight
item checked
code_block literal, info, attributes
html_block literal
table
table_head, table_body, table_row
table_cell align
footnote_definition label
definition_list
term
definition tight
div attributes
line_block, line
math_block literal
toc

Inline types

Type Fields
text literal
softbreak, linebreak
code literal, attributes
emphasis, strong
strikethrough, mark
superscript, subscript
link, image destination, title, attributes
html_inline literal
footnote_reference label. An inline note also has its text as children.
span attributes
math literal, display
abbreviation title
emoji literal, label

line and end_line are the first and last source lines of a block, counting from 1. They're nil for inline nodes.

Classes

Node

class markdown.Node
Zuri

A node in a parsed Markdown document.

Treat children as read-only. To change the tree, use append_child() and the other methods of this class, which keep each child's parent_node up to date.

  • printable — has a @to_string(), so echo and print() show something useful

Fields

Field Type Description
type string The kind of node, such as 'paragraph', 'link' or 'text'.
parent_node ?Node The node that this one belongs to, or nil for a document or a detached node.
children list The node's children, in document order.
literal ?string The text of a text, code, code_block, html_block, html_inline, math, math_block or emoji node.
level ?number A heading's level, from 1 to 6.
info ?string The info string of a fenced code block, which is the text after the opening fence, with escapes and entities...
destination ?string Where a link points or an image comes from, percent-encoded.
title ?string The title of a link or image, or the expansion of an abbreviation.
ordered ?bool Whether a list is numbered.
start ?number The number an ordered list starts at.
delimiter ?string The character after each number in an ordered list: '.' or ')'.
bullet ?string The character that each item of a bullet list starts with: '-', '*' or '+'.
tight ?bool Whether a list or a definition is tight.
checked ?bool Whether a task list item is checked.
align ?string A table cell's alignment: 'left', 'center', 'right', or nil for none.
label ?string The label of a footnote, as written between [^ and ], or the shortcode of an emoji without its colons.
kind ?string The kind of alert: 'note', 'tip', 'important', 'warning' or 'caution'.
display ?bool Whether a math node was written with $$, in which case it's shown on a line of its own instead of in the...
attributes dict The HTML attributes of the node's element, from a {#id .class key=value} block or a generated heading id.
metadata ?dict The decoded front matter at the start of a document, or nil if it has none.
line ?number The source line that a block starts on, counting from 1.
end_line ?number The source line that a block ends on, counting from 1.

Constructor

markdown.Node(type: string)
Zuri

Parameters

  • type (string)

Node.is_block()

markdown.Node.is_block() -> bool
Zuri

Whether the node is a block, such as a paragraph or a list.

Returns bool

Node.is_inline()

markdown.Node.is_inline() -> bool
Zuri

Whether the node is inline, meaning part of a block's text, such as emphasis or a link.

Returns bool

Node.first_child()

markdown.Node.first_child() -> ?Node
Zuri

Returns the first child, or nil if there are no children.

Returns ?Node

Node.last_child()

markdown.Node.last_child() -> ?Node
Zuri

Returns the last child, or nil if there are no children.

Returns ?Node

Node.index_in_parent()

markdown.Node.index_in_parent() -> number
Zuri

Returns the index of this node among its parent's children, or -1 if it has no parent.

Returns number

Node.next_sibling()

markdown.Node.next_sibling() -> ?Node
Zuri

Returns the next node in the same parent, or nil if this is the last one.

Returns ?Node

Node.previous_sibling()

markdown.Node.previous_sibling() -> ?Node
Zuri

Returns the previous node in the same parent, or nil if this is the first one.

Returns ?Node

Node.append_child()

markdown.Node.append_child(node: instance) -> Node
Zuri

Adds node as the last child, removing it from its current parent first. Returns node.

Parameters

  • node (Node)

Returns Node

Node.prepend_child()

markdown.Node.prepend_child(node: instance) -> Node
Zuri

Adds node as the first child, removing it from its current parent first. Returns node.

Parameters

  • node (Node)

Returns Node

Node.insert_before()

markdown.Node.insert_before(node: instance, reference: ?instance) -> Node
Zuri

Inserts node as a child immediately before reference, or as the last child if reference is nil. Returns node.

Parameters

  • node (Node)
  • reference (?Node) — a child of this node.

Returns Node

Raises ValueError If reference isn't a child of this node.

Node.insert_after()

markdown.Node.insert_after(node: instance, reference: instance) -> Node
Zuri

Inserts node as a child immediately after reference. Returns node.

Parameters

  • node (Node)
  • reference (Node) — a child of this node.

Returns Node

Raises ValueError If reference isn't a child of this node.

Node.replace_with()

markdown.Node.replace_with(node: instance) -> Node
Zuri

Puts node in this node's place in the tree, and detaches this node. Returns node.

Parameters

  • node (Node)

Returns Node

Node.detach()

markdown.Node.detach() -> Node
Zuri

Removes this node from its parent, and returns it.

Returns Node

Node.walk()

markdown.Node.walk(callback: function)
Zuri

Calls callback for each node below this one, in document order. If the callback returns false, the children of that node are skipped.

Don't add or remove nodes inside the callback. Find them with find_all() first, and then change the tree.

Parameters

  • callback (function)

Node.descendants()

markdown.Node.descendants() -> list[Node]
Zuri

Returns every node below this one, in document order.

Returns list[Node]

Node.find_all()

markdown.Node.find_all(type: string) -> list[Node]
Zuri

Returns every node of the given type below this one, in document order.

Parameters

  • type (string)

Returns list[Node]

Node.find()

markdown.Node.find(type: string) -> ?Node
Zuri

Returns the first node of the given type below this one, or nil if there isn't one.

Parameters

  • type (string)

Returns ?Node

Node.text_content()

markdown.Node.text_content() -> string
Zuri

Returns the text of this node and its descendants, with all markup removed. Line breaks become spaces, and an image is replaced by its description.

import markdown

var doc = markdown.parse('# A *very* `fine` title')
echo doc.first_child().text_content()
# A very fine title
Zuri

Returns string

Node.clone()

markdown.Node.clone() -> Node
Zuri

Returns a copy of this node and all its descendants. The copy has no parent.

Returns Node

Node.dump()

markdown.Node.dump() -> string
Zuri

Returns this node and its descendants as indented text, with one node on each line. It's useful for seeing the tree while you write a transform.

import markdown

echo markdown.parse('Hello *there*').dump()
# document
#   paragraph
#     text 'Hello '
#     emphasis
#       text 'there'
Zuri

Returns string

Node.to_string()

markdown.Node.to_string() -> string
Zuri

Returns the node's type and its main field, such as heading 2.

Returns string


2026, Richard Ore and Zuri contributors