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

새 문서: * * 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-out...
 
편집 요약 없음
 
(같은 사용자의 중간 판 13개는 보이지 않습니다)
1번째 줄: 1번째 줄:
/**
* Auto-number headings
*
* @source https://www.mediawiki.org/wiki/Snippets/Auto-number_headings
* @author Krinkle
* @version 2024-07-28
*/
var toc = document.querySelector('#toc');
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) {
if (toc) {
   document.body.classList.add('tpl-autonum-attr');
   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…>
   headings.forEach(function (headline) {
   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(
     var num = toc.querySelector('a[href="#' + CSS.escape(headline.id) + '"] .tocnumber');
      'a[href="#' + CSS.escape(headline.id) + '"] .tocnumber'
     if (num) headline.setAttribute('data-autonum', num.textContent);
    );
     if (num) {
      headline.setAttribute('data-autonum', num.textContent);
    }
   });
   });
} else {
} else {
   document.body.classList.add('tpl-autonum');
   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);
  });
}
}

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);
  });
}