mirror of
https://github.com/MonitorControl/MonitorControl.git
synced 2026-05-16 06:05:52 -06:00
42 lines
No EOL
1.2 KiB
JavaScript
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 }); |