By François FOURRIER - 🌐 LinkedIn
Make offers a Notion module called "Update a Page Content" which allows you to create as many blocks as you want.
The problem is that it cannot be used programmatically to add the same number of blocks as you have paragraphs in your content.
As a result, you usually end up with a single block containing all your content coming from another module, which is quite frustrating when you want to manipulate the content of your Notion page as usual, meaning one paragraph per block.
1️⃣ The first solution is to use the default Make modules to add as many blocks as there are paragraphs in your content.
Below is an example scenario: the first module contains just text in a JSON object that is converted to text from HTML with the Text parser
module. Nothing fancy here, and it can be whatever text you want to add to your Notion page coming from your modules.
2️⃣ The 3rd module Set Variable
splits the content coming from Text Parser
to an array.
3️⃣ Then we add an iterator to iterates on each key of our Paragraphs
array defined previously.
4️⃣ Then we take the value of the paragraphs array to create a block each time using the module Append a Page Content
.
While this approach is simple and straightforward, you may notice that it can become very costly because creating a block here consumes one operation at a time ! So yes, if you have 30 carriage returns in your text, it will be 30 operations ! 🤔
The advantage of this method is that it also allows you to style each block independently (bold, italic, etc.).
The previous solution is acceptable, but it becomes much too costly when dealing with hundreds of Notion pages or more. So, by reviewing the Notion API documentation, I found an expression that can perform the entire previous process in a single operation!
Simple scenario to update a Notion page with multiple paragraph in a SINGLE operation.
You’ll have to copy/paste the expression below inside the Body field of the Notion Module Make an API Call
and replace <your_text>
with the raw text coming from previous modules in your scenario.
{"children": [{{join(split(replace(replace(your_text; "/[\\n]{1,}/g"; "||"); "/[^|]+(?:\\|[^|]+)*/g"; "{""object"":""block"",""type"":""paragraph"",""paragraph"":{""rich_text"":[{""type"":""text"",""text"":{""content"":""$&""}}]}}"); "||"); ",")}}]}
For the module configuration, be careful with all the fields, which must match exactly as they are displayed on the right! All fields are mandatory !
/v1/blocks/**<your_page_id>**/children
Not dashes inside <your_page_id>
!
2022-06-28
PATCH
(and not POST* ! 😉)
Key : Content-type
Value : application/json
Beware to fill all the mandatory fields and the PATCH method, not POST !
You don’t have to understand the expression to use it, as long as you just replace your_text
with… your text ! 😉 But if you want to understand the logic behind it, read further…
So, let's begin !
To append a new block to an existing page, you have to send JSON to the Notion API “Append block children” endpoint, as formatted below, and use "object": "block"
for EACH paragraph.
{
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Your content"
}
}
]
}
}
]
}
The trick here is to perform a "mass" search-and-replace on each paragraph of your content to add the necessary JSON code for it to work, and generate a valid "object": "block"
for each paragraph.