From d667bca658a2b79fa5d0afba9ef25f26a10cdfc2 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:02:57 +0200 Subject: [PATCH] feat(Notion Node): Option to simplify output in getChildBlocks operation (#7791) Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: Marcus --- .../nodes/Notion/BlockDescription.ts | 15 +++++++ .../nodes/Notion/GenericFunctions.ts | 43 +++++++++++++++++++ .../nodes-base/nodes/Notion/Notion.node.ts | 3 +- .../nodes/Notion/v2/NotionV2.node.ts | 11 +++++ .../nodes/Notion/v2/VersionDescription.ts | 2 +- 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/nodes/Notion/BlockDescription.ts b/packages/nodes-base/nodes/Notion/BlockDescription.ts index 00e0241921..f8de52cada 100644 --- a/packages/nodes-base/nodes/Notion/BlockDescription.ts +++ b/packages/nodes-base/nodes/Notion/BlockDescription.ts @@ -199,4 +199,19 @@ export const blockFields: INodeProperties[] = [ }, default: false, }, + { + displayName: 'Simplify Output', + name: 'simplifyOutput', + type: 'boolean', + displayOptions: { + show: { + resource: ['block'], + operation: ['getAll'], + }, + hide: { + '@version': [1, 2], + }, + }, + default: true, + }, ]; diff --git a/packages/nodes-base/nodes/Notion/GenericFunctions.ts b/packages/nodes-base/nodes/Notion/GenericFunctions.ts index 74fc8a068e..f1ce288c63 100644 --- a/packages/nodes-base/nodes/Notion/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Notion/GenericFunctions.ts @@ -39,6 +39,7 @@ export type SortData = { key: string; type: string; direction: string; timestamp const apiVersion: { [key: number]: string } = { 1: '2021-05-13', 2: '2021-08-16', + 2.1: '2021-08-16', }; export async function notionApiRequest( @@ -1067,3 +1068,45 @@ export function extractDatabaseMentionRLC(blockValues: IDataObject[]) { } }); } + +export function simplifyBlocksOutput(blocks: IDataObject[], rootId: string) { + for (const block of blocks) { + const type = block.type as string; + block.root_id = rootId; + + ['created_time', 'last_edited_time', 'created_by'].forEach((key) => { + delete block[key]; + }); + + try { + if (['code'].includes(type)) { + const text = (block[type] as IDataObject).text as IDataObject[]; + if (text && Array.isArray(text)) { + const content = text.map((entry) => entry.plain_text || '').join(''); + block.content = content; + delete block[type]; + } + continue; + } + + if (['child_page', 'child_database'].includes(type)) { + const content = (block[type] as IDataObject).title as string; + block.content = content; + delete block[type]; + continue; + } + + const text = (block[type] as IDataObject)?.text as IDataObject[]; + + if (text && Array.isArray(text)) { + const content = text.map((entry) => entry.plain_text || '').join(''); + block.content = content; + delete block[type]; + } + } catch (e) { + continue; + } + } + + return blocks; +} diff --git a/packages/nodes-base/nodes/Notion/Notion.node.ts b/packages/nodes-base/nodes/Notion/Notion.node.ts index 9ea6e41f8c..293c386ec0 100644 --- a/packages/nodes-base/nodes/Notion/Notion.node.ts +++ b/packages/nodes-base/nodes/Notion/Notion.node.ts @@ -13,12 +13,13 @@ export class Notion extends VersionedNodeType { group: ['output'], subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume Notion API', - defaultVersion: 2, + defaultVersion: 2.1, }; const nodeVersions: IVersionedNodeType['nodeVersions'] = { 1: new NotionV1(baseDescription), 2: new NotionV2(baseDescription), + 2.1: new NotionV2(baseDescription), }; super(nodeVersions, baseDescription); diff --git a/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts b/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts index 3cec33dd2d..4bb9afca54 100644 --- a/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts +++ b/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts @@ -26,6 +26,7 @@ import { notionApiRequest, notionApiRequestAllItems, notionApiRequestGetBlockChildrens, + simplifyBlocksOutput, simplifyObjects, validateJSON, } from '../GenericFunctions'; @@ -312,6 +313,16 @@ export class NotionV2 implements INodeType { ..._data, })); + const nodeVersion = this.getNode().typeVersion; + + if (nodeVersion > 2) { + const simplifyOutput = this.getNodeParameter('simplifyOutput', i) as boolean; + + if (simplifyOutput) { + responseData = simplifyBlocksOutput(responseData, blockId); + } + } + const executionData = this.helpers.constructExecutionMetaData( this.helpers.returnJsonArray(responseData as IDataObject), { itemData: { item: i } }, diff --git a/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts b/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts index 51e45a15d0..4917fa9f34 100644 --- a/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts +++ b/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts @@ -15,7 +15,7 @@ export const versionDescription: INodeTypeDescription = { name: 'notion', icon: 'file:notion.svg', group: ['output'], - version: 2, + version: [2, 2.1], subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume Notion API', defaults: {