미디어위키:Gadget-autonum.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
 
1번째 줄: 1번째 줄:
/**
* Auto-number headings (fixed for no-TOC pages)
*/
var toc = document.querySelector('#toc');
var toc = document.querySelector('#toc');



2026년 2월 24일 (화) 14:35 기준 최신판

var toc = document.querySelector('#toc');

var headings = 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)'
);

if (toc) {
  document.body.classList.add('tpl-autonum-attr');

  headings.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-attr');

  let counters = [0, 0, 0, 0, 0, 0];

  headings.forEach(function (headline) {
    var level = parseInt(headline.closest('h1,h2,h3,h4,h5,h6').tagName[1], 10) - 1;

    counters[level]++;
    for (let i = level + 1; i < counters.length; i++) {
      counters[i] = 0;
    }

    let number = counters
      .slice(0, level + 1)
      .filter(n => n > 0)
      .join('.');

    headline.setAttribute('data-autonum', number);
  });
}