/** * Tree manipulation utilities for DatoCMS structured text documents. * * Provides a set of low-level utilities for visiting, transforming, and querying * structured text trees. Works with all tree variants (regular, request, nested). */ /** * Recursively extract all possible node types that can appear in a tree structure */ type AllNodesInTree = Depth extends 0 ? T extends readonly unknown[] ? never : T : T extends readonly (infer U)[] ? AllNodesInTree> : T extends { children: infer Children; } ? T | AllNodesInTree> : T; type WithChildren = Extract; type Prev = T extends 0 ? 0 : T extends 1 ? 0 : T extends 2 ? 1 : T extends 3 ? 2 : T extends 4 ? 3 : T extends 5 ? 4 : T extends 6 ? 5 : T extends 7 ? 6 : T extends 8 ? 7 : T extends 9 ? 8 : T extends 10 ? 9 : number; /** * Path through the Structured Text tree structure */ export type TreePath = readonly (string | number)[]; /** * Input that can be either a node or a structured text field value */ type StructuredTextDocumentOrNode = T | Document; /** * Generic predicate function type for Structured Text tree node filtering */ export type NodePredicate = (node: AllNodesInTree, parent: WithChildren> | null, path: TreePath) => R; type Document = { schema: 'dast'; document: T; }; /** * Visit every node in the Structured Text tree, calling the visitor function for each. * Uses pre-order traversal (parent is visited before its children). * * @template T - The type of the root node in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param visitor - Synchronous function called for each node. Receives the node, its parent, and path through the Structured Text tree */ export declare function forEachNode(input: StructuredTextDocumentOrNode, visitor: NodePredicate, parent?: WithChildren> | null, path?: TreePath): void; /** * Visit every node in the Structured Text tree, calling the visitor function for each. * Uses pre-order traversal (parent is visited before its children). * * @template T - The type of the root node in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param visitor - Asynchronous function called for each node. Receives the node, its parent, and path through the Structured Text tree * @returns Promise that resolves when all nodes have been visited */ export declare function forEachNodeAsync(input: StructuredTextDocumentOrNode, visitor: NodePredicate>, parent?: WithChildren> | null, path?: TreePath): Promise; /** * Default result shape for mapNodes mappers: a single node, an array of nodes * (for splat), or null/undefined (for removal). The mapper's actual return * type `R` (see `MapNodesMapper`) is left fully free, so callers may produce * nodes whose `item` (or other field-typed payload) has a shape different * from the input tree — e.g. swapping a nested-response block for a * request-shape block built via `buildBlockRecord`. */ export type MapNodesMapperResult = AllNodesInTree | readonly AllNodesInTree[] | null | undefined; /** * Mapper function type for `mapNodes`. `R` is the per-call return type and * is intentionally unconstrained — TypeScript infers it from the mapper * body, which keeps the mapper interoperable across response- and * request-shape DAST trees. */ export type MapNodesMapper> = (node: AllNodesInTree, parent: WithChildren> | null, path: TreePath) => R; /** * Mapper function type for `mapNodesAsync`. */ export type MapNodesMapperAsync> = (node: AllNodesInTree, parent: WithChildren> | null, path: TreePath) => Promise; /** * Transform nodes in the Structured Text tree by applying a mapping function. * Children are processed bottom-up: when the mapper sees a node, its descendants * have already been transformed, and the mapper's output for that node is final. * * The mapper may return: * - a single node — replaces the input node 1:1 * - an array of nodes — splatted into the parent's children (1:N) * - `null` / `undefined` — removes the node from its parent (1:0) * * Returning an array or null/undefined for the outermost (root) node throws, * since the function returns a single node. * * @template T - The type of nodes in the input Structured Text tree * @param input - A structured text document * @param mapper - Synchronous function that transforms each node. Receives the node, its parent, and path * @returns The transformed document */ export declare function mapNodes>(input: Document, mapper: MapNodesMapper): Document; /** * Transform nodes in the Structured Text tree by applying a mapping function. * See the document overload for details on bottom-up traversal and mapper return semantics. * * @template T - The type of nodes in the input Structured Text tree * @param input - A specific DAST node * @param mapper - Synchronous function that transforms each node. Receives the node, its parent, and path * @returns The transformed node */ export declare function mapNodes>(input: T, mapper: MapNodesMapper): T; /** * Transform nodes in the Structured Text tree by applying an async mapping function. * See `mapNodes` for details on bottom-up traversal and mapper return semantics. * * @template T - The type of nodes in the input Structured Text tree * @param input - A structured text document * @param mapper - Asynchronous function that transforms each node. Receives the node, its parent, and path * @returns Promise that resolves to the transformed document */ export declare function mapNodesAsync>(input: Document, mapper: MapNodesMapperAsync): Promise>; /** * Transform nodes in the Structured Text tree by applying an async mapping function. * See `mapNodes` for details on bottom-up traversal and mapper return semantics. * * @template T - The type of nodes in the input Structured Text tree * @param input - A specific DAST node * @param mapper - Asynchronous function that transforms each node. Receives the node, its parent, and path * @returns Promise that resolves to the transformed node */ export declare function mapNodesAsync>(input: T, mapper: MapNodesMapperAsync): Promise; /** * Collect all nodes that match the type guard function using depth-first search. * Returns an array containing all matching nodes along with their paths through the Structured Text tree. * * @template T - The type of nodes in the Structured Text tree * @template U - The specific type that the type guard narrows to * @param input - A structured text document, or a specific DAST node * @param predicate - Type guard function that tests and narrows each node type * @returns Array of objects, each containing a matching node (with narrowed type) and its path */ export declare function collectNodes>(input: StructuredTextDocumentOrNode, predicate: (node: AllNodesInTree, parent: AllNodesInTree | null, path: TreePath) => node is U, parent?: AllNodesInTree | null, path?: TreePath): Array<{ node: U; path: TreePath; }>; /** * Collect all nodes that match the predicate function using depth-first search. * Returns an array containing all matching nodes along with their paths through the Structured Text tree. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Synchronous function that tests each node. Should return true for desired nodes * @returns Array of objects, each containing a matching node and its path */ export declare function collectNodes(input: StructuredTextDocumentOrNode, predicate: NodePredicate, parent?: AllNodesInTree | null, path?: TreePath): Array<{ node: AllNodesInTree; path: TreePath; }>; /** * Collect all nodes that match the predicate function using depth-first search. * Returns an array containing all matching nodes along with their paths through the Structured Text tree. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Asynchronous function that tests each node. Should return true for desired nodes * @returns Promise that resolves to an array of objects, each containing a matching node and its path */ export declare function collectNodesAsync(input: StructuredTextDocumentOrNode, predicate: NodePredicate>, parent?: AllNodesInTree | null, path?: TreePath): Promise; path: TreePath; }>>; /** * Find the first node that matches the type guard function using depth-first search. * Returns the first matching node along with its path through the Structured Text tree, or null if no match is found. * * @template T - The type of nodes in the Structured Text tree * @template U - The specific type that the type guard narrows to * @param input - A structured text document, or a specific DAST node * @param predicate - Type guard function that tests and narrows each node type * @returns Object containing the first matching node (with narrowed type) and its path, or null if no match */ export declare function findFirstNode>(input: StructuredTextDocumentOrNode, predicate: (node: AllNodesInTree, parent: AllNodesInTree | null, path: TreePath) => node is U, parent?: AllNodesInTree | null, path?: TreePath): { node: U; path: TreePath; } | null; /** * Find the first node that matches the predicate function using depth-first search. * Returns the first matching node along with its path through the Structured Text tree, or null if no match is found. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Synchronous function that tests each node. Should return true for desired nodes * @returns Object containing the first matching node and its path, or null if no match */ export declare function findFirstNode(input: StructuredTextDocumentOrNode, predicate: NodePredicate, parent?: AllNodesInTree | null, path?: TreePath): { node: AllNodesInTree; path: TreePath; } | null; /** * Find the first node that matches the predicate function using depth-first search. * Returns the first matching node along with its path through the Structured Text tree, or null if no match is found. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Asynchronous function that tests each node. Should return true for desired nodes * @returns Promise that resolves to an object containing the first matching node and its path, or null if no match */ /** * Find the first node that matches the predicate function using depth-first search. * Returns the first matching node along with its path through the Structured Text tree, or null if no match is found. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Asynchronous function that tests each node. Should return true for desired nodes * @returns Promise that resolves to an object containing the first matching node and its path, or null if no match */ export declare function findFirstNodeAsync(input: StructuredTextDocumentOrNode, predicate: NodePredicate>, parent?: AllNodesInTree | null, path?: TreePath): Promise<{ node: AllNodesInTree; path: TreePath; } | null>; /** * Prune the Structured Text tree, removing nodes that don't match the predicate. * Creates a new tree structure containing only nodes that pass the predicate test. * Maintains the Structured Text tree hierarchy - if a parent node is kept, its structure is preserved. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document * @param predicate - Synchronous function that tests each node. Nodes returning false are removed * @returns The pruned document, or null if root node is filtered out */ export declare function filterNodes(input: Document, predicate: NodePredicate, parent?: AllNodesInTree | null, path?: TreePath): Document | null; /** * Prune the Structured Text tree, removing nodes that don't match the predicate. * Creates a new tree structure containing only nodes that pass the predicate test. * Maintains the Structured Text tree hierarchy - if a parent node is kept, its structure is preserved. * * @template T - The type of nodes in the Structured Text tree * @param input - A specific DAST node * @param predicate - Synchronous function that tests each node. Nodes returning false are removed * @returns The pruned node, or null if node is filtered out */ export declare function filterNodes(input: T, predicate: NodePredicate, parent?: AllNodesInTree | null, path?: TreePath): T | null; /** * Prune the Structured Text tree, removing nodes that don't match the predicate. * Creates a new tree structure containing only nodes that pass the predicate test. * Maintains the Structured Text tree hierarchy - if a parent node is kept, its structure is preserved. * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document * @param predicate - Asynchronous function that tests each node. Nodes returning false are removed * @returns Promise that resolves to the pruned document, or null if root node is filtered out */ export declare function filterNodesAsync(input: Document, predicate: NodePredicate>, parent?: AllNodesInTree | null, path?: TreePath): Promise | null>; /** * Prune the Structured Text tree, removing nodes that don't match the predicate. * Creates a new tree structure containing only nodes that pass the predicate test. * Maintains the Structured Text tree hierarchy - if a parent node is kept, its structure is preserved. * * @template T - The type of nodes in the Structured Text tree * @param input - A specific DAST node * @param predicate - Asynchronous function that tests each node. Nodes returning false are removed * @returns Promise that resolves to the pruned node, or null if node is filtered out */ export declare function filterNodesAsync(input: T, predicate: NodePredicate>, parent?: AllNodesInTree | null, path?: TreePath): Promise; /** * Reduce the Structured Text tree to a single value by applying a reducer function to each node. * Uses pre-order traversal (parent is processed before its children). * The reducer function is called for each node with the current accumulator value. * * @template T - The type of nodes in the Structured Text tree * @template R - The type of the accumulated result * @param input - A structured text document, or a specific DAST node * @param reducer - Synchronous function that processes each node and updates the accumulator * @param initialValue - The initial value for the accumulator * @returns The final accumulated value */ export declare function reduceNodes(input: StructuredTextDocumentOrNode, reducer: (accumulator: R, node: AllNodesInTree, parent: AllNodesInTree | null, path: TreePath) => R, initialValue: R, parent?: WithChildren> | null, path?: TreePath): R; /** * Reduce the Structured Text tree to a single value by applying a reducer function to each node. * Uses pre-order traversal (parent is processed before its children). * The reducer function is called for each node with the current accumulator value. * * @template T - The type of nodes in the Structured Text tree * @template R - The type of the accumulated result * @param input - A structured text document, or a specific DAST node * @param reducer - Asynchronous function that processes each node and updates the accumulator * @param initialValue - The initial value for the accumulator * @returns Promise that resolves to the final accumulated value */ export declare function reduceNodesAsync(input: StructuredTextDocumentOrNode, reducer: (accumulator: R, node: AllNodesInTree, parent: AllNodesInTree | null, path: TreePath) => Promise, initialValue: R, parent?: WithChildren> | null, path?: TreePath): Promise; /** * Check if any node in the Structured Text tree matches the predicate function. * Returns true as soon as the first matching node is found (short-circuit evaluation). * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Synchronous function that tests each node. Should return true for matching nodes * @returns True if any node matches, false otherwise */ export declare function someNode(input: StructuredTextDocumentOrNode, predicate: NodePredicate, parent?: WithChildren> | null, path?: TreePath): boolean; /** * Check if any node in the Structured Text tree matches the predicate function. * Returns true as soon as the first matching node is found (short-circuit evaluation). * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Asynchronous function that tests each node. Should return true for matching nodes * @returns Promise that resolves to true if any node matches, false otherwise */ export declare function someNodeAsync(input: StructuredTextDocumentOrNode, predicate: NodePredicate>, parent?: WithChildren> | null, path?: TreePath): Promise; /** * Check if every node in the Structured Text tree matches the predicate function. * Returns false as soon as the first non-matching node is found (short-circuit evaluation). * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Synchronous function that tests each node. Should return true for valid nodes * @returns True if all nodes match, false otherwise */ export declare function everyNode(input: StructuredTextDocumentOrNode, predicate: NodePredicate, parent?: WithChildren> | null, path?: TreePath): boolean; /** * Check if every node in the Structured Text tree matches the predicate function. * Returns false as soon as the first non-matching node is found (short-circuit evaluation). * * @template T - The type of nodes in the Structured Text tree * @param input - A structured text document, or a specific DAST node * @param predicate - Asynchronous function that tests each node. Should return true for valid nodes * @returns Promise that resolves to true if all nodes match, false otherwise */ export declare function everyNodeAsync(input: StructuredTextDocumentOrNode, predicate: NodePredicate>, parent?: WithChildren> | null, path?: TreePath): Promise; export {};