Action

Markdown Todo

Posted by mcgrealife, Last update about 6 years ago

Powerful drafts4 script rewritten for drafts5. Originally created by “elstudio”.
https://drafts4-actions.agiletortoise.com/k/1Mz

Appends “[ ]” to current line or multiple selected line.
If “[ ]” already exists, change to “[x]”.
Alternatively, tap inside the brackets to add “x”.

Useful for ad-hoc todo lists.

Originally titled “Markdown todo”

Steps

  • script

    // Prefix selected lines with Markdown to-do checkboxes
    // A plain Markdown checkbox, unckecked:
    var prefix = "- [ ] ";
    
    var lineRange = editor.getSelectedLineRange();
    var ln = editor.getTextInRange(lineRange[0], lineRange[1]);
    var selectedRange = editor.getSelectedRange();
    
    function chkLine(line) {
      // These regexp match various checkbox states
      chkd = /^(\s*[-*+]\s)\[x]/i      // checked: "- [x]"
      unchkd = /^(\s*[-*+]\s)\[\s?]/   // unchecked: "- [ ]"
      li = /^(\s*[-*+]\s)/             // list item: "- "
    
      added = 0;
      if (chkd.test(line)) {
        line = line.replace(chkd, "$1[ ]");
      }
      else if (unchkd.test(line)) {
        line = line.replace(unchkd, "$1[x]");
      }
      else if (li.test(line)) {
        line = line.replace(li, "$1[ ] ");
        added = 4;
      } else {
        line = prefix + line;
        added = 6;
      }
      return {
        text: line,
        added: added
      }
    }
    
    
    if (ln && ln != "") {
      var lines = ln.split('\n');
      var charsAdded = 0;
    
      for (var ix=0; ix < lines.length; ix++) {
        if (lines[ix].length > 0) {
          line = chkLine(lines[ix]);
          lines[ix] = line.text;
          charsAdded += line.added;
        }
      }
    
      // Replace text in draft
      editor.setTextInRange(lineRange[0], lineRange[1],lines.join("\n"));
    
    
      // Reset selection
      if (lines.length > 2) {
        editor.setSelectedRange(lineRange[0]+lineRange[1]+charsAdded, 0);
      }
      else {
        editor.setSelectedRange(selectedRange[0]+charsAdded, selectedRange[1]);
      }
    
    }
    else {
      // If the current line is blank, just add a checkbox
      editor.setSelectedText(prefix);
      charsAdded = prefix.length;
      editor.setSelectedRange(selectedRange[0]+charsAdded, 0);
    }
    

Options

  • After Success Default
    Notification None
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.