Action

Markdown Footnote (^)

Posted by agiletortoise, Last update over 1 year ago

Create a Markdown footnote at the cursor location, prompting for a footnote marker name, inserting the marker and footnote, leaving the cursor ready to edit the footnote content at the end of the current paragraph.

Inspired by Dr. Drang’s similar AppleScript for BBEdit

Steps

  • script

    // add footnote
    
    let f = () => {
    	// prompt for marker name
    	let p = new Prompt()
    	p.title = "Insert Footnote"
    	p.addTextField("marker", "Marker", "", {
    		"placeholder": "marker name",
    		"wantsFocus": true
    	})
    	p.addButton("Insert", "insert", true)
    	if (!p.show()) {
    		return false
    	}
    
    	// validate that we have a marker
    	let marker = p.fieldValues["marker"]
    	if (marker.length == 0) {
    		alert("Marker required")
    		context.fail()
    		return false
    	}
    
    	// get cursor info
    	const [st,len] = editor.getSelectedRange()
    	const [lnSt, lnLen] = editor.getSelectedLineRange()
    	// setup values
    	const m = `[^${marker}]`
    	let fn = `
    [^${marker}]: 
    `
    
    	// insert marker and footnote
    	const lnText = editor.getTextInRange(lnSt, lnLen)
    	if (lnText.length > 0 && lnText.slice(-1) != "\n") {
    		fn = "\n" + fn
    	}
    	
    	editor.setTextInRange(lnSt+lnLen, 0, fn)
    	editor.setTextInRange(st+len, 0, m)
    	editor.setSelectedRange(lnSt+lnLen+m.length+fn.length-1, 0)
    	editor.activate()
    	return true
    }
    
    if (!f()) {
    	context.cancel()
    }

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.