API reference for nodes in Plate.

Nodes are the building blocks of Plate documents. It can either be the Editor root node (highest), an Element node, or a Text node (lowest). This API provides utilities for interacting with nodes, including traversing, querying, and extracting data.

type TNode = Editor | TElement | TText;
 
type Descendant = Element | Text
type Ancestor = Editor | Element

NodeAPI

ancestor

Get the node at a specific path, asserting that it's an ancestor node.

Parameters

  • root TNode

    The root node to start from.

  • path Path

    The path to the ancestor node.

ReturnsAncestor | undefined

    The ancestor node if found, or undefined if not found.

ancestors

Return a generator of all the ancestor nodes above a specific path.

Parameters

Collapse all
  • root TNode

    The root node to start from.

  • path Path

    The path to get ancestors for.

  • options optional NodeAncestorsOptions

    Options for ancestor retrieval.

OptionsNodeAncestorsOptions

Collapse all
  • reverse optional boolean

    If true, returns ancestors top-down instead of bottom-up.

ReturnsGenerator<NodeEntry<Ancestor>, void, undefined>

    A generator of ancestor node entries.

child

Get the child of a node at a specific index.

Parameters

Collapse all
  • root TNode

    The parent node.

  • index number

    The index of the child.

ReturnsTNode | undefined

    The child node if found, or undefined otherwise.

children

Iterate over the children of a node at a specific path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the parent node.

  • options optional NodeChildrenOptions

    Options for iterating over children.

OptionsNodeChildrenOptions

Collapse all
  • reverse optional boolean

    If true, iterates in reverse order.

  • from optional number

    Start index (inclusive).

  • to optional number

    End index (exclusive).

ReturnsGenerator<NodeEntry<TNode>, void, undefined>

    A generator of child node entries.

common

Get an entry for the common ancestor node of two paths.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    First path.

  • another Path

    Second path.

ReturnsNodeEntry<N> | undefined

    The common ancestor entry if found, or undefined otherwise.

descendant

Get the node at a specific path, asserting that it's a descendant node.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the descendant.

ReturnsDescendant | undefined

    The descendant node if found, or undefined otherwise.

descendants

Return a generator of all the descendant node entries inside a root node.

Parameters

Collapse all
  • root TNode

    The root node.

  • options optional NodeDescendantsOptions

    Options for descendant retrieval.

OptionsNodeDescendantsOptions

Collapse all
  • from optional Path

    Starting path.

  • to optional Path

    Ending path.

  • reverse optional boolean

    If true, iterates in reverse order.

  • pass optional (node: Descendant) => boolean

    A function to filter descendants.

ReturnsGenerator<NodeEntry<Descendant>, void, undefined>

    A generator of descendant node entries.

elements

Return a generator of all the element nodes inside a root node.

Parameters

Collapse all
  • root TNode

    The root node.

  • options optional NodeElementsOptions

    Options for element retrieval.

OptionsNodeElementsOptions

Collapse all
  • pass optional (node: Element) => boolean

    A function to filter elements.

  • reverse optional boolean

    If true, iterates in reverse order.

  • from optional Path

    Starting path.

  • to optional Path

    Ending path.

ReturnsGenerator<NodeEntry<Element>, void, undefined>

    A generator of element entries.

first

Get the first node entry in a root node from a path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsNodeEntry<N> | undefined

    The first node entry if found, or undefined otherwise.

firstChild

Get the first child node entry of a node.

Parameters

Collapse all
  • root TNode

    The parent node.

  • path Path

    The path to the parent node.

ReturnsNodeEntry<N> | undefined

    The first child node entry if found, or undefined otherwise.

firstText

Get the first text node entry of a node.

Parameters

Collapse all
  • root TNode

    The parent node.

  • path Path

    The path to the parent node.

ReturnsNodeEntry<N> | undefined

    The first text node entry if found, or undefined otherwise.

fragment

Get the sliced fragment represented by a range inside a root node.

Parameters

Collapse all
  • root TNode

    The root node.

  • range TRange

    The range to slice.

ReturnsN[]

    The sliced fragment.

get

Get the descendant node referred to by a specific path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsTNode | undefined

    The node if found, or undefined otherwise.

last

Get the last node entry in a root node from a path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsNodeEntry<N> | undefined

    The last node entry if found, or undefined otherwise.

lastChild

Get the last child node entry of a node.

Parameters

Collapse all
  • root TNode

    The parent node.

  • path Path

    The path to the parent node.

ReturnsNodeEntry<N> | undefined

    The last child node entry if found, or undefined otherwise.

leaf

Get the node at a specific path, ensuring it's a leaf text node.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsN | undefined

    The leaf node if found, or undefined otherwise.

levels

Return a generator of the in a branch of the tree, from a specific path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsGenerator<NodeEntry<N>, void, undefined>

    A generator of node entries in a branch of the tree from a specific path.

nodes

Return a generator of all the node entries of a root node.

Parameters

Collapse all
  • root TNode

    The root node.

  • options optional NodeTextsOptions

    Similar options to descendants.

ReturnsGenerator<NodeEntry<N>, void, undefined>

    A generator of node entries.

parent

Get the parent of a node at a specific path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

ReturnsAncestor | undefined

    The parent node if found, or undefined otherwise.

texts

Return a generator of all leaf text nodes in a root node.

Parameters

Collapse all
  • root TNode

    The root node.

  • options optional NodeTextsOptions

    Options for text node retrieval.

ReturnsGenerator<NodeEntry<Text>, void, undefined>

    A generator of text node entries.

extractProps

Get the props of a node.

Parameters

Collapse all
  • node TNode

    The node to extract props from.

ReturnsNodeProps<N>

    The props of the node.

has

Check if a descendant node exists at a specific path.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to check.

Returnsboolean

    true if a node exists at the specified path, false otherwise.

hasSingleChild

Check if a node has a single child.

Parameters

Collapse all
  • node TNode

    The node to check.

Returnsboolean

    true if the node has a single child.

isAncestor

Check if a value implements the Ancestor interface.

Parameters

Collapse all
  • value any

    The value to check.

Returnsboolean

    true if the value implements the Ancestor interface.

isDescendant

Check if a value implements the Descendant interface.

Parameters

Collapse all
  • value any

    The value to check.

Returnsboolean

    true if the value implements the Descendant interface.

isLastChild

Check if a node is the last child of its parent.

Parameters

Collapse all
  • root TNode

    The root node.

  • path Path

    The path to the node.

Returnsboolean

    true if the node is the last child of its parent.

isNode

Check if a value implements the TNode interface.

Parameters

Collapse all
  • value any

    The value to check.

Returnsboolean

    true if the value implements the TNode interface.

isNodeList

Check if a value is a list of Descendant objects.

Parameters

Collapse all
  • value any

    The value to check.

Returnsboolean

    true if the value is a list of Descendant objects.

matches

Check if a node matches a set of props.

Parameters

Collapse all
  • node Descendant

    The node to check.

  • props Partial<Descendant>

    The properties to match against.

Returnsboolean

    true if the node matches the provided properties.

string

Get the concatenated text string of a node's content.

Parameters

Collapse all
  • node TNode

    The node to get text from.

Returnsstring

    The concatenated text content.

Types

TNode

Node is a type alias for TNode.

type TNode = Editor | TElement | TText;

NodeEntry

NodeEntry objects are returned when iterating over the nodes in a Plate document tree. They consist of an array with two elements: the TNode and its Path relative to the root node in the document.

Attributes

Collapse all
  • 0 TNode

    The node itself.

  • 1 Path

    The path to the node.

Descendant

The Descendant union type represents nodes that are descendants in the tree.

type Descendant = TElement | TText;

Ancestor

The Ancestor union type represents nodes that are ancestors in the tree.

type Ancestor = Editor | TElement;

NodeOf<N>

Parameters

Collapse all
  • node TNode

    The node to get the type of.

ReturnsN

    The node type.

NodeIn<V>

Parameters

Collapse all
  • value Value

    The value to get node types from.

ReturnsNodeOf<V[number]>

    All possible node types from the specified value.

TNodeMatch<N>

Parameters

Collapse all
  • node N

    The node to match.

Returnsboolean

    true if the node matches the predicate.

DescendantOf<N>

Parameters

Collapse all
  • node N

    The node to get descendant types from.

ReturnsDescendantOf<N>

    All possible descendant node types from the specified root node.

DescendantIn<V>

Parameters

Collapse all
  • value Value

    The value to get descendant types from.

ReturnsDescendantIn<V>

    All possible descendant node types from the specified value.

ChildOf<N>

Parameters

Collapse all
  • node N

    The node to get the child type from.

ReturnsChildOf<N>

    The child node type.

AncestorOf<N>

Parameters

Collapse all
  • node N

    The node to get ancestor types from.

ReturnsAncestorOf<N>

    All possible ancestor node types from the specified root node.

AncestorIn<V>

Parameters

Collapse all
  • value Value

    The value to get ancestor types from.

ReturnsAncestorIn<V>

    All possible ancestor node types from the specified value.

AncestorEntry

Ancestor entries represent an ancestor node (Editor or Element) and its path.

Attributes

Collapse all
  • 0 Ancestor

    The Editor or Element node.

  • 1 Path

    The path to the ancestor.

DescendantEntry

Descendant entries represent a descendant node (Element or Text) and its path.

Attributes

Collapse all
  • 0 Descendant

    The Element or Text node.

  • 1 Path

    The path to the descendant.

NodeChildEntry

Node child entries represent a child node and its path relative to its parent.

Attributes

Collapse all
  • 0 TNode

    The child node.

  • 1 Path

    The path to the child.