From c16618307b2aec56ef42a9fdc70947d26537e6d4 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 17 Oct 2022 12:12:06 +0200 Subject: [PATCH 1/2] feat: implement support for block children paging this hasn't been a problem so far since blocks usually don't have more than 100 children. Fix this potential issue nonetheless while we're at it. --- src/NotionApiFacade.ts | 33 +++++++++++++++++++-------------- src/RecursiveBodyRenderer.ts | 5 ++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/NotionApiFacade.ts b/src/NotionApiFacade.ts index 5b4727d..721c2d2 100644 --- a/src/NotionApiFacade.ts +++ b/src/NotionApiFacade.ts @@ -49,10 +49,10 @@ export class NotionApiFacade { ...query, start_cursor: next_cursor || undefined, }) - ); + ); results.push(...response.results); - + next_cursor = response.next_cursor; } while (next_cursor); @@ -66,20 +66,25 @@ export class NotionApiFacade { } async listBlockChildren(blockId: string) { - const result = await this.withRetry( - async () => - await this.client.blocks.children.list({ - block_id: blockId, - }) - ); // todo: paging here? - - if (result.next_cursor) { - throw new Error( - `Paging not implemented, block ${blockId} has more children than returned in a single request` + const results = []; + + let next_cursor: string | null = null; + + do { + const response = await this.withRetry( + async () => + await this.client.blocks.children.list({ + block_id: blockId, + start_cursor: next_cursor || undefined, + }) ); - } - return result; + results.push(...response.results); + + next_cursor = response.next_cursor; + } while (next_cursor); + + return results; } printStats() { diff --git a/src/RecursiveBodyRenderer.ts b/src/RecursiveBodyRenderer.ts index 2d7f01a..251b9e3 100644 --- a/src/RecursiveBodyRenderer.ts +++ b/src/RecursiveBodyRenderer.ts @@ -20,8 +20,7 @@ export class RecursiveBodyRenderer { const childs = await this.publicApi.listBlockChildren(page.id); - // todo: paging - const renderChilds = childs.results.map( + const renderChilds = childs.map( async (x) => await this.renderBlock(x, "", context) ); const blocks = await Promise.all(renderChilds); @@ -47,7 +46,7 @@ export class RecursiveBodyRenderer { // blocks, see https://developers.notion.com/reference/retrieve-a-block // "If a block contains the key has_children: true, use the Retrieve block children endpoint to get the list of children" const children = block.has_children - ? (await this.publicApi.listBlockChildren(block.id)).results + ? (await this.publicApi.listBlockChildren(block.id)) : []; const childIndent = indent + " ".repeat(parentBlock?.childIndent || 0); From 6da3627993f3823dc43d80b654dd126708f4ef6f Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 17 Oct 2022 12:14:17 +0200 Subject: [PATCH 2/2] chore: bump version to v0.11.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 361c857..7d64c0f 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.11.0", + "version": "0.11.1", "name": "@meshcloud/notion-markdown-cms", "engines": { "node": ">=14"