Action

Append to Craft Note with date

Posted by @FlohGro, Last update 26 days ago

Append Draft to Craft Note with date

  • this action appends the content of the current draft to a document in a space
  • the configuration is very simple and requires only manual interaction when you want to add more notes or blocks from craft to append / prepend to
  • when you run the configured action, you will see a prompt which presents the names / titles of blocks or documents. You can press a button and the content of the current draft will be appended to the selected block
  • the action will extract the spaceID and blockID out of the configuration which is done in a separate draft so there is no need for you to adapt the script of the action
  • additionally this action will prefix the appended content with [date in ISO format]:
    • if you want to change the format of the date, change the „dateFromat“ variable to the format you want, refer to template docs in the „Dates and Locations“ section. you only need to specify the format, not the complete template string

Configuration of the Blocks / Notes

  • the configuration of the selectable documents is done in a separate draft note to simplify the action
  • on the first run the action will create a new draft with the title Append / Prepend to Craft Action Configuration (you can change this if you want by simply changing the string of the variable configurationDraftTitle in the script). You can tag and archive the created draft after configuration to hide it in your day to day work.
  • to simplify the configuration the „Append to Craft Note“ action uses the same configuration Draft like the „Prepend to Craft Note“. If you want to use different notes for Append & Prepend, just configure a different name in both actions
  • adding blocks and notes is very easy, you simply copy the “Markdown Deeplink” to a block or document in Craft and add it to a new line in the configuration draft.
    • you can get the Markdown Deeplink by selecting the block or open the document you want to add, then go to the share menu and select “Copy Markdown Deeplink”.
  • I recommend to use a bullet list (a bullet for each block / note)
    • it is absolutely necessary to have a new line for each block.
  • a small example to illustrate this:
# Append / Prepend to Craft Action Configuration

- [MOVIE LIST](craftdocs://open?blockId=[a-complex-blockId]&spaceId=[the-complex-spaceId-from the block])
- [BOOK LIST](craftdocs://open?blockId=[another-complex-blockId]&spaceId=[the-complex-spaceId-from the block])

If you find this useful you can Buy Me A Coffee

Steps

  • script

    // created by @FlohGro
    
    // append to Craft note
    const configurationDraftTitle = "Append / Prepend to Craft Action Configuration"
    
    const dateFromat = "%Y-%m-%d"
    
    var foundDraft = Draft.queryByTitle(configurationDraftTitle)
    
    if (foundDraft.length == 0) {
        // the configuration draft is currently not existing, we need to create it for the user so that he/she can configure it later
        let newDraft = new Draft()
        newDraft.content = "# " + configurationDraftTitle + "\n\n- "
        newDraft.update()
    
        console.log("created configuration draft with title: " + configurationDraftTitle + " since it was not existing")
    
        alert("created configuration draft with title: " + configurationDraftTitle + " since it was not existing\n\nplease configure it according to the actiondescription and run the action again")
        context.cancel()
    
    } else if (foundDraft.length > 1) {
        // there are several drafts with the specified title. The user needs to cleanup the mess to run this again.
        console.log("more than one configuration draft with title: " + configurationDraftTitle + " exist")
    
        alert("more than one configuration draft with title: " + configurationDraftTitle + " exist\n\nplease clean up the mess (merge these two Drafts and run the action again")
        context.cancel()
    } else {
        // the configuration draft draft exists, which is great. We can work with that
    
        let p = new Prompt()
    
        p.title = "append to which block?"
    
        let lines = foundDraft[0].content.split("\n")
        const regex = /\[(.+)\]\(craftdocs:\/\/open\?blockId=([^&]+)\&spaceId=([^\)]+)\)/
        let matchCount = 0
        for (line of lines) {
            let match = line.match(regex)
            if (match) {
                matchCount++
                let blockTitle = match[1]
                let blockID = match[2]
                let spaceID = match[3]
                p.addButton(blockTitle, [spaceID,blockID])
            }
        }
        if (matchCount == 0) {
            // no matches where found, the prompt won't contain any useable button, the user needs to insert at least one Craft Markdown Deeplink
            console.log("no markdown deeplinks to Craft Blocks / Notes where found in the configuration draft with title: " + configurationDraftTitle)
            alert("no markdown deeplinks to Craft Blocks / Notes where found in the configuration draft with title: " + configurationDraftTitle + "\n\nplease add markdown deeplinks to craft notes as described in the description and run the action again")
            context.cancel()
        } else {
            // there one or more matches, show the prompt and proceed according to the users selection
    
            if (p.show()) {
                let spaceID = p.buttonPressed[0]
                let parentBlockID = p.buttonPressed[1]
    
                let title = draft.displayTitle
                let templateString = "[[date|" + dateFromat + "]]"
    				let date = draft.processTemplate(templateString)
                let content = date + ": " + draft.processTemplate("[[draft]]")
    
                content = encodeURIComponent(content)
    
                const baseURL = "craftdocs://createblock?"
    
                // index needed as high number to append the content at the end of a craft document
                const index = 200000
                var cb = CallbackURL.create()
                cb.baseURL = baseURL
                cb.addParameter("spaceId", spaceID)
                cb.addParameter("parentBlockId", parentBlockID)
                cb.addParameter("index", index)
                cb.addParameter("content", content)
                //cb.addParameter("folderId","")
                cb.waitForResponse = false
    
                cb.open()
            } else {
                // no block selected by the user
                console.log("user did not select a Craft note")
                context.cancel()
            }
        }
    }

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.