Skip to main content

12.10.1 A scenario for transferring Notion content to a Google Drive file

To upload the Notion content to the created Google Drive file and save its ID, you need to create a scenario of 6 nodes:

  • (1) Schedule, to run the scenario, for example, once a week at the time of the desired time zone:
  • (2) Retrieve Block, to get the content of the selected page and its child pages. To get the content, including child pages, it is necessary to select the Retrieve Block Children switch:
  • (3) JavaScript, for processing the data of the Retrieve Block node. Since the Retrieve Block node returns a lot of system information, it is necessary to isolate the text from the data, including the text of the child pages:
// The main function of the JavaScript node, which will be executed with the input data.
export default async function run({execution_id, input, data}) {
// Parsing the JSON string from the data object with the key corresponding to node id "2"
const result = JSON.parse(data["{{2.\`result\`}}"]);
// Accessing the 'children' property of the result, which is expected to be an array of blocks
const blocks = result.children;

// Function that extracts and cleans text from an array of rich text objects
const extractRichText = (richTextArray) => {
// Maps over the rich text array, removing commas and quotes from the 'plain_text' property
// and then joins all pieces of text into a single string
return richTextArray.map(text => text.plain_text.replace(/,/g, '').replace(/["']/g, '')).join('');
};

// Recursive function that processes each block and its children to extract text
const processBlocks = (blocks, level) => {
let content = []; // Initializes an array to hold the extracted content

// Iterates over each block in the 'blocks' array
blocks.forEach(block => {
// Skips the current iteration if the block type is 'image'
if (block.type === "image") return;

// List of block types that contain text
const blockTypes = ['paragraph', 'heading_1', 'heading_2', 'heading_3', 'bulleted_list_item', 'numbered_list_item', 'to_do', 'quote'];

// Iterates over each block type and extracts text if present
blockTypes.forEach(type => {
if (block[type] && block[type].rich_text) {
// Extracts and cleans the text content from the rich text
const text = extractRichText(block[type].rich_text);
// Indents the text according to its level in the block hierarchy
const indentedText = ' '.repeat(level * 2) + text;
// Adds the indented text to the content array
content.push(indentedText);
}
});

// If the block has children, recursively process them with an incremented level
if (block.children && Array.isArray(block.children) && block.children.length > 0) {
// Processes the children of the block and concatenates their content
const childrenContent = processBlocks(block.children, level + 1);
// Merges the content from children with the current content array
content = content.concat(childrenContent);
}
});

return content; // Returns the array of extracted content
};

// Processes the top-level blocks with an initial indentation level of 0
const allTextContent = processBlocks(blocks, 0);
// Joins all text content pieces into a single string with spaces in between
const finalText = allTextContent.join(' ');

// Returns an object with a key 'textContent' holding the final concatenated text
return {
textContent: finalText
};
}
  • (4) Create a New Document from Text to create a file on Google drive with the text obtained in the JavaScript node. To configure the node, you need to perform authorization, determine the name of the file and, for example, specify the current date in the name. You also need to add the data of the previous node to the Text field {{$3.textContent}} :
  • (5) SetGlobalVariables, to create a global variable FileID and save the identifier of the created file in the value of the variable. The ID of the created file is the output parameter of the node Create New Document from Text:
  • (6) Webhook response, receiving a response when the scenario is successfully executed:

The result of executing the script will be a file created on Google Drive with the Notion content and writing its identifier to the global variable FileID: