Action

Markdown Quotation (>)

Posted by agiletortoise, Last update almost 4 years ago

Apply Markdown Quote (“> “) at beginning of selected lines.

Steps

  • script

    // make selected lines into Markdown quotation
    
    // grab state
    const [lnStart, lnLen] = editor.getSelectedLineRange();
    const lnText = editor.getTextInRange(lnStart, lnLen);
    const [selStart, selLen] = editor.getSelectedRange();
    
    // setup vars
    const lines = lnText.split("\n");
    const quoteString = "> ";
    let isFirst = true;
    let newLines = [];
    let startAdjust = 0;
    let lengthAdjust = 0;
    let flTrailing = false;
    if (lines[lines.length - 1] == "") { 
    	lines.pop();
    	flTrailing = true;
    }
    
    // just add mark if empty line
    if (lnText.length == 0 || lnText == "\n") { 
      editor.setSelectedText(quoteString);
      editor.setSelectedRange(selStart + quoteString.length + 1, 0);
    }
    else {
    	// loop over lines
    	for (let line of lines) {
    		newLines.push(quoteString + line);
    		if (isFirst) { 
    			startAdjust += quoteString.length;
    		}
    		else {
    			lengthAdjust += quoteString.length;
    		}
    		isFirst = false
    	}
    
    	// update text 
    	if (flTrailing) {
    		newLines.push("");
    	}
    	editor.setTextInRange(lnStart, lnLen, newLines.join("\n"));
    
    	// update selection
    	editor.setSelectedRange(selStart + startAdjust, 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.