Action

Drop Selected Tasks

Posted by jochi, Last update 4 months ago

“Drop” selected tasks with strikethrough markdown

Steps

  • script

    // Get the selected range and full text
    const selectionStart = editor.getSelectedRange()[0];
    const selectionLength = editor.getSelectedRange()[1];
    const selectionEnd = selectionStart + selectionLength;
    const fullText = editor.getText();
    
    // Calculate the start position of the selection, ensuring it starts at the beginning of a line
    let startOfSelection = fullText.lastIndexOf("\n", selectionStart - 1) + 1;
    if (startOfSelection === 0) {
        startOfSelection = 0; // If no newline is found, start from the beginning
    }
    
    // Calculate the end position of the selection, ensuring it ends at the end of a line
    let endOfSelection = fullText.indexOf("\n", selectionEnd);
    if (endOfSelection === -1) {
        endOfSelection = fullText.length; // If no newline is found, end at the last character
    }
    
    // Get the text from the start to the end of the lines
    const textToWrap = fullText.substring(startOfSelection, endOfSelection);
    
    // Split the text into lines
    const lines = textToWrap.split("\n");
    
    // Wrap each line's content in strikethrough markdown
    const updatedLines = lines.map(line => {
        // Match lines that start with "- [ ] "
        if (line.startsWith("- [ ] ")) {
            return line.replace(/^- \[ \] (.*)/, "- [ ] ~~$1~~");
        }
        // Return the line unchanged if it does not match
        return line;
    });
    
    // Join the updated lines back into a single string
    const updatedSelection = updatedLines.join("\n");
    
    // Replace the selected text with the updated text
    editor.setTextInRange(startOfSelection, endOfSelection - startOfSelection, updatedSelection);
    
    // Calculate the new end position of the selection
    const newEndOfSelection = startOfSelection + updatedSelection.length;
    
    // Set the new selection range
    editor.setSelectedRange(startOfSelection, updatedSelection.length);

Options

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