✪ ≣
Better Markdown List

Script key: Better Markdown List

Posted by @mjdescy

Install

Details

Label ✪ ≣
Voice Over Better Markdown List
Script
// Markdown list to current line, or output the list prefix
// if the current line is blank and nomtext is selected

// set your preferred Markdown list prefix character (- or + or *)
var mdListPrefixChar = "-";
var mdListPrefix = mdListPrefixChar + " ";

var lnRange = getSelectedLineRange();
var ln = getTextInRange(lnRange[0], lnRange[1]);
var selRange = getSelectedRange();

if (ln == "") {
  setSelectedText(mdListPrefix);
  setSelectedRange(selRange[0] + mdListPrefix.length, 0);
} else {
  var lines = ln.split('\n');
  var charsAdded = 0;

  for (var ix=0; ix < lines.length; ix++) {
    if (lines[ix].length > 0) {
      if (lines[ix][0] != mdListPrefixChar) {
        var prefix = mdListPrefixChar;
        if (lines[ix][0] != " ") {
          prefix = mdListPrefix;
        }
        lines[ix] = prefix + lines[ix];
        charsAdded = charsAdded + prefix.length;
      }
    }
  }
  setTextInRange(lnRange[0], lnRange[1],lines.join("\n"));
  if (lines.length > 2) {
    setSelectedRange(lnRange[0]+lnRange[1]+charsAdded, 0);
  }
  else {
    setSelectedRange(selRange[0]+charsAdded, selRange[1]);
  }
}


Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus