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