@@ -12,7 +12,6 @@ import { parsePageId } from './parse-page-id'
12
12
* If `rootSpaceId` is not defined, the space ID of the root page will be used
13
13
* to scope traversal.
14
14
*
15
- *
16
15
* @param rootPageId - Page ID to start from.
17
16
* @param rootSpaceId - Space ID to scope traversal.
18
17
* @param getPage - Function used to fetch a single page.
@@ -25,18 +24,24 @@ export async function getAllPagesInSpace(
25
24
{
26
25
concurrency = 4 ,
27
26
traverseCollections = true ,
28
- targetPageId
27
+ targetPageId,
28
+ maxDepth = Number . POSITIVE_INFINITY
29
29
} : {
30
30
concurrency ?: number
31
31
traverseCollections ?: boolean
32
32
targetPageId ?: string
33
+ maxDepth ?: number
33
34
} = { }
34
35
) : Promise < PageMap > {
35
36
const pages : PageMap = { }
36
37
const pendingPageIds = new Set < string > ( )
37
38
const queue = new PQueue ( { concurrency } )
38
39
39
- async function processPage ( pageId : string ) {
40
+ async function processPage ( pageId : string , depth = 0 ) {
41
+ if ( depth > maxDepth ) {
42
+ return
43
+ }
44
+
40
45
if ( targetPageId && pendingPageIds . has ( targetPageId ) ) {
41
46
return
42
47
}
@@ -94,7 +99,7 @@ export async function getAllPagesInSpace(
94
99
95
100
return true
96
101
} ) ) {
97
- void processPage ( subPageId )
102
+ void processPage ( subPageId , depth + 1 )
98
103
}
99
104
100
105
// traverse collection item pages as they may contain subpages as well
@@ -107,7 +112,7 @@ export async function getAllPagesInSpace(
107
112
108
113
if ( blockIds ) {
109
114
for ( const collectionItemId of blockIds ) {
110
- void processPage ( collectionItemId )
115
+ void processPage ( collectionItemId , depth + 1 )
111
116
}
112
117
}
113
118
}
0 commit comments