const footnoteMatch = /^\[\^([^\]]+)\]:([\s\S]*)$/; const referenceMatch = /\[\^([^\]]+)\](?!\()/g; const referencePrefix = "fnref"; const footnotePrefix = "fn"; const footnoteTemplate = (ref, text) => { return `

${ref}. ${text}

`; }; const footnoteContainerTemplate = (text) => { return `

Footnotes

${text}` } const referenceTemplate = ref => { return `[${ref}]`; }; const interpolateReferences = (text) => { return text.replace(referenceMatch, (_, ref) => { return referenceTemplate(ref); }); } const interpolateFootnotes = (text) => { const found = text.match(footnoteMatch) if (found) { const replacedText = text.replace(footnoteMatch, (_, value, text) => { return footnoteTemplate(value, text); }); return footnoteContainerTemplate(replacedText) } return text } const renderer = { paragraph(text) { return marked.Renderer.prototype.paragraph.apply(null, [ interpolateReferences(interpolateFootnotes(text)) ]); }, text(text) { return marked.Renderer.prototype.text.apply(null, [ interpolateReferences(interpolateFootnotes(text)) ]); } }; marked.use({ renderer });