Node

API reference for nodes in Slate.

Nodes are the building blocks of Slate 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

Collapse all

    The root node to start from.

    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

    The root node to start from.

    The path to get ancestors for.

    Options for ancestor retrieval.

OptionsNodeAncestorsOptions

Collapse all

    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

    The parent node.

    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

    The root node.

    The path to the parent node.

    Options for iterating over children.

OptionsNodeChildrenOptions

Collapse all

    If true, iterates in reverse order.

    Start index (inclusive).

    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

    The root node.

    First 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

    The root node.

    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

    The root node.

    Options for descendant retrieval.

OptionsNodeDescendantsOptions

Collapse all

    Starting path.

    Ending path.

    If true, iterates in reverse order.

    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

    The root node.

    Options for element retrieval.

OptionsNodeElementsOptions

Collapse all

    A function to filter elements.

    If true, iterates in reverse order.

    Starting 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

    The root node.

    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

    The parent node.

    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

    The parent node.

    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

    The root node.

    The range to slice.

ReturnsN[]

    The sliced fragment.

get

Get the descendant node referred to by a specific path.

Parameters

Collapse all

    The root node.

    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

    The root node.

    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

    The parent node.

    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

    The root node.

    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

    The root node.

    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

    The root node.

    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

    The root node.

    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

    The root node.

    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

    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

    The root node.

    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

    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

    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

    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

    The root node.

    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

    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

    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

    The node to check.

    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

    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 Slate 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

    The node itself.

    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

    The node to get the type of.

ReturnsN

    The node type.

NodeIn<V>

Parameters

Collapse all

    The value to get node types from.

ReturnsNodeOf<V[number]>

    All possible node types from the specified value.

TNodeMatch<N>

Parameters

Collapse all

    The node to match.

Returnsboolean

    true if the node matches the predicate.

DescendantOf<N>

Parameters

Collapse all

    The node to get descendant types from.

ReturnsDescendantOf<N>

    All possible descendant node types from the specified root node.

DescendantIn<V>

Parameters

Collapse all

    The value to get descendant types from.

ReturnsDescendantIn<V>

    All possible descendant node types from the specified value.

ChildOf<N>

Parameters

Collapse all

    The node to get the child type from.

ReturnsChildOf<N>

    The child node type.

AncestorOf<N>

Parameters

Collapse all

    The node to get ancestor types from.

ReturnsAncestorOf<N>

    All possible ancestor node types from the specified root node.

AncestorIn<V>

Parameters

Collapse all

    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

    The Editor or Element node.

    The path to the ancestor.

DescendantEntry

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

Attributes

Collapse all

    The Element or Text node.

    The path to the descendant.

NodeChildEntry

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

Attributes

Collapse all

    The child node.

    The path to the child.