I wish to place text after a ## tag in an obsidian file

I think this action should do what you want.

Set Up

In order for the action to work, it has to know the location of the obsidian vault you want to use.

You need to have a Drafts Bookmark set up that points to the root of your Obsidian vault. in the script, the name of the bookmark is specified as “obsidian_drafts”. You can name your bookmark the same, but if you name it something different, you MUST change the OBSIDIAN_FOLDER_BOOKMARK constant to match the name that you wish to use.

Features

  • Works directly with the Markdown file, so is not opening Obsidian and relying on URL schema to drive the changes.
  • Automatically trims whitespace from content.
  • Will add multiple lines of content as multiple timestamped entries.
  • Basic options are available as constants in the insertintoDailyNote() function.
  • The default action does no processing of the draft after completion, but you could modify the standard action options to archive or trash a draft once processed if required.

Action Steps

The action consists of a single script step.

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");

Testing

I have successfully tested it with a draft that meets the criteria you have specified. This has included processing both single line draft content and multi-line draft content.


Hopefully, that covers everything.