Skip to content

Commit 41e4782

Browse files
feat: add maxDepth param to getAllPagesInSpace
1 parent 2decc27 commit 41e4782

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/notion-utils/src/get-all-pages-in-space.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { parsePageId } from './parse-page-id'
1212
* If `rootSpaceId` is not defined, the space ID of the root page will be used
1313
* to scope traversal.
1414
*
15-
*
1615
* @param rootPageId - Page ID to start from.
1716
* @param rootSpaceId - Space ID to scope traversal.
1817
* @param getPage - Function used to fetch a single page.
@@ -25,18 +24,24 @@ export async function getAllPagesInSpace(
2524
{
2625
concurrency = 4,
2726
traverseCollections = true,
28-
targetPageId
27+
targetPageId,
28+
maxDepth = Number.POSITIVE_INFINITY
2929
}: {
3030
concurrency?: number
3131
traverseCollections?: boolean
3232
targetPageId?: string
33+
maxDepth?: number
3334
} = {}
3435
): Promise<PageMap> {
3536
const pages: PageMap = {}
3637
const pendingPageIds = new Set<string>()
3738
const queue = new PQueue({ concurrency })
3839

39-
async function processPage(pageId: string) {
40+
async function processPage(pageId: string, depth = 0) {
41+
if (depth > maxDepth) {
42+
return
43+
}
44+
4045
if (targetPageId && pendingPageIds.has(targetPageId)) {
4146
return
4247
}
@@ -94,7 +99,7 @@ export async function getAllPagesInSpace(
9499

95100
return true
96101
})) {
97-
void processPage(subPageId)
102+
void processPage(subPageId, depth + 1)
98103
}
99104

100105
// traverse collection item pages as they may contain subpages as well
@@ -107,7 +112,7 @@ export async function getAllPagesInSpace(
107112

108113
if (blockIds) {
109114
for (const collectionItemId of blockIds) {
110-
void processPage(collectionItemId)
115+
void processPage(collectionItemId, depth + 1)
111116
}
112117
}
113118
}

0 commit comments

Comments
 (0)