markdown_it.tree module

A tree representation of a linear markdown-it token stream.

This module is not part of upstream JavaScript markdown-it.

class markdown_it.tree.SyntaxTreeNode(tokens: Sequence[markdown_it.token.Token] = (), *, create_root: bool = True)[source]

Bases: object

A Markdown syntax tree node.

A class that can be used to construct a tree representation of a linear markdown-it-py token stream.

Each node in the tree represents either:
  • root of the Markdown document

  • a single unnested Token

  • a Token “_open” and “_close” token pair, and the tokens nested in

    between

attrGet(name: str)Union[None, str, int, float][source]

Get the value of attribute name, or null if it does not exist.

property attrs

Html attributes.

property block

True for block-level tokens, false for inline tokens.

property children
property content

In a case of self-closing tag (code, html, fence, etc.), it has contents of this tag.

property hidden

If it’s true, ignore this element when rendering. Used for tight lists to hide paragraphs.

property info

fence infostring

property is_nested

Is this node nested?.

Returns True if the node represents a Token pair and tokens in the sequence between them, where Token.nesting of the first Token in the pair is 1 and nesting of the other Token is -1.

property is_root

Is the node a special root node?

property level

nesting level, the same as state.level

property map

Source map info. Format: Tuple[ line_begin, line_end ]

property markup

‘*’ or ‘_’ for emphasis, fence string for fence, etc.

property meta

A place for plugins to store an arbitrary data.

property next_sibling

Get the next node in the sequence of siblings.

Returns None if this is the last sibling.

property parent
pretty(*, indent: int = 2, show_text: bool = False, _current: int = 0)str[source]

Create an XML style string of the tree.

property previous_sibling

Get the previous node in the sequence of siblings.

Returns None if this is the first sibling.

property siblings

Get siblings of the node.

Gets the whole group of siblings, including self.

property tag

html tag name, e.g. “p”

to_tokens()List[markdown_it.token.Token][source]

Recover the linear token stream.

property type

Get a string type of the represented syntax.

  • “root” for root nodes

  • Token.type if the node represents an unnested token

  • Token.type of the opening token, with “_open” suffix stripped, if

    the node represents a nester token pair

walk(*, include_self: bool = True)Generator[_NodeType, None, None][source]

Recursively yield all descendant nodes in the tree starting at self.

The order mimics the order of the underlying linear token stream (i.e. depth first).