Action

Hash Up

Posted by drdrang, Last update about 4 years ago - Unlisted

Steps

  • script

    // Extend the selection to include full lines
    var r = editor.getSelectedLineRange();
    editor.setSelectedRange(r[0], r[1]);
    var sel = editor.getSelectedText();
    
    // Don't include any trailing newline
    if (sel.substring(sel.length-1) =="\n") {
    	editor.setSelectedRange(r[0], r[1]-1);
    	sel = editor.getSelectedText();
    }
    
    // Make a list of lines
    var lines = sel.split("\n");
    var n = lines.length;
    
    // Adjust the leading hashes for each line in turn
    for (var i=0; i<n; i++) {
    	if (lines[i].substring(0, 4) == "### ") {
    		lines[i] = lines[i].substring(4);
    	} else if (lines[i].substring(0, 1) == "#") {
    		lines[i] = "#" + lines[i];
    	} else {
    		lines[i] = "# " + lines[i];
    	}
    }
    // Rejoin the lines and replace the text in the editor
    var s = lines.join("\n");
    editor.setSelectedText(s);

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.