Action

Blockquote

Posted by garbonsai, Last update 12 months ago

Blockquote or increase quote level of (prepend lines with ‘> ‘) selected text, or entire draft if no selection. Fixes a bug in another action that quotes empty lines after the selection, which annoyed me to no end.

Steps

  • script

    // declare a few variables
    let selText,
    	newSelLength,
    	lastLine = '',
    	quoteStr = '> ';
    
    // get the selected lines of text or the full text of the draft if none
    let selRange = editor.getSelectedRange();
    let selLength = selRange[1];
    let selLineRange = editor.getSelectedLineRange();
    if (selLength !== 0) {
    	selText = editor.getTextInRange(selLineRange[0], selLineRange[1]);
    	newSelLength = selLength;
    } else {
    	selText = editor.getText();
    	newSelLength = selText.length;
    }
    
    // split the text into an array
    let lines = selText.split('\n');
    
    // prepend the quote string to each line and rejoin the lines of text
    for (let i = 0; i < lines.length - 1; i++) {
    	lines[i] = quoteStr + lines[i];
    	newSelLength += quoteStr.length;
    }
    if (lines[lines.length - 1] !== '') {
    	lines[lines.length - 1] = quoteStr + lines[lines.length - 1];
    	newSelLength += quoteStr.length;
    }
    selText = lines.join('\n');
    
    // update the draft and selection
    if (selLength !== 0) {
    	editor.setTextInRange(selLineRange[0], selLineRange[1], selText);
    	editor.setSelectedRange(selLineRange[0], newSelLength);
    } else {
    	editor.setText(selText);
    	editor.setSelectedRange(0, newSelLength);
    }

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.