Action

Send to Readwise

Posted by Nathan Crowther, Last update 5 months ago

A Draft action to publish highlights to Readwise.

Make sure to update it with your token on line 52. Token can be fournd here: https://readwise.io/access_token

This action assumes the draft template starting on line 70 of the script.

Steps

  • script

    // Global Variables
    let RWArray = [];
    let d = draft;
    
    // Get Title & Author
    var title = d.displayTitle;
    var content = d.content;
    let author = content.match("Author: (.*)")[1];
    
    // Get Array of Quotes
    var content = d.content;
    var passages = content.split("## Quotes")[1];
    var passagesArray = passages.split("> ");
    passagesArray.shift(); // First entry in array is blank. Get rid of it.
    
    // Create Array of Objects
    passagesArray.forEach(addToRWArray);
    
    // Send items to Readwise
    sendToReadwise(RWArray);
    
    // Function to create JSON object out of highlight and add it to the final array.
    function addToRWArray(item){
        //Create an array with 2 items: the passage and the array
        var highlightArray = item.split("@Page: ");
        //alert("Highlight Array: " + highlightArray);
    
        // Set the passage location
        var highlightPageString = highlightArray[1].split("\n")[0];
        var highlightPage = parseInt(highlightPageString);
        //alert("HighlightPage: " + highlightPage);
        
        // Create JSON and add it to array
        var highlightObject = 
        {
            text: highlightArray[0],
            title: title,
            author: author,
            location_type: "page",
            location: highlightPage,
            highlighted_at: "2022-02-07T20:11:24+00:00"
        };
        //alert(JSON.stringify(highlightObject));
    
        RWArray.push(highlightObject);
        // alert({JSON.stringify(RWArray));
    }
    
    
    function sendToReadwise(RWArray) {
        // Create HTTP Request
        var rwToken = "YOUR READWISE TOKEN HERE"; //Get token here: https://readwise.io/access_token
        var http = HTTP.create();
    
        // Readwise Response
        var response = http.request({
            "url": "https://readwise.io/api/v2/highlights",
            "method": "POST",
            "data": {
            "highlights": RWArray,
            },
            "headers": {
                "Authorization": "Token " + rwToken,
                "Content-Type": "application/json",
            }
        });
    }
    
    /* Example Drafts Entry
    # Moby Dick
    Author: Herman Melville
    
    ## Quotes
    > Call me Ishmael
    @Page: 15
    
    > But don't ever call me an octopus.
    @Page: 15
    
    
    */

Options

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