lepa_octomode/static/footnotes.js
2025-12-27 14:51:12 +01:00

56 lines
1.8 KiB
JavaScript

/*
Adds a custom css property that switches footnote style
between endnodes (default for Pandoc) and footnotes
modified from: https://pagedjs.org/plugins/endnotes-to-footnotes/
*/
const endNoteCalloutsQuery = ".footnote-ref";
// the hook
class endToFootNotes extends Paged.Handler {
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
this.notestyle = 'endnotes';
}
onDeclaration(declaration, dItem, dList, rule) {
if (declaration.property == "--paged-note-style") {
if (declaration.value.value.includes("footnote")) {
this.notestyle = 'footnotes';
}
}
}
beforeParsed(content) {
if (this.notestyle !== 'footnotes') return;
// Clean up previously injected clones (in case of re-render).
content.querySelectorAll('.pagedjs-footnote-clone').forEach((n) => n.remove());
// Optional: hide the original footnote list; we will clone notes inline.
const originalList = content.querySelector('.footnotes');
if (originalList) {
originalList.classList.add('pagedjs-footnotes-hidden');
}
const callouts = content.querySelectorAll(endNoteCalloutsQuery);
callouts.forEach((callout) => {
if (!callout.hash) return;
const note = content.querySelector(callout.hash);
if (!note) {
console.warn(`No footnote found for ${callout.hash}`);
return;
}
// Clone the note content and float it as a footnote.
const clone = document.createElement('span');
clone.classList.add('pagedjs-footnote-clone');
clone.innerHTML = note.innerHTML;
callout.insertAdjacentElement('afterend', clone);
});
}
}
Paged.registerHandlers(endToFootNotes);