Action

Append to Craft Note with Date

Posted by FlohGro, Last update 8 months ago

UPDATES

8 months ago

Date fixed

show all updates...

8 months ago

Date fixed

10 months ago

fixed date format

10 months ago

description update

11 months ago

Added date links to todays note

over 1 year ago

updated description

created by @FlohGro / more on my Blog

Append to Craft Note with Date

This action appends the content of the current draft to a selectable document in one of your Craft spaces. The current date will be prefixed before the content.

You can configure the documents which should be selectable in the prompt very easily.

Follow the instructions to configure the action before you use it.

[Configuration]

the first time you run the action, it will create a new draft titled “Append / Prepend to Craft Action Configuration”. Think of this draft as the engine or library behind the Draft Action. (You can tag and archive this draft in the Drafts after configuration to hide it in your day to day work.)

Now you can configure to which Craft documents the action can append/prepend:

  • Open the Document / Page you want to add to the Menu of this action in the Craft app.
  • click the three dots in the upper right corner and select “Share & Export”, scroll down and click “Copy Markdown Deeplink”
  • In Drafts, open the draft “Append / Prepend to Craft Action Configuration” and paste that Markdown Deeplink into it (I’d recommend a bullet list for each page / 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 / page you are likely to append/prepend content to
  • To test your set up, create a new Draft and run the Append / Prepend action. You should see a menu of the Craft documents in your configuration. Select the Craft document to append/prepend the content 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

Here is a small example to illustrate how a configuration Draft 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])

This action will also work for documents in different spaces.

ADDITIONAL NOTES

There are several actions to prepend / append contents to Craft documents in the action directory. All of these actions share the same configuration draft titled “Append / Prepend to Craft Action Configuration”

Here is a list of the related actions:

If you want to have different destinations in craft for those actions or e.g. want to have separate personal and work configuration drafts you can edit the line const configurationDraftTitle = "Append / Prepend to Craft Action Configuration" in the script step and change the title there.

[Usage]

This action supports you for many different use cases. You can e.g. append / prepend notes and thoughts to a list of gift ideas, a list of places you want to visit or a list of things you are grateful for. You can also create a document in Craft for regular meetings you have and add things you want to mention in the meetings to this document by using this action.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    // created by @FlohGro
    
    // append to Craft note
    const configurationDraftTitle = "Append / Prepend to Craft Action Configuration"
    
    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 content = createCraftDate() + ": " + 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()
            }
        }
    }
    
    function createCraftDate() {
    	let date = new Date();
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let dayStr = ""
        if (day < 10) {
            dayStr = "0" + day
        } else {
            dayStr = day
        }
        let monthStr = ""
        if (month < 10) {
            monthStr = "0" + month
        } else {
            monthStr = month
        }
        let dateStr = year + "." + monthStr + "." + dayStr
        return "[" + dateStr + "](day://" + dateStr + ")"
    }

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.