Jump to Section
Shared by MK
Scrolls to the chosen section (heading) in the document.
Script
var txt = editor.getText(),
txtEnd = txt.length,
setextHeadings, atxHeadings,
headings = [],
currentHeading, currentHeadingPosition;
setextHeadings = txt.match(/\n?.+\n(\=|\-)+\n/gi);
atxHeadings = txt.match(/\n?\#{1,6}\s+.+\n/gi);
if (setextHeadings) {
for (var i = 0; i < setextHeadings.length; i++) {
currentHeading = setextHeadings[i].trim();
currentHeadingPosition = txt.indexOf(currentHeading);
currentHeading = currentHeading.match(/[^\n\=\-]+/gi)[0].trim();
headings.push(currentHeading + "|" + currentHeadingPosition);
}
}
if (atxHeadings) {
for (var i = 0; i < atxHeadings.length; i++) {
currentHeading = atxHeadings[i].trim();
currentHeadingPosition = txt.indexOf(currentHeading);
currentHeading = currentHeading.match(/[^\n\#]+/gi)[0].trim();
headings.push(currentHeading + "|" + currentHeadingPosition);
}
}
ui.list("Choose Section", headings, false, function(headingPosition){
if (headingPosition) {
editor.setSelectedRange(txtEnd);
editor.setSelectedRange(headingPosition);
}
});