Skip to content

Commit

Permalink
also consider the case of appendix titles in (div) wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed May 1, 2024
1 parent 25bb0fa commit 08a6dfc
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion js/appendix.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
// find <h[1-6] class="appendix"> and create a new <div> next to the parent
// find <h[1-6] class="appendix"> and either add the class 'appendix' to
// wrappers of appendix elements or create a new <div> next to the parent
// element to hold all appendix elements
(d => {
const h = d.querySelector([1, 2, 3, 4, 5, 6].map(i => `h${i}.appendix`).join(','));
if (!h) return;
h.classList.remove('appendix');
// if h is in a wrapper whose next sibling has the same class, simply add class to wrappers
const p1 = h.parentNode; let p2 = p1, wrapper = false;
while (p1) {
p2 = p2.nextElementSibling;
if (!p2 || p1.tagName !== p2.tagName || p1.className !== p2.className) break;
p2.classList.add('appendix');
wrapper = true;
}
if (wrapper) {
p1.classList.add('appendix');
return;
}
// create a new div instead
const a = d.createElement('div');
a.className = 'appendix';
a.append(h.cloneNode(true));
Expand Down

0 comments on commit 08a6dfc

Please sign in to comment.