Action

Notion Database

Posted by ttxx, Last update 8 months ago

Base on the extension “Notion: add page to Database + MD”. Fixed some bugs.

Updated 08/04/2024

Adds draft as new page in Notion database , converts basic markdown to Notion block types (to do, list, headings 1-3).

To use:

  • ADD your DATABASE ID to the script.

For example: https://www.notion.so/usr-domain/e0095a1ef18112de94df998c521c9cab. Then the DATABASE ID is : e0095a1e-f181-12de-94df-998c521c9cab (8-4-4-4-12).

  • Make sure the DATABASE is shared with drafts.

For how to setup Drafts/Notion integration and how to get database iD , see Notion Integration Guide

Steps

  • script

    /* ----- database id: paste ID between "quotes" -----*/
    const dbid = ""; // from db page link,
    
    // 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 = { 
    					"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();
    }
    if (response.statusCode == 200) {
        console.log('Successfully inserted into Notion database');
      } else {
        alert(`Notion Error: ${response.statusCode} ${notion.lastError}`);
        console.log('Failed to insert into Notion database');
        console.log(response.statusCode + ': ' + response.error);
      }
     

Options

  • After Success Default , Tags: notion-added
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.