From 17e66aa01018b4f91acdbdfe9029e1a673d42904 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 14 Mar 2024 22:13:27 +0800 Subject: [PATCH] append new highlights only --- .../api/src/services/integrations/notion.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/api/src/services/integrations/notion.ts b/packages/api/src/services/integrations/notion.ts index 21770fdc4..a4e8a84e5 100644 --- a/packages/api/src/services/integrations/notion.ts +++ b/packages/api/src/services/integrations/notion.ts @@ -356,12 +356,32 @@ export class NotionClient implements IntegrationClient { properties: notionPage.properties, }) - // append the children - if (notionPage.children) { - await this.client.blocks.children.append({ + const children = notionPage.children + if (children) { + // get the existing children + const response = await this.client.blocks.children.list({ block_id: existingPage.id, - children: notionPage.children, }) + if (response.results.length > 0) { + const existingChildren = + response.results as NotionPage['children'] + // delete the existing children from children + notionPage.children = children.filter( + (child) => + !existingChildren?.some( + (existingChild) => + existingChild.paragraph.rich_text[0].text.link?.url === + child.paragraph.rich_text[0].text.link?.url + ) + ) + } + // append the children + if (notionPage.children && notionPage.children.length > 0) { + await this.client.blocks.children.append({ + block_id: existingPage.id, + children: notionPage.children, + }) + } } return