docs: Hide "on this page" element when there are no headings (#31635)

We were still showing the "On this page" element even when the page
didn't contain any h2s or h3s.

Release Notes:

- N/A
This commit is contained in:
Danilo Leal 2025-05-28 19:16:32 -03:00 committed by GitHub
parent 9cc1851be7
commit 0791596cda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -59,6 +59,19 @@ const updateFunction = () => {
window.addEventListener("load", () => {
const pagetoc = getPagetoc();
const headers = [...document.getElementsByClassName("header")];
const nonH1Headers = headers.filter(
(header) => !header.parentElement.tagName.toLowerCase().startsWith("h1"),
);
const sidetoc = document.querySelector(".sidetoc");
if (nonH1Headers.length === 0) {
if (sidetoc) {
sidetoc.style.display = "none";
}
return;
}
headers.forEach((header) => {
const link = Object.assign(document.createElement("a"), {
textContent: header.text,