I found an excellent draft action on the directory which using the notion scripting option to add a new page to a database. I have multiple workspaces in notion and I have been able to configure the normal notion actions, using the credentials identifier, to be able to add into different workspaces but I am unable to get the script option to work across multiple workspaces. I have one that works in one space but I tried to copy it and change the database id to another workspace and I was unable to get it work. I no it must be something to do with authentication but cannot seem to figure it out.
Here is script:
// Notion Add Page to Databse + MD
// adds page to notion db with MD converted to Notion block types
// Updated 22/3/22 - change to block properties
// Assumes that the title property of your db pages default is "Name", if not, change below
// Enter your database ID below
/* ----- database id: paste ID between "quotes" -----*/
const dbid = "**I INSERTED MY DATABASE ID HERE AS INSTRUCTED**"; // from db page link, between last "/" and "?"
// endpoint url
const endpoint = "https://api.notion.com/v1/pages";
//get draft title + content, remove blank lines
const lines = draft.content.replace(/$\s*\n+/gm, "\n").trim().split('\n');
const title = lines.shift().replace(/^# /, "");
// create page
const parent = { "database_id": dbid };
//"Name" refers to the title property of your db pages, default is "Name", if you have changed this, replace "Name" below
const properties = {
"Name": { "title": [ {"text": { "content": title}}]} ,
};
const children = [];
const page = { parent, properties, children };
//-----------------
function addBlock(line){
const block = {"object": "block", "type" : "paragraph"};
const anyPref = /^(\s*([-*+]|#+)\s+)/;
// map prefix to block type
const types = new Map ([
["to_do", /^(\s*[-*+]\s\[\s]\s+)/],
["bulleted_list_item", /^(\s*[-*+]\s+)/],
["heading_3", /^(\s*###\s+)/],
["heading_2", /^(\s*##\s+)/],
["heading_1", /^(\s*#\s+)/],
]);
// if any prefix
if (anyPref.test(line)) {
// get type
types.forEach( ( reg, name ) => {
if (reg.test(line)) {
block.type = name;
line = line.replace(reg, "");
}})
}
// complete block
block[block.type] = {"rich_text": [{ "type": "text",
"text": { "content" : line }}]};
children.push(block);
}
// ---------------------
//if lines, add child blocks
if (lines) {
lines.forEach(addBlock);
}
// create Notion instance to make request
let notion = Notion.create();
let response = notion.request({
"url": endpoint,
"method": "POST",
"data": page
});
// result
if (!response.statusCode == 200) {
alert(`Notion Error: ${response.statusCode}
${notion.lastError}`);
context.fail();
}