MonitorControl/docs/scripts/marked-footnotes.min.js
2021-12-11 14:52:47 +01:00

42 lines
No EOL
1.2 KiB
JavaScript

const footnoteMatch = /^\[\^([^\]]+)\]:([\s\S]*)$/;
const referenceMatch = /\[\^([^\]]+)\](?!\()/g;
const referencePrefix = "fnref";
const footnotePrefix = "fn";
const footnoteTemplate = (ref, text) => {
return `<p id="${footnotePrefix}:${ref}">${ref}. ${text}</p>`;
};
const footnoteContainerTemplate = (text) => {
return `<h3>Footnotes</h3>${text}`
}
const referenceTemplate = ref => {
return `<sup id="${referencePrefix}:${ref}"><a href="#${footnotePrefix}:${ref}">[${ref}]</a></sup>`;
};
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 });