Compile one draft from several tagged drafts

Hi there,

I would like some advice as how to approach this workflow : during the day I write several tagged drafts, for example #morning, #afternoon, etc . At the end of the day I would like to merge them all into one draft following a template containing the same template tags , for example :

This I have done this morning : [[ morning ]]
And this afternoon : [[afternoon]]

Obviously I would like the content of each tagged draft to fill in the template tags .

Any idea on how to do that ? Or maybe there is a better approach to update the one draft continuously throughout the day without having to merge a bunch of them ?

Thank you :grinning:

  1. Do you always have one and only one draft for each tag? Could you have no draft for a tag or multiple drafts for a single tag?
    • if the former, what should happen (be the output) when there is no draft?
    • If the latter, how should they be sorted/prioritised/concatenated?
  2. Would you always run processing same day, or would you want to run processing for several days, the previous day, etc?
  3. What should happen to processed drafts? Delete/archive, un-tag/re-tag/additional tag.

Appending to a section in a daily draft throughout the day could also be an option.

The question of options comes down to:

  • what you are ultimately wanting to achieve.
  • what automation and flexibility you would want to use.
  • what your personal preferences for processing are.

Hey @sylumer , thank you for your reply .

Here is what I ultimately want to achieve, throughout the day I add information, at the moment in a new draft that I tag manually . At the end of the day I compile all that information in one draft/journal note that I send to Obsidian . To be a bit more precise about that daily compiled note, it contains YAML headers, those YAML headers are actually populated by the individual drafts I gather throughout the day . In an ideal scenario I would select all the drafts in my workspace and merge them all in a template containing template tags, those template tags would be the same as the individual tags from each separate drafts that make up the final draft .

To answer your questions :

  1. I could have multiple drafts with the same tag, if so, they should just concatenate in one string ( by date would even be better )
  2. Same day processing , right now everything is based on the current day with links to day before and after, so yeah, save a headache and i’ll do same day processing :slight_smile:
  3. just archive and untag

I hope this make sense, I can’t think of a better workflow to compile atomic information throughout the day, but if you have a better idea as how to constantly edit the same draft, I would be very interested as well …

Thank you :slight_smile:

Have you solved this, or still need help with it?

Here’s a worked example.

I create a template based on the one you shared in the original post. In it I have added three tags, but I have added them in the format +{tag}+. The double square brackets defines a cross-link in Drafts, and so could get confusing very quickly. Also you had variations in your tags between including spaces between the tag and the square brackets; I’ve standardised on no spacing in the delimiter string.

Expand to show example template
This is the template: 2C3231AD-E047-421C-B268-4A55016B9AFE

---

In the morning I did:
+{morning}+

In the afternoon I did:
+{afternoon}+

In the evening I did:
+{evening}+

---

At the top of my template, you’ll see it has a random looking string. This is the unique identifier of the draft (you can copy it in the info section of any draft), and is shown here just for illustrative purposes. The key parts really are the inclusion of those substitution tags.

I’ve created a function that will scan through the template for tags. Using those tags it will find drafts created today with each tag (sorted in creation order). The content of those drafts is concatenated using single new line characters between them. Should none be found “Nothing.” will be used instead. The content is then substituted in for the tags in the template (multiple times if the tag is in the template multiple times).

The processed drafts then have the tags removed and are put into the archive. I think you would be better off not removing the tags, so if you want to eliminate that behaviour add “//” to the start of the drMatched.removeTag(strDailyTag); line near the end of the function. The reason is then you know what was added to what section if there is a later issue. There’s certainly no harm in retaining it as the searching is only being carried out in the inbox, things being in the archive being assumed to have already been processed.

Expand to show constructDaily Function
function constructDaily(p_strTemplateUUID)
{
	//Get the tag matches
	let drTemplate = Draft.find(p_strTemplateUUID);
	let regexDailyTag = /\+{(.*)}\+/g;
	let astrDailyTags = drTemplate.content.match(regexDailyTag);
	astrDailyTags = astrDailyTags.map(strDailyTag => {return strDailyTag.replace(/\+{|}\+/g, '');});

	//Set the output
	let strOutput = drTemplate.content;

	//Process each substitution tag in the template
	astrDailyTags.map(strDailyTag => {
		//Build query date for today
		let qdCreated = new QueryDate();
		qdCreated.field = "created"
		qdCreated.type = "absolute"
		qdCreated.date = Date.today();

		//Make a temporary Workspace
		let wsDaily = Workspace.create();
		wsDaily.setInboxSort("created", false, false);

		//Technically we could have something generate a future dated draft, so
		//for the sake of one line, let's be specific.
		wsDaily.startDate = qdCreated;
		wsDaily.endDate = qdCreated;

		//Apply a tag filter
		wsDaily.tagFilter = strDailyTag;

		//Get the drafts in the Workspace and concatenate their content
		let adrDailyTagged = wsDaily.query("inbox");
		let strDailyTaggedContent = adrDailyTagged.map(drMatch => {return drMatch.content}).join("\n");

		//Account for no tags that day
		if(strDailyTaggedContent == "") strDailyTaggedContent = "Nothing.";

		//Substitute in the concatenated results to the output
		let reDailyTag = new RegExp("\\+{" + strDailyTag + "}\\+","g");
		strOutput = strOutput.replace(reDailyTag,strDailyTaggedContent);

		//Untag and archive the drafts in the workspace
		adrDailyTagged.map(drMatched => {
			drMatched.removeTag(strDailyTag);
			drMatched.isArchived = true;
			drMatched.update();
		})
	});

	//Output built from template content to new draft
	let drNew = Draft.create();
	drNew.content = strOutput;
	drNew.update();

	//Load the new draft
	editor.load(drNew);
	editor.activate();
}

The function takes a single parameter. The UUID of the template draft, the one I showed in the template so you could see how it is used later … here. Therefore for the example template, my function call looks like this:

constructDaily("2C3231AD-E047-421C-B268-4A55016B9AFE");

Putting the function and the function call together in an action is just a case of adding them sequentially into a single script step within an action.

To use this, you just need to edit the action and set that UUID to the UUID of your template draft. The function call line is the last line of the action step.

Hope that helps.

1 Like

Hi @sylumer ,

Thank you for your time, this is exactly what I was after and nicely packaged up .
I appreciate it , awesome work :+1: