How to grab current line, but also check for matching text below?

I’m starting to manage project reference materials in Drafts and I created an action that sends the current line to Things as a task. Works great. However, I’d like to take it one step further.

My project reference material looks like this:

# Project

- item 1
- item 2
    > additional notes and links and things
    > perhaps even multiple blocks of notes
- item three
    > sometimes just one block of notes

Could I edit my existing script (pasted below), so that before sending the current line to Things as a task, it checked to see if there were any note blocks below (which will always be preceded by a tab and a “>”, then sent those along too as “notes”?

let [loc, len] = editor.getSelectedLineRange(),
  str = editor.getTextInRange(loc, len),
  icon = '🔘',
  listTitle = draft.processTemplate("[[title]]"),
  regex = /# /,
  regex2 = /- /;

let todoList = listTitle.replace(regex,"");
let todoTitle = str.trim(); 
todoTitle = todoTitle.replace(regex2,"");

var cb = CallbackURL.create();
const baseURL = "things:///add?"
  cb.baseURL = baseURL;
  cb.waitForResponse = true;
  var success =;
  if (success) {
    console.log("Task created in Things" + cb.url);
    var response = cb.callbackResponse;
  else {;

newStr = icon + ' ' + todoTitle + " [things](things:///show?id=" + response["x-things-id"] + ")\n";
editor.setTextInRange(loc, len, newStr);
// move the cursor to the end of the edited line
editor.setSelectedRange(loc + newStr.trim().length, 0);
// activate is not required if the action is launched via a keyboard button

I’m just learning JS, and I’m jotting this down in a hurry, so this might not be the most elegant solution but i would add something like this:

// get contents of entire draft
const fullContent = draft.content;
// find the task you already have, in an array of task+notes
const task = fullContent.split(‘-‘)
                         .find(task => task.includes(todoTitle));
// separate notes from task title
let notesArray = task.split(‘>’);
notesArray.shift(); // remove task title

// end result
const notes = notesArray.join(‘’);

This might be a little sloppy, and could probably be refactored a bit. You might need to clean up the formatting of the notes. You could also change this around to capture all tasks from the draft if you wanted to do that.

Also, I didn’t add anything to check for note blocks, so you probably want to add some logic for that.

Anyways, thought I’d just throw out some ideas, so you would have something to work woth.

This is very helpful! Thank you. I don’t have enough scripting knowledge to come up with a solution like this, but I am familiar with all the techniques in your example and I’m pretty sure that will get me where I’m looking to go. Thanks again!