Action

memo to Notion

Posted by posuke, Last update 12 months ago

This action allows you to transfer notes from Drafts to Notion with ease. The first line of your Drafts note becomes the title in Notion, and the rest is added as the body.

Steps

  • script

    function addPermanentNote(title) {
      let database_id;
      let credential = Credential.create("PermanentNoteDBID", "Enter DatabaseId for LiteratureNote.");
      credential.addTextField("database_id", "Database ID");
      if (credential.authorize()) {
        database_id = credential.getValue("database_id");
      } else {
        console.log("Failed to obtain credential.");
      }
    
      let endpoint = "https://api.notion.com/v1/pages"
      let data = {};
      data["parent"] = {"database_id": database_id}
      data["properties"] = {
        "Name": {
          "title": [
            { "text": { "content": title } }
          ]
        }
      }
    
      let response = notion.request({  
          "url": endpoint,
          "method": "POST",
          data: data
      });
      if (response.statusCode == 200) {
        console.log("Permanent Note Created SUCCESS!");
        let content = JSON.parse(response.responseText);
    
        return content["id"];
      } else {
        console.log(`Notion Error: ${response.statusCode} ${notion.lastError}`);  
        context.fail();  
      }
    }
    
    function createBlockObject(line) {
      let result = {"object": "block"};
      result["type"] = "paragraph"
      result["paragraph"] = {
              "rich_text": [ {
                  "type": "text",
                  "text": { "content": line }
             } ] }
    
      return result;
    }
    
    function addBlockToPermanentNote(page_id) {
      let endpoint = "https://api.notion.com/v1/blocks/" + page_id + "/children";
      let children = [];
      let lines = draft.lines;
    
      if(lines.length > 1){
        lines.map(line => children.push(createBlockObject(line)));
        let response = notion.request({
            "url": endpoint,  
            "method": "PATCH",
            data: { "children": children },
        });
        if (response.statusCode == 200) {
            console.log(page_id + ". Block add SUCCESS!");
            //console.log(lines);
            let content = JSON.parse(response.responseText);
            return content;
        } else {
            console.log(`Notion Error: ${result.statusCode} ${notion.lastError}`);  
            context.fail();  
        }
      }
    }
    
    function main() {
      let title = draft.title;
      let page_id = addPermanentNote(title);
      addBlockToPermanentNote(page_id);
    }
    
    var notion = Notion.create();  
    main();
    

Options

  • After Success Trash
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.