-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: add thanks * refactor: rewrite footnotes * refactor: rewrite search * chore: rename to el
- Loading branch information
WingLim
authored
Jul 5, 2021
1 parent
0f7d522
commit 5618eca
Showing
5 changed files
with
216 additions
and
203 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
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,56 @@ | ||
const removeEl = (el: Element) => { | ||
if (!el) return; | ||
el.remove ? el.remove() : el.parentNode.removeChild(el); | ||
}; | ||
|
||
const insertAfter = (target: HTMLElement, sib: Element) => { | ||
target.after ? target.after(sib) : ( | ||
target.parentNode.insertBefore(sib, target.nextSibling) | ||
); | ||
}; | ||
|
||
const insideOut = (el: Element) => { | ||
var p = el.parentNode as HTMLElement, | ||
x = el.innerHTML, | ||
c = document.createElement('div'); // a tmp container | ||
insertAfter(p, c); | ||
c.appendChild(el); | ||
el.innerHTML = ''; | ||
el.appendChild(p); | ||
p.innerHTML = x; // let the original parent have the content of its child | ||
insertAfter(c, c.firstElementChild); | ||
removeEl(c); | ||
}; | ||
|
||
export let renderFootnotes = function () { | ||
document.querySelectorAll('.footnotes > ol > li[id^="fn"], #refs > div[id^="ref-"]').forEach(function (fn) { | ||
let a = document.querySelectorAll('a[href="#' + fn.id + '"]'); | ||
if (a.length === 0) return; | ||
a.forEach(function (el) { el.removeAttribute('href') }); | ||
let newA = a[0] as HTMLElement; | ||
let side = document.createElement('div'); | ||
side.className = 'side side-right'; | ||
if (/^fn/.test(fn.id)) { | ||
side.innerHTML = fn.innerHTML; | ||
var number = newA.innerHTML; // footnote number | ||
side.firstElementChild.innerHTML = '<span class="bg-number">' + number + | ||
'</span> ' + side.firstElementChild.innerHTML; | ||
removeEl(side.querySelector('a[href^="#fnref"]')); // remove backreference | ||
let newAParent = newA.parentNode as HTMLElement | ||
newAParent.tagName === 'SUP' && insideOut(newA); | ||
} else { | ||
side.innerHTML = fn.outerHTML; | ||
newA = newA.parentNode as HTMLElement; | ||
} | ||
insertAfter(newA, side); | ||
newA.classList.add('note-ref'); | ||
removeEl(fn); | ||
}) | ||
document.querySelectorAll('.footnotes, #refs').forEach(function (fn) { | ||
var items = fn.children; | ||
if (fn.id === 'refs') return items.length === 0 && removeEl(fn); | ||
// there must be a <hr> and an <ol> left | ||
if (items.length !== 2 || items[0].tagName !== 'HR' || items[1].tagName !== 'OL') return; | ||
items[1].childElementCount === 0 && removeEl(fn); | ||
}); | ||
}; |
Oops, something went wrong.