Action

Archive Tasks

Posted by productnook, Last update 11 months ago

Checks for Completed tasks within a Draft and adds them to a separate “Archived” header within the same Draft.

Steps

  • script

    var text = editor.getText();
    var lines = text.split("\n");
    var begin = '';
    var archive = ''; // Will include the Archive header if tasks are present
    var end = '';
    var completedTaskIndicator = "- [x]";
    var archiveHeader = "## Archive";
    var foundArchiveHeader = false;
    
    for (var line of lines) {
      if (line.trim() === archiveHeader) {
        foundArchiveHeader = true;
        continue;
      }
    
      if (line.includes(completedTaskIndicator)) {
        if (archive === '') {
          archive += archiveHeader + "\n\n"; // Add header with extra newline
        }
        archive += line + "\n";
      } else {
        if (!foundArchiveHeader) {
          begin += line + "\n";
        } else {
          end += line + "\n";
        }
      }
    }
    
    begin = begin.trim();
    archive = archive.trim();
    end = end.trim();
    
    // Construct the final text
    var finalText = begin;
    if (finalText && end) {
      finalText += "\n\n" + end;
    } else {
      finalText += end;
    }
    if (archive) {
      finalText += "\n\n" + archive;
    }
    
    editor.setText(finalText);

Options

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