Action

Add to Obsidian Daily Note Section

Posted by FlohGro, Last update about 1 month ago

created by @FlohGro / more on my Website

Add to Obsidian Daily Note Section

This action will add the contents of the current draft into the configured section in todays Daily Note in Obsidian.
If the configured section is not present, you can choose if the action should append or prepend the content. You can also configure if you want to add a text prefix (e.g. to add a todo or a bullet point to the daily note). If you want to use different prefixes, you can duplicate the action and configure and rename the duplicate accordingly.

[Configuration]

Before you can use the action you need to configure it depending on your setup in Obsidian and personal preference.

All configuration parameters are adapted in the „Define Template Tag“ steps in the Action. To change the configurations edit them to match your setup - don’t change the names of the template tags since the action won’t work anymore afterwards. The following parameters must be adapted:

  • obsidianVaultBookmarkName: the name of the folder bookmark that will be created to access the files in your vault (see the Drafts documentation for more information about Folder Bookmarks). When you run the Action the first time it should prompt you to select a folder for that bookmark. Default: Obsidian Vault
  • dateFormat: the date format of your daily notes in Obsidian. You need to use the Drafts specific notation documented here Default: %Y-%m-%d which matches YYYY-MM-DD in Obsidian
  • dailyNotesFolderPath: the path where obsidian creates the daily notes (matches the setting for „New file location“ in the daily notes settings section). Default: empty
  • dailyNotesSection: the section in the daily note to where you want to add the content of the current draft. the section must be unique in the Daily Note. Default: UNCONFIGURED
  • textAddPrefix: an optional prefix that will be prepended to the content of the Draft

IMPORTANT: If you‘re running the Action the first time and it shows an error that the file is not existing even when the file exists in Obsidian then please go into the Settings of Drafts, navigate to „Bookmarks“ and make sure that the configured bookmark name is not set to „-unknown-“. If it is set to Unknown then tap „Select Folder“ and navigate to your Obsidian Vault in the files UI, select it and tap „Done“ in the upper right corner.

[Usage]

The action directly accessed the files so Obsidian won’t be opened when adding something to the daily note. If you didn’t open obsidian on that day, the daily note file will not be created or synced into the folder. Make sure to open Obsidian on the device if you see the alert that the file is missing. This is to prevent sync issues and ensure that potential templates for daily notes are applied first.

Simply run the the Action to add the content of the current draft into the configured section of your Daily Note in Obsidian. When you e.g. want to use the Action to add tasks to your Daily Note then you need to configure - [ ] as textAddPrefix. If you have different usecases, just duplicate the Action and configure it differently.


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

Buy Me A Coffee

Steps

  • defineTemplateTag

    name
    obsidianVaultBookmarkName
    template
    Obsidian Vault
  • defineTemplateTag

    name
    dateFormat
    template
    %Y-%m-%d
  • defineTemplateTag

    name
    dailyNotesFolderPath
    template
  • defineTemplateTag

    name
    dailyNotesSection
    template
    UNCONFIGURED
  • defineTemplateTag

    name
    textAddPrefix
    template
  • script

    // add to section in obsidian daily note
    
    const obsidianVaultBookmarkName = draft.processTemplate("[[obsidianVaultBookmarkName]]")
    const dateFormat = draft.processTemplate("[[dateFormat]]");
    
    let dailyNoteFileName = draft.processTemplate("[[date|" + dateFormat + "]]") + ".md";
    
    const dailyNotesFolderPath = draft.processTemplate("[[dailyNotesFolderPath]]") + "/";
    const dailyNoteSection = draft.processTemplate("[[dailyNotesSection]]") + "\n";
    
    
    dailyNoteFileName = dailyNotesFolderPath + dailyNoteFileName //"2022\.10\.25.md"
    let bookmark = Bookmark.findOrCreate(obsidianVaultBookmarkName);
    
    
    const configEntryPrefix = "- "
    const textAddPrefix = draft.processTemplate("[[textAddPrefix]]");
    const textAddMode = draft.processTemplate("[[textAddMode]]");
    
    let fm = FileManager.createForBookmark(bookmark);
    
    if (fm.exists(dailyNoteFileName)) {
        // file exists
        let fileContent = fm.readString(dailyNoteFileName)
        let splits = fileContent.split(dailyNoteSection)
    
        let adaptFileContent = true;
        if (splits.length == 2) {
            // the section is found
            splits[1] = "\n" + draft.content + "\n" + splits[1]
    
            fileContent = splits.join(dailyNoteSection);
    
        } else if (splits.length == 1) {
            // the configured section was not found in the daily note let the user decide what to do:
            let p = new Prompt()
            p.title = "Section not existing"
            p.message = "the configured section " + dailyNoteSection + " is not existing in the Daily Note right now. Select one of the following options:"
            p.addButton("append draft to Daily Note", 1)
            p.addButton("prepend draft to Daily Note", 2)
            p.addButton("abort adding draft to Daily Note", 0)
            p.isCancellable = false
    
            p.show()
    
            switch (p.buttonPressed) {
                case 0:
                    context.cancel("aborted by user");
                    app.displayWarningMessage("Aborted adding to Daily Note");
                    break;
                case 1:
                    fileContent = fileContent + "\n\n" + draft.content;
                    break;
                case 2:
                    fileContent = draft.content + "\n\n" + fileContent;
                    break;
            }
        } else if (splits.length > 2) {
            // section is found multiple time, user should clean up
            const errorStr = "Section \"" + dailyNoteSection + "\" was found multiple times in the Daily Note. Clean up this mess and try again"
            context.fail(errorStr)
            alert("Error:\n" + errorStr)
            adaptFileContent = false;
        }
        if (adaptFileContent) {
    
            if (!fm.writeString(dailyNoteFileName, fileContent)) {
                console.log("failed writing content to file")
                alert("failed writing content to file - this should not happen. please contact FlohGro via Twitter or the Drafts Forum.")
                context.fail()
            }
        }
    
    } else {
        // file does note exist
        console.log("daily notes file does not (yet) exist")
        alert("daily notes file does not (yet) exist, create it in obsidian")
        context.fail()
    }

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.