markdown_it.token module#

class markdown_it.token.Token(type: 'str', tag: 'str', nesting: 'int', attrs: 'dict[str, str | int | float]' = <factory>, map: 'list[int] | None' = None, level: 'int' = 0, children: 'list[Token] | None' = None, content: 'str' = '', markup: 'str' = '', info: 'str' = '', meta: 'dict' = <factory>, block: 'bool' = False, hidden: 'bool' = False)[source]#

Bases: object

as_dict(*, children: bool = True, as_upstream: bool = True, meta_serializer: Callable[[dict], Any] | None = None, filter: Callable[[str, Any], bool] | None = None, dict_factory: Callable[..., MutableMapping[str, Any]] = <class 'dict'>) MutableMapping[str, Any][source]#

Return the token as a dictionary.

  • children – Also convert children to dicts

  • as_upstream – Ensure the output dictionary is equal to that created by markdown-it For example, attrs are converted to null or lists

  • meta_serializer – hook for serializing Token.meta

  • filter – A callable whose return code determines whether an attribute or element is included (True) or dropped (False). Is called with the (key, value) pair.

  • dict_factory – A callable to produce dictionaries from. For example, to produce ordered dictionaries instead of normal Python dictionaries, pass in collections.OrderedDict.

attrGet(name: str) None | str | int | float[source]#

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

attrIndex(name: str) int[source]#
attrItems() list[tuple[str, str | int | float]][source]#

Get (key, value) list of attrs.

attrJoin(name: str, value: str) None[source]#

Join value to existing attribute via space. Or create new attribute if not exists. Useful to operate with token classes.

attrPush(attrData: tuple[str, str | int | float]) None[source]#

Add [ name, value ] attribute to list. Init attrs if necessary.

attrSet(name: str, value: str | int | float) None[source]#

Set name attribute to value. Override old value if exists.

attrs: dict[str, str | int | float]#

HTML attributes. Note this differs from the upstream “list of lists” format, although than an instance can still be initialised with this format.

block: bool = False#

True for block-level tokens, false for inline tokens. Used in renderer to calculate line breaks

children: list[Token] | None = None#

Array of child nodes (inline and img tokens).

content: str = ''#

Inner content, in the case of a self-closing tag (code, html, fence, etc.),

copy(**changes: Any) markdown_it.token.Token[source]#

Return a shallow copy of the instance.

classmethod from_dict(dct: MutableMapping[str, Any]) Token[source]#

Convert a dict to a Token.

hidden: bool = False#

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

info: str = ''#

Additional information: - Info string for “fence” tokens - The value “auto” for autolink “link_open” and “link_close” tokens - The string value of the item marker for ordered-list “list_item_open” tokens

level: int = 0#

Nesting level, the same as state.level

map: list[int] | None = None#

Source map info. Format: [ line_begin, line_end ]

markup: str = ''#

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

meta: dict#

A place for plugins to store any arbitrary data

nesting: int#

Level change (number in {-1, 0, 1} set), where: - 1 means the tag is opening - 0 means the tag is self-closing - -1 means the tag is closing

tag: str#

HTML tag name, e.g. ‘p’

type: str#

Type of the token (string, e.g. “paragraph_open”)

markdown_it.token.convert_attrs(value: Any) Any[source]#

Convert Token.attrs set as None or [[key, value], ...] to a dict.

This improves compatibility with upstream markdown-it.