Interface Token

Source
Expand description

Represents one item in the parsed token stream, storing parsed data and providing helpers for managing HTML attributes.


interface Token {
    type: string;
    tag: string;
    attrs: [name: string, value: string | number][] | null;
    map: [number, number] | null;
    nesting: 0 | 1 | -1;
    level: number;
    children: Token[] | null;
    content: string;
    markup: string;
    info: string;
    meta: Record<string, unknown> | null;
    block: boolean;
    hidden: boolean;
    attrIndex(name: string): number;
    attrPush(attrData: [name: string, value: string | number]): void;
    attrSet(name: string, value: string | number): void;
    attrGet(name: string): string | number | null;
    attrJoin(name: string, value: string | number): void;
}

Properties§

§type: string

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

§tag: string

html tag name, e.g. "p"

§attrs: [name: string, value: string | number][] | null

Html attributes. Format: [ [ name1, value1 ], [ name2, value2 ] ]

§map: [number, number] | null = null

Source map info. Format: [ line_begin, line_end ]

§nesting: 0 | 1 | -1

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
§level: number = 0

nesting level, the same as state.level

§children: Token[] | null = null

An array of child nodes (inline and img tokens)

§content: string = ''

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

§markup: string = ''

'*' or '_' for emphasis, fence string for fence, etc.

§info: string = ''

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
§meta: Record<string, unknown> | null

A place for plugins to store an arbitrary data

§block: boolean = false

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

§hidden: boolean = false

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

Methods§

Source§

attrIndex(name: string): number

Search attribute index by name.

Source§

attrPush(attrData: [name: string, value: string | number]): void

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

Source§

attrSet(name: string, value: string | number): void

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

Source§

attrGet(name: string): string | number | null

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

Source§

attrJoin(name: string, value: string | number): void

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