Action

Merlin's Wisdom Importer

Posted by FlohGro, Last update 19 days ago

created by @FlohGro / more on my Blog

Merlin’s Wisdom Importer

This action will retrieve a random item of Merlin Mann’s Wisdom Project (details here).
Then it displays a prompt where you either can load another random item, create a new draft from that item or just Cancel the action and come back later


If you find this useful and want to support me you can donate or buy me a coffe

Steps

  • script

    // Merlin's Wisdom Importer - created by @flohgro@social.lol
    
    const wisdomSourceUrl = `https://raw.githubusercontent.com/merlinmann/wisdom/master/wisdom.md`
    
    let wisdoms = []
    
    function run() {
        if (wisdoms.length == 0) {
            //nothing was retrieved yet, request the wisdom
            wisdoms = getWisdom()
            let fail = false
            if (!wisdoms) {
                fail = true
            }
            if (wisdoms.length == 0) {
                fail = true
            }
            if (fail) {
                app.displayErrorMessage("retrieving wisdom failed - make sure you have an internet connection")
                context.fail("retrieving wisdom failed - make sure to have an internet connection");
            }
        }
        let randWisdom = getRandomItem(wisdoms)
        presentOptionsForWisdomItem(randWisdom)
    }
    
    function getWisdom() {
        let http = new HTTP();
        // get the file
        let requestResult = http.request({
            "url": wisdomSourceUrl,
            "method": "GET"
        });
        // check if the result was successful
        let wisdomResultText = ""
        if (requestResult.success) {
            wisdomResultText = requestResult.responseText
        } else {
            return undefined;
        }
        // at this point we have the wisdom text in the wisdomResultText variable
        // now we need to find the correct markdown section
        let splits = wisdomResultText.split("## ")
        let wisdomSectionText = ""
        for (let split of splits) {
            if (split.startsWith("The Wisdom")) {
                wisdomSectionText = split
                break
            }
        }
        if (wisdomSectionText == "") {
            //something went wrong, couldn't find wisdomSection
            app.displayErrorMessage("something failed, check log")
            context.fail("could not find the wisdom section in the retrieved text. contact @flohgro@social.lol")
            return undefined
        }
    
        // now we need to get rid of all empty lines, separators (----) and the heading
        let wisdomSplits = wisdomSectionText.split("\n")
        // remove first split, this is the heading
        wisdomSplits.shift()
        // now filter out empty lines and separators and remvoe the dash at the beginning; store it in a new array
        let theWisdoms = []
        for (let split of wisdomSplits) {
            if (split.trim().length == 0 || split.trim() == "----") {
                continue
            } else {
                // add to new array without the first two characters = dash + space
                theWisdoms.push(split.slice(2))
            }
        }
        return theWisdoms
    }
    
    function getRandomItem(arr) {
        let index = Math.floor(Math.random() * (arr.length - 1))
        let randomItem = arr[index]
        // now tremove that item
        wisdoms = wisdoms.filter(item => item !== randomItem);
        return randomItem
    }
    
    function presentOptionsForWisdomItem(item) {
        let p = new Prompt()
        p.title = "Merlin's Wisdom Importer"
        p.message = item
        p.addButton("get new wisdom")
        p.addButton("create draft")
        if (p.show()) {
            if (p.buttonPressed == "create draft") {
                let d = new Draft()
                d.content = item + "\n-- Merlin Mann"
                d.update
                editor.load(d)
                return
            } else {
                run()
                return
            }
        }
    }
    
    run()

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.