Action Directory / New File with L2 section.

Install

Split current file into two files based on L2 markdown section.
Select L2 section from list of L2 headers, and then current L2 section will be removed from current file and new File will be create with the Section.

Shared by Chan-gu Lee, @stratosphere631

Script

var txt=editor.getText();
var ptn=/^## +.*$/gm
var headers=txt.match(ptn);
ui.list("Select L2 header", headers, false,
  function(selectedValues, selectedIndices)
  {
    if (!selectedValues) return;
    if (!selectedIndices) return;
    if (selectedIndices[0]!=headers.length-1)
    {
      var begin=txt.search(selectedValues);
      var next=headers[selectedIndices[0]+1];
	  var end=txt.search(next);
      splitFileWithSelectedRange(begin, end);
    }
    else
    {
      var begin=txt.search(selectedValues);
      var end=txt.length;
      splitFileWithSelectedRange(begin, end);
    }
    
  }
);
function splitFileWithSelectedRange(begin, end) {
  var txt=editor.getTextInRange(begin, end);
  editor.replaceTextInRange(begin, end, "");
  editor.newFile(txt);
}