MediaWiki:Gadget-autoNumberHeadings.js

From Working With Glass
Revision as of 04:35, 6 November 2026 by Claycorp (talk | contribs) (Created page with "→* * Auto-number headings, modified to use a template to trigger. * @source https://www.mediawiki.org/wiki/Snippets/Auto-number_headings * @author Krinkle * @version 2024-07-28: (function () { if (!document.querySelector('.enable-autonum')) { return; } var toc = document.querySelector('#toc'); if (toc) { document.body.classList.add('tpl-autonum-attr'); // Support legacy Parser: <h2><span class=mw-headline id=…>...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Auto-number headings, modified to use a template to trigger.
 * @source https://www.mediawiki.org/wiki/Snippets/Auto-number_headings
 * @author Krinkle
 * @version 2024-07-28
 */
(function () {
    if (!document.querySelector('.enable-autonum')) {
        return;
    }
    var toc = document.querySelector('#toc');
    if (toc) {
        document.body.classList.add('tpl-autonum-attr');
        // Support legacy Parser: <h2><span class=mw-headline id=…>  
        // Support Parsoid: <section><div class=mw-heading><h2 id…>  
        document.querySelectorAll('.mw-parser-output :is(h1,h2,h3,h4,h5,h6) .mw-headline[id], .mw-parser-output .mw-heading [id]:is(h1,h2,h3,h4,h5,h6)').forEach(function (headline) {
            var num = toc.querySelector('a[href="#' + CSS.escape(headline.id) + '"] .tocnumber');
            if (num) headline.setAttribute('data-autonum', num.textContent);
        });
    } else {
        document.body.classList.add('tpl-autonum');
    }
})();