Action Directory / Footnote

Install

Add a MultiMarkdown footnote to the current document. The action asks for a footnote name and, if not already in use, asks to enter the footnote text. Footnotes are added to the bottom of the document and caret position is automatically restored at the end.

Shared by viticci

Script

var allText = editor.getText();
var range = editor.getSelectedRange();

ui.input('Footnote Name', '', 'Enter Footnote Name', enterFootnote);

function enterFootnote(footnoteLabel) {
    if (!footnoteLabel) { //user pressed Cancel button
        return;
    }
    if (allText.indexOf('^' + footnoteLabel) !=-1) {
    	ui.hudError('Choose A Different Name');
    	return;
    }
    ui.input('Footnote Text', '', 'Enter Footnote Text', enterFootnoteText);
    function enterFootnoteText(footnoteText) {
    if (!footnoteText) { //user pressed Cancel button
        return;
    }
    var final = '\n\n[^' + footnoteLabel + ']: ' + footnoteText;
    editor.replaceTextInRange(1000000, 1000000, final)
    editor.replaceTextInRange(range[0], range[1], ('[^'+ footnoteLabel + ']'))
    }
}