-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hacky JS for footnote backreferences
This code should work fine until `pulldown-cmark` and Zola are updated to support footnote backreferences natively. pulldown-cmark/pulldown-cmark#142
- Loading branch information
1 parent
b906189
commit 9ba8baf
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// The DOMContentLoaded event fires when the initial HTML | ||
// document has been completely loaded and parsed, without | ||
// waiting for stylesheets, images, and subframes to finish loading. | ||
document.addEventListener('DOMContentLoaded', (_event) => { | ||
const references = document.getElementsByClassName('footnote-reference') | ||
// For each footnote reference, set an id so we can refer to it from the definition. | ||
// If the definition had an id of 'some_id', then the reference has id `some_id_ref`. | ||
for (const reference of references) { | ||
const link = reference.firstChild | ||
const id = link.getAttribute('href').slice(1) // skip the '#' | ||
link.setAttribute('id', `${id}_ref`) | ||
} | ||
|
||
const footnotes = document.getElementsByClassName('footnote-definition') | ||
// For each footnote-definition, add an anchor element with an href to its corresponding reference. | ||
// The text used for the added anchor is 'Leftwards Arrow with Hook' (U+21A9). | ||
for (const footnote of footnotes) { | ||
const id = footnote.getAttribute('id') | ||
const backReference = document.createElement('a') | ||
backReference.setAttribute('href', `#${id}_ref`) | ||
backReference.textContent = '↩' | ||
footnote.append(backReference) | ||
} | ||
}); |