Action

Markdown Checklist

Posted by Adam Garrett-Harris, Last update almost 5 years ago

Toggle Markdown checklist for selected lines, maintaining indentation.

Steps

  • script

    // Toggle tasks marks on selected lines
    var listMark = "- [ ]";
    
    // grab state
    var [lnStart, lnLen] = editor.getSelectedLineRange();
    var lnText = editor.getTextInRange(lnStart, lnLen);
    var [selStart, selLen] = editor.getSelectedRange();
    
    // just add mark if empty line
    if (lnText.length == 0 || lnText == "\n") { 
      editor.setSelectedText(`${listMark} `);
      editor.setSelectedRange(selStart + listMark.length + 1, 0);
    }
    else {
      // create line array and tracking vars
      var lines = lnText.split('\n');
      var startOffset = 0;
      var lengthAdjust = 0;
      var flTrailing = false;
      if (lines[lines.length - 1] == "") { 
        lines.pop();
        flTrailing = true;
      }
      var newLines = [];
      const re = /^(\s*)?([-\ [*]*] )?(.*)/;
      const containsRe = /^(\s?)([-\ [*]*] )/;
    
      // determine if we are removing or adding marks
      var flRemoving = true;
      for (var line of lines) {
       if (line.length > 0 && !line.match(containsRe)) {
         flRemoving = false;
        }
      }
    
      if (!flRemoving) {
        // add marks
        var isFirst = true;
        for (var line of lines) {
          const match = re.exec(line);
    		if (match[2] || line.length == 0) {
          	  newLines.push(line);
          }
          else {
            var prefix = match[1];
            var suffix = match[3];
            if (!prefix) { prefix = ""; }
            if (!suffix) { suffix = ""; }
            newLines.push(`${prefix}${listMark} ${suffix}`);
            if (isFirst) {
              startOffset = listMark.length + 1;
            }
            else {
              lengthAdjust += (listMark.length + 1);
            }
          }
          isFirst = false;
        }
      } else {
        // remove marks
        var isFirst = true;
        for (var line of lines) {
          if (line.trim() == "") {
            newLines.push(line);
            continue;
          }
          const match = re.exec(line);
          var prefix = match[1];
          var suffix = match[3];
          var state = match[2];
          if (!prefix) { prefix = ""; }
          if (!suffix) { suffix = ""; }
          if (suffix.startsWith(" ")) { 
            suffix = suffix.substr(1);
            if (isFirst) { startOffset -= 1; }
            else { lengthAdjust -= 1; }
          }
          newLines.push(`${prefix}${suffix}`);
          if (isFirst) {
            startOffset -= state.length;
          }      
          else {
          	  lengthAdjust -= state.length;
          }
          isFirst = false;
        }
      }
    
      // update text and selection
      if (flTrailing) {
        newLines.push("");
      }
      var newText = newLines.join("\n");
      editor.setTextInRange(lnStart, lnLen, newText);
      editor.setSelectedRange(selStart + startOffset, selLen + lengthAdjust);
    }
    

Options

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