UNPKG

notion-page-tree

Version:

Recursively fetch nested Notion pages from the root page/database/block node.

39 lines (38 loc) 1.43 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildPageTreeToDepth = void 0; var buildPageTreeToDepth = function (collection, rootId, maxDepth) { if (maxDepth === void 0) { maxDepth = Infinity; } var root = __assign({}, collection[rootId]); var queue = []; queue.push(root); while (queue.length > 0) { var currentNode = queue.shift(); if (currentNode !== undefined && currentNode.children) { currentNode.children = currentNode.children.map(function (child) { // console.log(collection[child as string].depth - root.depth); if (collection[child].depth - root.depth < maxDepth) { var childRef = __assign({}, collection[child]); queue.push(childRef); return childRef; } else { return child; } }); } } return root; }; exports.buildPageTreeToDepth = buildPageTreeToDepth;