✪ [^]
Markdown footnote

Script key: Markdown footnote

Posted by agiletortoise

Install

Details

Label ✪ [^]
Voice Over Markdown footnote
Script
// Insert Markdown footnote marker ([^1]) at cursor,
// and footnote tag ([^1]:) at after the line.
//
// If text is selected, use that as footnote name.
// If no text is selected, insert numbered footnote.
//
// Script will scan draft for highest existing
// numbered footnote and increment that value.
//
// After insertion, the cursor will be left
// ready to type footnote text.

// get info to use to use
var name = getSelectedText();
var selRange = getSelectedRange();
var lnRange = getSelectedLineRange();
var ln = getTextInRange(lnRange[0],lnRange[1]);
var endsInFeed = (ln[ln.length-1] == '\n');

// if no selection to name, use numbering
// this will scan for existing numbered
// footnotes in draft and use next higher number
var hasText = (name.length > 0);
if (!hasText) {
  var re = /\[\^(\d+)\]/g;
  var text = getText();
  var result;
  var ct = 0;
  while((result = re.exec(text)) !== null) {
    val = parseInt(result[1]);
    if (val > ct) { ct = val; }
  }
  ct++;
  name = ct.toString();
}

// setup values to insert
var footnote = '\n[^' + name + ']:\n';
if (!endsInFeed) { footnote = '\n'+footnote; }
var marker = '[^' + name + ']';

// add footnote placeholder
setTextInRange(lnRange[0]+lnRange[1],0,footnote);

// add footnote marker
setTextInRange(selRange[0],selRange[1],marker);

// update selection
setSelectedRange(lnRange[0]+lnRange[1]+marker.length-(hasText ? name.length : 0)+footnote.length-1,0);

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus