Skip to main content

Function: removeNode()

removeNode(version, nodeId): DocumentVersion

Defined in: packages/core/src/removeNode.ts:36

Removes a node and everything beneath it, and drops the reference to it from whatever held it — a slot, or the document's roots.

The cascade follows slot references from the named node, so a descendant that a second slot elsewhere also references goes with it.

Parameters​

version​

DocumentVersion

The document to edit. Read, never written.

nodeId​

string

Id of the node to remove. The document must hold it.

Returns​

DocumentVersion

A new DocumentVersion with the subtree gone from elements and its id gone from every slot and from roots. The argument is not mutated, version.version is not bumped, and every surviving node the edit did not touch is carried over by reference.

Throws​

no-such-node when nodeId names a node the document does not hold.

Example​

// stack → [a, b]; a → [a1, a2]
const next = removeNode(version, "a");
next.elements.a1; // undefined — the cascade reached it
next.elements.stack?.slots?.sections; // ["b"]

// Removing the last root leaves an empty document, which `compile` refuses as `no-roots`.
removeNode(version, "stack").roots; // []