Jump to content

MediaWiki:Gadget-autonum.js: Difference between revisions

From wikiNonStop
Set number as data attribute instead of inline text, and display via CSS before pseudo-element. This matches the way it works on pages without TOC. This is also fixes the issue of gadgets reading text from headings, reported by @JWBTH, Talk:Snippets/Auto-number_headings#c-JWBTH-20240722135600-Wrap_numbers_in_a_class.
 
m 1 revision imported: Initial setup
 
(No difference)

Latest revision as of 07:53, 25 December 2024

/**
 * Auto-number headings
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Auto-number_headings
 * @author Krinkle
 * @version 2024-07-28
 */
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');
}