Skip to content

Commit 00f5978

Browse files
author
Sergei Orlov
committed
✨ Add support for extracting nested blocks
They are available under '[block][blockType].children'
1 parent 32cd5b8 commit 00f5978

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/notion-api/get-blocks.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const fetch = require("node-fetch")
22
const { errorMessage } = require("../error-message")
33

4-
exports.getBlocks = async ({ id, page, notionVersion, token }, reporter) => {
4+
exports.getBlocks = async ({ id, block, notionVersion, token }, reporter) => {
55
let hasMore = true
6-
let pageContent = []
6+
let blockContent = []
77
let startCursor = ""
88

99
while (hasMore) {
@@ -22,8 +22,17 @@ exports.getBlocks = async ({ id, page, notionVersion, token }, reporter) => {
2222
},
2323
})
2424
.then((res) => res.json())
25-
.then((res) => {
26-
pageContent = pageContent.concat(res.results)
25+
.then(async (res) => {
26+
for (let childBlock of res.results) {
27+
if (childBlock.has_children) {
28+
childBlock[childBlock.type].children = await this.getBlocks(
29+
{ id: block.id, block: childBlock, notionVersion, token },
30+
reporter,
31+
)
32+
}
33+
}
34+
35+
blockContent = blockContent.concat(res.results)
2736
startCursor = res.next_cursor
2837
hasMore = res.has_more
2938
})
@@ -32,7 +41,5 @@ exports.getBlocks = async ({ id, page, notionVersion, token }, reporter) => {
3241
}
3342
}
3443

35-
page.children = pageContent
36-
37-
return page
44+
return blockContent
3845
}

src/notion-api/get-pages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, r
1717
}).then((res) => res.json())
1818

1919
for (let page of db.results) {
20-
page = await getBlocks({ id: page.id, page, token, notionVersion }, reporter)
20+
page.children = await getBlocks({ id: page.id, block: page, token, notionVersion }, reporter)
2121
}
2222

2323
return db.results

0 commit comments

Comments
 (0)