docs/api/slate/node.mdx
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
NodeAPIancestorGet the node at a specific path, asserting that it's an ancestor node.
<API name="ancestor"> <APIParameters> <APIItem name="root" type="TNode"> The root node to start from. </APIItem> <APIItem name="path" type="Path"> The path to the ancestor node. </APIItem> </APIParameters> <APIReturns type="Ancestor | undefined"> The ancestor node if found, or `undefined` if not found. </APIReturns> </API>ancestorsReturn a generator of all the ancestor nodes above a specific path.
<API name="ancestors"> <APIParameters> <APIItem name="root" type="TNode"> The root node to start from. </APIItem> <APIItem name="path" type="Path"> The path to get ancestors for. </APIItem> <APIItem name="options" type="NodeAncestorsOptions" optional> Options for ancestor retrieval. </APIItem> </APIParameters> <APIOptions type="NodeAncestorsOptions"> <APIItem name="reverse" type="boolean" optional> If true, returns ancestors top-down instead of bottom-up. </APIItem> </APIOptions> <APIReturns type="Generator<NodeEntry<Ancestor>, void, undefined>"> A generator of ancestor node entries. </APIReturns> </API>childGet the child of a node at a specific index.
<API name="child"> <APIParameters> <APIItem name="root" type="TNode"> The parent node. </APIItem> <APIItem name="index" type="number"> The index of the child. </APIItem> </APIParameters> <APIReturns type="TNode | undefined"> The child node if found, or `undefined` otherwise. </APIReturns> </API>childrenIterate over the children of a node at a specific path.
<API name="children"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the parent node. </APIItem> <APIItem name="options" type="NodeChildrenOptions" optional> Options for iterating over children. </APIItem> </APIParameters> <APIOptions type="NodeChildrenOptions"> <APIItem name="reverse" type="boolean" optional> If true, iterates in reverse order. </APIItem> <APIItem name="from" type="number" optional> Start index (inclusive). </APIItem> <APIItem name="to" type="number" optional> End index (exclusive). </APIItem> </APIOptions> <APIReturns type="Generator<NodeEntry<TNode>, void, undefined>"> A generator of child node entries. </APIReturns> </API>commonGet an entry for the common ancestor node of two paths.
<API name="common"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> First path. </APIItem> <APIItem name="another" type="Path"> Second path. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The common ancestor entry if found, or `undefined` otherwise. </APIReturns> </API>descendantGet the node at a specific path, asserting that it's a descendant node.
<API name="descendant"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the descendant. </APIItem> </APIParameters> <APIReturns type="Descendant | undefined"> The descendant node if found, or `undefined` otherwise. </APIReturns> </API>descendantsReturn a generator of all the descendant node entries inside a root node.
<API name="descendants"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="options" type="NodeDescendantsOptions" optional> Options for descendant retrieval. </APIItem> </APIParameters> <APIOptions type="NodeDescendantsOptions"> <APIItem name="from" type="Path" optional> Starting path. </APIItem> <APIItem name="to" type="Path" optional> Ending path. </APIItem> <APIItem name="reverse" type="boolean" optional> If true, iterates in reverse order. </APIItem> <APIItem name="pass" type="(node: Descendant) => boolean" optional> A function to filter descendants. </APIItem> </APIOptions> <APIReturns type="Generator<NodeEntry<Descendant>, void, undefined>"> A generator of descendant node entries. </APIReturns> </API>elementsReturn a generator of all the element nodes inside a root node.
<API name="elements"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="options" type="NodeElementsOptions" optional> Options for element retrieval. </APIItem> </APIParameters> <APIOptions type="NodeElementsOptions"> <APIItem name="pass" type="(node: Element) => boolean" optional> A function to filter elements. </APIItem> <APIItem name="reverse" type="boolean" optional> If true, iterates in reverse order. </APIItem> <APIItem name="from" type="Path" optional> Starting path. </APIItem> <APIItem name="to" type="Path" optional> Ending path. </APIItem> </APIOptions> <APIReturns type="Generator<NodeEntry<Element>, void, undefined>"> A generator of element entries. </APIReturns> </API>firstGet the first node entry in a root node from a path.
<API name="first"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The first node entry if found, or `undefined` otherwise. </APIReturns> </API>firstChildGet the first child node entry of a node.
<API name="firstChild"> <APIParameters> <APIItem name="root" type="TNode"> The parent node. </APIItem> <APIItem name="path" type="Path"> The path to the parent node. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The first child node entry if found, or `undefined` otherwise. </APIReturns> </API>firstTextGet the first text node entry of a node.
<API name="firstText"> <APIParameters> <APIItem name="root" type="TNode"> The parent node. </APIItem> <APIItem name="path" type="Path"> The path to the parent node. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The first text node entry if found, or `undefined` otherwise. </APIReturns> </API>fragmentGet the sliced fragment represented by a range inside a root node.
<API name="fragment"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="range" type="TRange"> The range to slice. </APIItem> </APIParameters> <APIReturns type="N[]"> The sliced fragment. </APIReturns> </API>getGet the descendant node referred to by a specific path.
<API name="get"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="TNode | undefined"> The node if found, or `undefined` otherwise. </APIReturns> </API>lastGet the last node entry in a root node from a path.
<API name="last"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The last node entry if found, or `undefined` otherwise. </APIReturns> </API>lastChildGet the last child node entry of a node.
<API name="lastChild"> <APIParameters> <APIItem name="root" type="TNode"> The parent node. </APIItem> <APIItem name="path" type="Path"> The path to the parent node. </APIItem> </APIParameters> <APIReturns type="NodeEntry<N> | undefined"> The last child node entry if found, or `undefined` otherwise. </APIReturns> </API>leafGet the node at a specific path, ensuring it's a leaf text node.
<API name="leaf"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="N | undefined"> The leaf node if found, or `undefined` otherwise. </APIReturns> </API>levelsReturn a generator of the in a branch of the tree, from a specific path.
<API name="levels"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="Generator<NodeEntry<N>, void, undefined>"> A generator of node entries in a branch of the tree from a specific path. </APIReturns> </API>nodesReturn a generator of all the node entries of a root node.
<API name="nodes"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="options" type="NodeTextsOptions" optional> Similar options to `descendants`. </APIItem> </APIParameters> <APIReturns type="Generator<NodeEntry<N>, void, undefined>"> A generator of node entries. </APIReturns> </API>parentGet the parent of a node at a specific path.
<API name="parent"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="Ancestor | undefined"> The parent node if found, or `undefined` otherwise. </APIReturns> </API>textsReturn a generator of all leaf text nodes in a root node.
<API name="texts"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="options" type="NodeTextsOptions" optional> Options for text node retrieval. </APIItem> </APIParameters> <APIReturns type="Generator<NodeEntry<Text>, void, undefined>"> A generator of text node entries. </APIReturns> </API>extractPropsGet the props of a node.
<API name="extractProps"> <APIParameters> <APIItem name="node" type="TNode"> The node to extract props from. </APIItem> </APIParameters> <APIReturns type="NodeProps<N>"> The props of the node. </APIReturns> </API>hasCheck if a descendant node exists at a specific path.
<API name="has"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to check. </APIItem> </APIParameters> <APIReturns type="boolean"> `true` if a node exists at the specified path, `false` otherwise. </APIReturns> </API>hasSingleChildCheck if a node has a single child.
<API name="hasSingleChild"> <APIParameters> <APIItem name="node" type="TNode"> The node to check. </APIItem> </APIParameters> <APIReturns type="boolean"> `true` if the node has a single child. </APIReturns> </API>isAncestorCheck if a value implements the Ancestor interface.
isDescendantCheck if a value implements the Descendant interface.
isLastChildCheck if a node is the last child of its parent.
<API name="isLastChild"> <APIParameters> <APIItem name="root" type="TNode"> The root node. </APIItem> <APIItem name="path" type="Path"> The path to the node. </APIItem> </APIParameters> <APIReturns type="boolean"> `true` if the node is the last child of its parent. </APIReturns> </API>isNodeCheck if a value implements the TNode interface.
isNodeListCheck if a value is a list of Descendant objects.
matchesCheck if a node matches a set of props.
<API name="matches"> <APIParameters> <APIItem name="node" type="Descendant"> The node to check. </APIItem> <APIItem name="props" type="Partial<Descendant>"> The properties to match against. </APIItem> </APIParameters> <APIReturns type="boolean"> `true` if the node matches the provided properties. </APIReturns> </API>stringGet the concatenated text string of a node's content.
<API name="string"> <APIParameters> <APIItem name="node" type="TNode"> The node to get text from. </APIItem> </APIParameters> <APIReturns type="string"> The concatenated text content. </APIReturns> </API>TNodeNode is a type alias for TNode.
type TNode = Editor | TElement | TText;
NodeEntryNodeEntry 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.
DescendantThe Descendant union type represents nodes that are descendants in the tree.
type Descendant = TElement | TText;
AncestorThe Ancestor union type represents nodes that are ancestors in the tree.
type Ancestor = Editor | TElement;
NodeOf<N>NodeIn<V>TNodeMatch<N>DescendantOf<N>DescendantIn<V>ChildOf<N>AncestorOf<N>AncestorIn<V>AncestorEntryAncestor entries represent an ancestor node (Editor or Element) and its path.
<API name="AncestorEntry"> <APIAttributes> <APIItem name="0" type="Ancestor"> The Editor or Element node. </APIItem> <APIItem name="1" type="Path"> The path to the ancestor. </APIItem> </APIAttributes> </API>DescendantEntryDescendant entries represent a descendant node (Element or Text) and its path.
<API name="DescendantEntry"> <APIAttributes> <APIItem name="0" type="Descendant"> The Element or Text node. </APIItem> <APIItem name="1" type="Path"> The path to the descendant. </APIItem> </APIAttributes> </API>NodeChildEntryNode child entries represent a child node and its path relative to its parent.
<API name="NodeChildEntry"> <APIAttributes> <APIItem name="0" type="TNode"> The child node. </APIItem> <APIItem name="1" type="Path"> The path to the child. </APIItem> </APIAttributes> </API>