Action

Move Checked to End

Posted by zisoft, Last update about 1 month ago

This action moves the checked items of a list to the end of the list, maintaining the structure of the draft. So if you have multiple lists in your document, they are treated each of their own.

Example:
- [ ] item 1
- [x] item 2
- [x] item 3
- [ ] item 4

After running the action the list appears as:
- [ ] item 1
- [ ] item 4
- [x] item 2
- [x] item 3

Steps

  • script

    // Move checked items to the end of a list
    // 
    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    let content = editor.getText();
    let lines = content.split("\n");
    lines.push(""); // make sure the content ends with an empty line
    
    let unchecked = [];
    let checked = [];
    let new_content = [];
    
    for (let line of lines) {
        if (line.startsWith("- [")) {
            // found a list item
            if (line.startsWith("- [x]")) {
                checked.push(line);
            }
            else {
                unchecked.push(line);
            }
        }
        else {
            // not within in a list
            if (unchecked.length > 0 || checked.length > 0) {
                // output lists
                new_content = new_content.concat(unchecked, checked);
                
                checked = [];
                unchecked = [];
            }
            
            new_content.push(line);
        }
    }
    
    new_content.pop();    // remove the added empty line
    
    editor.setText(new_content.join("\n"));
    editor.activate();
    

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.