Skip to content

Commit d956cd7

Browse files
michelle0927Lokeshchand33
authored andcommitted
Notion improvements - use notion-helper (#18797)
* use notion-helper * pnpm-lock.yaml * versions
1 parent 6f9e603 commit d956cd7

File tree

17 files changed

+124
-121
lines changed

17 files changed

+124
-121
lines changed

components/notion/actions/append-block/append-block.mjs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import notion from "../../notion.app.mjs";
22
import base from "../common/base-page-builder.mjs";
3+
import { appendBlocks } from "notion-helper";
34

45
export default {
56
...base,
67
key: "notion-append-block",
78
name: "Append Block to Parent",
8-
description:
9-
"Append new and/or existing blocks to the specified parent. [See the documentation](https://developers.notion.com/reference/patch-block-children)",
10-
version: "0.3.11",
9+
description: "Append new and/or existing blocks to the specified parent. [See the documentation](https://developers.notion.com/reference/patch-block-children)",
10+
version: "0.4.0",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,
@@ -81,16 +81,6 @@ export default {
8181

8282
return {};
8383
},
84-
methods: {
85-
...base.methods,
86-
chunkArray(array, chunkSize = 100) {
87-
const chunks = [];
88-
for (let i = 0; i < array.length; i += chunkSize) {
89-
chunks.push(array.slice(i, i + chunkSize));
90-
}
91-
return chunks;
92-
},
93-
},
9484
async run({ $ }) {
9585
const { blockTypes } = this;
9686
const children = [];
@@ -133,20 +123,13 @@ export default {
133123
return;
134124
}
135125

136-
const results = [];
137-
const chunks = this.chunkArray(children);
138-
139-
for (const chunk of chunks) {
140-
const { results: payload } = await this.notion.appendBlock(
141-
this.pageId,
142-
chunk,
143-
);
144-
results.push(payload);
145-
}
146-
147-
const totalAppended = results.reduce((sum, res) => sum + res.length, 0);
126+
const response = await appendBlocks({
127+
client: await this.notion._getNotionClient(),
128+
block_id: this.pageId,
129+
children,
130+
});
148131

149-
$.export("$summary", `Appended ${totalAppended} block(s) successfully`);
150-
return results.flat();
132+
$.export("$summary", "Appended blocks successfully");
133+
return response.apiResponses;
151134
},
152135
};

components/notion/actions/common/base-page-builder.mjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
} from "../../common/notion-meta-properties.mjs";
66
import NOTION_META from "../../common/notion-meta-selection.mjs";
77
import NOTION_PAGE_PROPERTIES from "../../common/notion-page-properties.mjs";
8+
import {
9+
createPage, createNotionBuilder,
10+
} from "notion-helper";
811
import { ConfigurationError } from "@pipedream/platform";
912

1013
export default {
@@ -299,5 +302,59 @@ export default {
299302
});
300303
return children.filter((child) => child !== undefined);
301304
},
305+
async buildPageFromDataSource({
306+
pageContent, parentDataSourceId, parentPageId, properties = [], icon, cover,
307+
}) {
308+
let pageBlocks = [];
309+
if (pageContent && pageContent.trim()) {
310+
try {
311+
pageBlocks = markdownToBlocks(pageContent);
312+
} catch (error) {
313+
throw new ConfigurationError(`Failed to convert Markdown content to Notion blocks: ${error.message}`);
314+
}
315+
}
316+
317+
// Build the Notion page using notion-helper
318+
let pageBuilder = createNotionBuilder({
319+
limitChildren: false,
320+
limitNesting: false,
321+
allowBlankParagraphs: true,
322+
});
323+
if (parentDataSourceId) {
324+
pageBuilder = pageBuilder.parentDataSource(parentDataSourceId);
325+
}
326+
if (parentPageId) {
327+
pageBuilder = pageBuilder.parentPage(parentPageId);
328+
}
329+
330+
for (const property of properties) {
331+
const propertyTypeCamelCase = property.type.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
332+
pageBuilder = pageBuilder[propertyTypeCamelCase](property.label, property.value);
333+
}
334+
335+
if (icon) {
336+
pageBuilder = pageBuilder.icon(icon);
337+
}
338+
339+
if (cover) {
340+
pageBuilder = pageBuilder.cover(cover);
341+
}
342+
343+
if (pageBlocks.length > 0) {
344+
pageBuilder = pageBuilder.loop(
345+
(page, block) => {
346+
return page.addExistingBlock(block);
347+
},
348+
pageBlocks,
349+
);
350+
}
351+
352+
const page = pageBuilder.build();
353+
const response = await createPage({
354+
client: await this.notion._getNotionClient(),
355+
data: page.content,
356+
});
357+
return response.apiResponse;
358+
},
302359
},
303360
};

components/notion/actions/complete-file-upload/complete-file-upload.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-complete-file-upload",
77
name: "Complete File Upload",
88
description: "Use this action to finalize a `mode=multi_part` file upload after all of the parts have been sent successfully. [See the documentation](https://developers.notion.com/reference/complete-a-file-upload)",
9-
version: "0.0.6",
9+
version: "0.0.7",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

components/notion/actions/create-database/create-database.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "notion-create-database",
88
name: "Create Database",
99
description: "Create a database and its initial data source. [See the documentation](https://developers.notion.com/reference/database-create)",
10-
version: "0.1.3",
10+
version: "0.1.4",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,

components/notion/actions/create-file-upload/create-file-upload.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-create-file-upload",
77
name: "Create File Upload",
88
description: "Create a file upload. [See the documentation](https://developers.notion.com/reference/create-a-file-upload)",
9-
version: "0.0.6",
9+
version: "0.0.7",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

components/notion/actions/create-page-from-database/create-page-from-database.mjs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
key: "notion-create-page-from-database",
1010
name: "Create Page from Data Source",
1111
description: "Create a page from a data source. [See the documentation](https://developers.notion.com/reference/post-page)",
12-
version: "1.0.4",
12+
version: "1.1.0",
1313
annotations: {
1414
destructiveHint: false,
1515
openWorldHint: true,
@@ -91,35 +91,33 @@ export default {
9191
* @returns the constructed page in Notion format
9292
*/
9393
buildPage(parentDataSource) {
94-
const meta = this.buildDataSourceMeta(parentDataSource);
9594
this.properties = utils.parseObject(this.properties);
9695
const properties = this.buildPageProperties(parentDataSource.properties);
97-
const children = this.createBlocks(this.pageContent);
98-
return {
99-
...meta,
100-
properties,
101-
children,
102-
};
96+
97+
const propertiesArray = [];
98+
for (const property of Object.values(parentDataSource.properties)) {
99+
if (properties[property.id]) {
100+
propertiesArray.push({
101+
label: property.name,
102+
type: property.type,
103+
value: this[property.name] || this.properties[property.name],
104+
});
105+
}
106+
}
107+
108+
return propertiesArray;
103109
},
104110
},
105111
async run({ $ }) {
106-
const MAX_BLOCKS = 100;
107112
const parentPage = await this.notion.retrieveDataSource(this.parentDataSource);
108-
const {
109-
children, ...page
110-
} = this.buildPage(parentPage);
111-
const response = await this.notion.createPage({
112-
...page,
113-
children: children.slice(0, MAX_BLOCKS),
114-
parent: {
115-
data_source_id: this.parentDataSource,
116-
},
113+
const properties = await this.buildPage(parentPage);
114+
const response = await this.buildPageFromDataSource({
115+
pageContent: this.pageContent,
116+
parentDataSourceId: this.parentDataSource,
117+
properties,
118+
icon: this.icon,
119+
cover: this.cover,
117120
});
118-
let remainingBlocks = children.slice(MAX_BLOCKS);
119-
while (remainingBlocks.length > 0) {
120-
await this.notion.appendBlock(response.id, remainingBlocks.slice(0, MAX_BLOCKS));
121-
remainingBlocks = remainingBlocks.slice(MAX_BLOCKS);
122-
}
123121
$.export("$summary", "Created page successfully");
124122
return response;
125123
},

components/notion/actions/create-page/create-page.mjs

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import utils from "../../common/utils.mjs";
21
import notion from "../../notion.app.mjs";
32
import base from "../common/base-page-builder.mjs";
43

@@ -7,7 +6,7 @@ export default {
76
key: "notion-create-page",
87
name: "Create Page",
98
description: "Create a page from a parent page. [See the documentation](https://developers.notion.com/reference/post-page)",
10-
version: "0.2.24",
9+
version: "0.3.0",
1110
annotations: {
1211
destructiveHint: false,
1312
openWorldHint: true,
@@ -49,64 +48,20 @@ export default {
4948
meta: this.metaTypes,
5049
});
5150
},
52-
methods: {
53-
...base.methods,
54-
/**
55-
* Builds a page from a parent page
56-
* @param parentPage - the parent page
57-
* @returns the constructed page in Notion format
58-
*/
59-
buildPage(parentPage) {
60-
const meta = this.buildPageMeta(parentPage);
61-
const children = this.createBlocks(this.pageContent);
62-
63-
const properties = {};
64-
if (this.title) {
65-
properties.title = {
66-
title: utils.buildTextProperty(this.title),
67-
};
68-
}
69-
70-
return {
71-
...meta,
72-
properties,
73-
children,
74-
};
75-
},
76-
splitChildrenArray(children) {
77-
const first100Children = children.slice(0, 100);
78-
const restOfChildren = children.slice(100);
79-
return {
80-
first100Children,
81-
restOfChildren,
82-
};
83-
},
84-
async appendChildren(pageId, children) {
85-
while (children.length) {
86-
const {
87-
first100Children, restOfChildren,
88-
} = this.splitChildrenArray(children);
89-
await this.notion.appendBlock(pageId, first100Children);
90-
children = restOfChildren;
91-
}
92-
},
93-
},
9451
async run({ $ }) {
95-
const parentPage = await this.notion.retrievePage(this.parent);
96-
const page = this.buildPage(parentPage);
97-
98-
// Notion restricts children array length to <= 100
99-
const {
100-
first100Children, restOfChildren,
101-
} = this.splitChildrenArray(page.children);
102-
page.children = first100Children;
103-
104-
const response = await this.notion.createPage(page);
105-
106-
if (restOfChildren.length) {
107-
await this.appendChildren(response.id, restOfChildren);
108-
}
109-
52+
const response = await this.buildPageFromDataSource({
53+
pageContent: this.pageContent,
54+
parentPageId: this.parent,
55+
properties: [
56+
{
57+
label: "title",
58+
type: "title",
59+
value: this.title,
60+
},
61+
],
62+
icon: this.icon,
63+
cover: this.cover,
64+
});
11065
$.export("$summary", "Created page successfully");
11166
return response;
11267
},

components/notion/actions/delete-block/delete-block.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-delete-block",
77
name: "Delete Block",
88
description: "Sets a Block object, including page blocks, to archived: true using the ID specified. [See the documentation](https://developers.notion.com/reference/delete-a-block)",
9-
version: "0.0.6",
9+
version: "0.0.7",
1010
annotations: {
1111
destructiveHint: true,
1212
openWorldHint: true,

components/notion/actions/duplicate-page/duplicate-page.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "notion-duplicate-page",
88
name: "Duplicate Page",
99
description: "Create a new page copied from an existing page block. [See the documentation](https://developers.notion.com/reference/post-page)",
10-
version: "0.0.21",
10+
version: "0.0.22",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,

components/notion/actions/list-file-uploads/list-file-uploads.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-list-file-uploads",
77
name: "List File Uploads",
88
description: "Use this action to list file uploads. [See the documentation](https://developers.notion.com/reference/list-file-uploads)",
9-
version: "0.0.6",
9+
version: "0.0.7",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

0 commit comments

Comments
 (0)