Append question from a Newbie

Hi
Sorry to borrow you with basic question here !
What I would do : Is inserting the content of a Draft into an existing document.
My document is an .md file which contain some tags #todo and I would append the Draft into the correct section (below the tag #todo but before the next one)
Any help will be appreciated
Thanks
Patrick

1 Like

Some questions for you to consider that are likely to affect the options and approach:

  • Where is the Markdown document located in the file system?
  • Is it always the same Markdown file you are updating or does it vary in name/location?
  • What operating system/device do you want to do this with?
  • Do you want new lines before or after the inserted draft?
  • is the draft to insert the currently viewed draft, a draft that is always a particular draft, or do you want to choose a draft each time?
  • Just to confirm, it is always an insertion after the todo tag, and never a replacement of the content between the todo tag and the next hash tag.

Thanks a lot the Mardown file is part of a Obsidian Vault located in Dropbox
I can append or pre-append the Draft into the correct doc it is the mechanic to put into the doc below the #todo tag without an empty line I’m failing with
I use Osx for that Big Sur and Draft 27.0.11
The aim is to enter my todo’s in my obsidian doc in the right place with the draft content ( the full draft) it can contain one or multiple lines and it is only an insert
Thanks for your help here

If it is just the algorithmic bit you are stuck on, try this:

  1. Read the Markdown file content.
  2. Split thre Markdown file content into two parts at the #todo tag - e.g. Into an array.
  3. Create new content that consists of concatenation of the first part of the Markdown content, followed by the #todo tag, followed by the draft content, followed by the last part of the Markdown content.
  4. Replace the Markdown file content with the new content.

Hi @sylumer good point ! And from an algorithmic point of view is fully understood
It should be in 3 part as the document have other section below the #todo tag
But the main question is How to do it ?
I’m not a dev and bit lost with

Thanks

Can you start with sharing what you have? I still don’t know how you are triggering or doing what you have said you can/do do.

Otherwise there are other unknowns from the above that are not covered that would affect how you might choose to do it.

Hi @sylumer
Thanks a lot
I have daily note file name YYYY-MM-DD.md
With some sections :

# 🗞️ **Agenda**: 
 
## **Meeting: **
 #meeting
 ---
 ##  ✔️ **Todo: ** 
 #todo 
 - [ ] 
 - [ ] 
	
 --- 
 ## Notes:
#notes

I use a dropbox action to append my information into this file (with some variables) to reach the right file
However the append add the content of the draft at the end of the file and not in the correct section.
The aim is to have the content of the Draft Inserted just below the (#todo), moving the existing lines below the the inserted Draft without blank line
It would be a new draft at each time but I can imagine to have it based on a template if needed

The mechanic can be also find the Tag and insert the Draft content below

Hope is clearer
Thanks

To do what you want, you need a script that:

  • Reads the content of the file into a string variable
  • Finds the location of the #todo marker in the string
  • Inserts the additional text at that location
  • Save the file back overwriting the existing version

You said you are using Dropbox, so that might look something like:

// not sure where your file is, update this...
let path = "/path/to/file.md";
// use full text of current draft? Not sure...
let newContent = draft.content;
// the string you looking for (included line feed)
let marker = "#todo\n";

// read file from Dropbox
let db = Dropbox.create();
let oldContent = db.read(path);

// replace marker with marker + newContent
let content = oldContent.replace(marker, `${marker}
${newContent}`);

// save back to Dropbox
db.write(path, content, "overwrite", false)
1 Like

Hi Thanks a lot for you support

I got :
Script Error: TypeError: undefined is not an object (evaluating ‘oldContent.replace’)
Line number: 16, Column 25

I have created a new Action

  • Select script
  • Paste the code provided
  • set the path to a test file ( it will be in variable to fit with my daily note )
    And run
// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

// not sure where your file is, update this...
let path = "~/Dropbox/@PLAINTEXT/CALENDAR/2021_CALENDAR_NOTES/test.md";
// use full text of current draft? Not sure...
let newContent = draft.content;
// the string you looking for (included line feed)
let marker = "#todo\n";

// read file from Dropbox
let db = Dropbox.create();
let oldContent = db.read(path);

// replace marker with marker + newContent
let content = oldContent.replace(marker, `${marker}
${newContent}`);

// save back to Dropbox
db.write(path, content, "overwrite", false)

Thanks

I didn’t bother with pleasant error handling for the example. That error means the file was not read successfully…which makes sense, as your path is not right. If using the Dropbox object, paths are relative to the root of your Dropbox, so probably:

let path = "/@PLAINTEXT/CALENDAR/2021_CALENDAR_NOTES/test.md";

Just Perfect !!!

Thanks a lot for your support !!! So Great !!

One more thing :slight_smile:
How can I make the file name as a variable?
It should be like YYYY-MM_DD.md this will help me to fill my daily journal with the same action

Thanks
Patrick

Assuming you have a typo in your format compared to the earlier reference, this amendment will include the current date in yyyy-MM-dd format for the base file name.

let path = "/@PLAINTEXT/CALENDAR/2021_CALENDAR_NOTES/" + draft.processTemplate("[[Date|%Y-%m-%d]]") + ".md";

Thanks a lot @sylumer It works perfectly !!!
Very usefull action !!

Thanks a ll for your support !!!