/* jshint node:true, browser:true */
// Adds footnote syntax as per Markdown Extra:
//
// https://michelf.ca/projects/php-markdown/extra/#footnotes
//
// That's some text with a footnote.[^1]
//
// [^1]: And that's the footnote.
//
// That's the second paragraph.
//
// Also supports [^n] if you don't want to worry about preserving
// the footnote order yourself.
function replaceInlineFootnotes(text) {
// Inline footnotes e.g. "foo[^1]"
var inlineRegex = /(?!^)\[\^(\d|n)\]/gim,
i = 0;
return text.replace(inlineRegex, function (match, n) {
// We allow both automatic and manual footnote numbering
if (n === 'n') {
n = i + 1;
}
var s = '' +
'' + n + '' +
'';
i += 1;
return s;
});
}
function replaceEndFootnotes(text, converter) {
// Expanded footnotes at the end e.g. "[^1]: cool stuff"
var endRegex = /\[\^(\d|n)\]: ([\s\S]*?)$(?! )/gim,
m = text.match(endRegex),
total = m ? m.length : 0,
i = 0;
return text.replace(endRegex, function (match, n, content) {
if (n === 'n') {
n = i + 1;
}
content = content.replace(/\n /g, '
');
content = converter.makeHtml(content);
content = content.replace(/<\/p>$/, '');
var s = '