Skip to content

Instantly share code, notes, and snippets.

@Termplexed
Created April 20, 2021 18:29
Show Gist options
  • Save Termplexed/bc8677a9c396786c9a080fee3a6eb87e to your computer and use it in GitHub Desktop.
Save Termplexed/bc8677a9c396786c9a080fee3a6eb87e to your computer and use it in GitHub Desktop.
Option to collapse TOC items on nodejs.org
((s) => {
s = document.createElement('style');
s.textContent = `
li[data-open="0"] ul {
display:none;
}
li[data-open="0"] {
filter:brightness(0.7) hue-rotate(45deg);
font-weight: 700;
list-style-type: disc!important;
}
`;
document.head.append(s);
document.querySelector('#toc')
.querySelectorAll('ul')
.forEach((u) => {
const p = u.parentElement;
if (p.tagName !== 'LI')
return;
if (p.dataset.open == "1")
return;
p.dataset.open = 1;
let a = document.createElement('a');
a.textContent = " \u25BC ";
a.href = "javascript:;";
p.insertBefore(a, p.firstChild);
a.addEventListener('click', (e) => {
e = e.target;
let l = e.closest('li');
if ( l ) {
let x = l.dataset;
if (x.open == "1") {
x.open = 0;
e.textContent = " \u25B6 ";
} else {
x.open = 1;
e.textContent = " \u25BC ";
}
}
}, false);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment