Prepend to draft preserving first line (title)

Hi all,
my first post here. I am using an action to prepend a draft to an existing draft to form a daily log. (code below).

I do not want to overwrite the first line as this is the title of my draft - ‘Daily Notes’ for example.

I would prefer to use prepend rather than append so that the most recent notes are at the top of the draft. It would also be great if all notes added on a certain day get listed under a single date rather than adding the date every time I add something during the day.

So what I am hoping to achieve is something like:

Daily Notes

2020-09-23

Oh and I thought of adding this just before bed
I made this note later in the day
A note made over breakfast

2020-09-22

I added all these notes at the same time
This as well
And this

I am currently using the following action which has got me part way. Any tips?

thanks Kenny

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

const template = `[[date]]
[[draft]]

`;
const currentContent = draft.content.trim();
// replace the UUID here with the UUID you want to append too.
var d = Draft.find('F452AD5A-4BA9-49F8-AA40-B87029BD8ED7');
d.content = draft.processTemplate(template) + d.content;
d.update();
2 Likes

Maybe this will help with the first aspect?

function addAfterLine(p_draft, p_strAddText, p_intAfterLine)
{
	p_draft.content = p_draft.processTemplate(`[[line|..${p_intAfterLine}]]`) + `\n${p_strAddText}\n` + p_draft.processTemplate(`[[line|${p_intAfterLine + 1}..]]`);
	p_draft.update();
}

addAfterLine(Draft.find("E3968AD7-8D5F-422E-B607-A2D942BDF6B0"), "My extra text", 3);

The function splits a draft (denoted by the first parameter) at the end of a particular line (denoted by the third parameter), and inserts the required text (denoted by the second parameter) wrapped with newline characters.

The final line shows an example where the Draft find() function is used to pass in a draft object to the function based on a UUID, and insert the text “My extra text” after the third line (and before the fourth line).

Based on that I think you might do something like this for your case above, but I can’t see how your trimming gets used, so you may want to revised based on that.

const template = `[[date]]
[[draft]]
`;

let strInsertText = draft.processTemplate(template);

function addAfterLine(p_draft, p_strAddText, p_intAfterLine)
{
	p_draft.content = p_draft.processTemplate(`[[line|..${p_intAfterLine}]]`) + `\n${p_strAddText}\n` + p_draft.processTemplate(`[[line|${p_intAfterLine + 1}..]]`);
	p_draft.update();
}

addAfterLine(Draft.find("F452AD5A-4BA9-49F8-AA40-B87029BD8ED7"), strInsertText, 2);

Now if you want to go to the second part to add into a particular section multiple times per day that may or may not exist, I would adopt a different approach.

You are going to have to split the draft into sections and search for a matching section each time you want to add something. To help with that you might consider making each date a Markdown heading for example and you could then use some of Drafts’ navigation maker functionality. If not, maybe just knowing the current year would suffice - it all depends upon what you might note beneath a date and the uniqueness.

Once you split the draft up by date headings you would iterate over each section looking for a matching date to today’s at the start of the section. If you find one, append your new text to that. If you don’t, add a new section with your content.

I’d probably look at doing those by working with an array with a section per element. It is then relatively easy to produce, and relatively easy to update an element or append to the end of the first element which will be the heading and lines up to the first date entry.

Hope that helps

2 Likes

@agiletortoise that would be a nice option for settings. I would love to pretend text and keep the existing heading from the share dialog.

… would you put it on your Somday/Maybe list for Drafts?

2 Likes

Glorious typo: “pretend text”. :slight_smile:

1 Like