Action

MD Footnote

Posted by @MyGeekDaddy, Last update almost 2 years ago

UPDATES

almost 2 years ago

The initial footnote number is randomized and then sequence with any additional footnotes. This was used to fix a problem with using footnotes in Markdown files on Pelican static websites.

show all updates...

almost 2 years ago

The initial footnote number is randomized and then sequence with any additional footnotes. This was used to fix a problem with using footnotes in Markdown files on Pelican static websites.

about 2 years ago

Fixed error where initial footnote did not include ‘:’

Another variation of two different footnote actions @pdavisonreiber‘s Markdown Footnote Action and David Blue’s variation

This version will insert the footnote marker at the point of markdown text and append the footnote marker at the end of the draft note ready for the user to enter the footnote text.

Steps

  • script

    /* 
    If no text is selected:
    - add footnote [^xxxx] at current cursor position
    - add reference [^xxxx]: at end of document
    - move cursor to after reference [^1]: for text entry
    
    If text is selected:
    - replace selection with [^xxxx]
    - add reference [^1]: at end of document, followed by selected text
    - move cursor to after [^xxxx] in the body of the text
    
    Script automatically increments reference number for each new footnote.
    
    Rev 2022-10-07: Fixed initial footnote entry missing ':'
    
    Rev 2023-03-01: Updated footnote numbering to start with a randomized number 
    to increase probability of a unique number when using MD file on 
    elican & bigfoot.js footnote script. 
    
    */
    
    var fnBase = Math.floor(Math.random() * 10000)
    
    // helper to check current highest link number and return next available number
    function nextLinkNumber() {
    	var lines = editor.getText().split("\n")
    	var lastLine = lines.pop()
    	// Ignore whitespace after final line of document
    	nonWhitespaceRegex = /\S/ 
    	while (!nonWhitespaceRegex.test(lastLine)){
    		lastLine = lines.pop()
    	}	
    	var refRegex = /\[\^*(\d+)\]/
    	var existingLink = refRegex.test(lastLine)
    	if (existingLink == false){
    		return fnBase
    	} else {
    		var currentLinkNumber = parseInt(refRegex.exec(lastLine)[1])
    		return currentLinkNumber + 1
    	}
    }
    
    var selection = editor.getSelectedText()
    var selectedRange = editor.getSelectedRange()
    var fnNumber = nextLinkNumber()
    
    // Add additional new line character if first reference, otherwise add to next line
    if (fnNumber == 1) {
    	var fn = "\n\n[^" + fnNumber + "]: "
    } else {
    	var fn = "\n\n[^" + fnNumber + "]: "
    }
    
    // If no text is selected
    if (selection.length == 0) {
    	// Add reference link at current cursor position
    	editor.setSelectedText("[^" + fnNumber + "]")
    	// Add reference to end of document
    	editor.setText(editor.getText() + fn)
    	// Move cursor to end of reference
    	editor.setSelectedRange(editor.getText().length, 0)
    } else {
    	// Replace currently selected text with footnote
    	editor.setSelectedText("[^" + fnNumber + "]:")
    	// Add reference to end of document with selected text
    	editor.setText(editor.getText() + fn + selection)
    
    	// If selected text was moved, move cursor to after footnote in body
    editor.setSelectedRange(selectedRange[0] + + 3 + fnNumber.toString().length, 0)
    }

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.