Action

michaelw - Obsidian Journal Update

Posted by sylumer, Last update over 1 year ago - Unlisted

Steps

  • script

    function insertIntoDailyNote(p_strJournalAddition)
    {
    	// Settings
    	const ENTRY_SPACER = " ";
    	const OBSIDIAN_FOLDER_BOOKMARK = "obsidian_drafts";
    	const JOURNALPATH = "/Daily/";
    	const REGEX_SECTIONS = /(.*)(## Journal\n)(.*)(\n\n\n)(.*)/gs;
    
    	// Set up file access and get file content
    	let bmObisidian = Bookmark.findOrCreate(OBSIDIAN_FOLDER_BOOKMARK);
    	let fmObsidian = FileManager.createForBookmark(bmObisidian);
    	let strDailyNotePath = JOURNALPATH + draft.processTemplate("[[date]]") + ".md";
    	let strDailyNoteContent = fmObsidian.readString(strDailyNotePath);
    
    	// If the daily note does not exist, log it and bail out
    	if (strDailyNoteContent === undefined)
    	{
    		console.log(strDailyNotePath + " not found");
    		return false;
    	}
    
    	// Build new content and add it to the daily note
    	// - Input is processed per line and whitespace is trimmed
    	// - Inserting is before the two blank lines first found after the H2 Journal heading (Markdown)
    	// - Boolean success of the file updating is returned.
    	let strContentToInsert =  p_strJournalAddition.split('\n').map(s => draft.processTemplate("\n[[date|%R]]") + ENTRY_SPACER + s.trim()).join("");;
    	strDailyNoteContent = strDailyNoteContent.replace(REGEX_SECTIONS, "$1$2$3" + strContentToInsert + "$4$5");
    	return fmObsidian.writeString(strDailyNotePath, strDailyNoteContent);
    }
    
    // Add current draft to dail note in Obsidian as journal entries
    if (insertIntoDailyNote(draft.content)) app.displaySuccessMessage("Daily Note Updated");
    else app.displayErrorMessage("Failed to Update Daily Note");

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.