Append to Craft Note with date

Posted by FlohGro, Last update 4 months ago


4 months ago

updated description

Append Draft to Craft Note with date

  • this action appends the content of the current draft to a document in a Craft workspace
  • the content of the draft will be prefixed with the current date in the craft document
  • when you run the action after its configuration, you will be prompted to append Craft documents of your choice


  • Install this Drafts Action
  • Run the Action to create the Configuration Draft note
  • Add Markdown Deeplinks to Craft Document/Block to Configuration document
  • Use Drafts Action as needed


  • the first time you run the action, it will create a new draft in the Drafts App titled “Append / Prepend to Craft Action Configuration”. Think of this draft as the engine behind the Draft Action. (You can tag and archive this draft in the Draft App after configuration to hide it in your day to day work.)
  • To configure which Craft documents you can append/prepend:
    • In Craft, open the Document / Block you want to add to the Menu of this action.
    • click the three dots in the upper right corner, select “Share & Export,” scroll down and click “Copy Markdown Deeplink”
    • In Drafts, paste that Markdown Deeplink into the Configuration Draft (I’d recommend a bullet list for each block / document, however it’s absolutely necessary to have a new line for each item which should appear in the menu).
    • Repeat this for each Craft document you are likely to append/prepend
    • To test your set up, create a new Draft and select the Append/Prepend action. You should see a menu of the Craft documents in your configuration. Select the Craft document to append/prepend and that’s it!
    • note: if you want to add contents to a block you should create this block as a subpage or add content into it in Craft first to make sure that this works


  • This action will prefix the prepended content with [date in ISO format]:
    • if you want to change the format of the date, change the „dateFormat“ 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
  • there are several actions to prepend / append contents to Craft documents in the action directory:
  • a small example to illustrate how a configuration Dr aft could look like:
# 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


  • 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- "
        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")
    } 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")
    } 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) {
                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")
        } else {
            // there one or more matches, show the prompt and proceed according to the users selection
            if ( {
                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.waitForResponse = false
            } else {
                // no block selected by the user
                console.log("user did not select a Craft note")


  • 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.