From 0791596cdacfafc2541f94411c3aa2acf67e01a2 Mon Sep 17 00:00:00 2001 From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com> Date: Wed, 28 May 2025 19:16:32 -0300 Subject: [PATCH] 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 --- docs/theme/page-toc.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/theme/page-toc.js b/docs/theme/page-toc.js index 62af2e7dc4..647a381058 100644 --- a/docs/theme/page-toc.js +++ b/docs/theme/page-toc.js @@ -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,