MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 141:
document.body;
 
// Collect headings (H2–H6).H2–H5; Preferh6 spansis withbold .mw-headlinebody (stabletext, anchorexcluded idsfrom the TOC).
// Prefer spans with .mw-headline (stable anchor ids)
var items = [];
var headings = root.querySelectorAll('h2, h3, h4, h5, h6');
headings.forEach(function (h) {
var level = parseInt(h.tagName.slice(1), 10);
if (level < 2 || level > 65) return;
var headline = h.querySelector('.mw-headline') || h;
var id = headline.id || h.id;
Line 688 ⟶ 689:
}, function (e) { mw.log('TermMap review: mediawiki.api failed to load', e); });
});
 
 
/* ── Hide level-6 headings from the sidebar (Vector) TOC ─────────────────────
MediaWiki's TOC classes (vector-toc-level-N) encode only a COMPACTED nesting
depth, NOT the original heading tag — when levels jump (h3->h6) it still only
increments one level — so CSS cannot reliably target h6. Resolve each TOC
entry to its body heading and hide the ones that are actually <h6>. Safe: it
only ever hides h6 (never h2–h5). The mobile overlay excludes h6 separately
(above). */
(function () {
function headingFor(anchor) {
if (!anchor) { return null; }
var el = document.getElementById(anchor);
if (!el) {
try { el = document.getElementById(decodeURIComponent(anchor)); } catch (e) {}
}
if (!el) { return null; }
if (el.matches && el.matches('h1,h2,h3,h4,h5,h6')) { return el; }
return el.closest ? el.closest('h1,h2,h3,h4,h5,h6') : null;
}
function pruneTocH6() {
var items = document.querySelectorAll('.vector-toc .vector-toc-list-item');
Array.prototype.forEach.call(items, function (li) {
var a = li.querySelector('a.vector-toc-link');
if (!a) { return; }
var href = a.getAttribute('href') || '';
if (href.charAt(0) !== '#' || href.length < 2) { return; }
var h = headingFor(href.slice(1));
if (h && h.tagName === 'H6') { li.style.display = 'none'; }
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', pruneTocH6);
} else {
pruneTocH6();
}
// Vector can lazily (re)build the pinned TOC; re-run on its content hook.
if (window.mw && mw.hook) { mw.hook('wikipage.content').add(pruneTocH6); }
})();