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

편집 요약 없음
태그: 되돌려진 기여
편집 요약 없음
 
(같은 사용자의 중간 판 6개는 보이지 않습니다)
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');
}


mw.loader.using(['mediawiki.util'], function () {
  let counters = [0, 0, 0, 0, 0, 0];
    $(function () {
        // 검색 특별 문서에서만 동작
        if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') {
            return;
        }


        var $results = $('.searchresults');
  headings.forEach(function (headline) {
        if (!$results.length) {
    var level = parseInt(headline.closest('h1,h2,h3,h4,h5,h6').tagName[1], 10) - 1;
            return;
        }


        // OOUI 검색 입력창 (#ooui-php-1)에서 검색어 가져오기
    counters[level]++;
        var $input = $('#ooui-php-1');
    for (let i = level + 1; i < counters.length; i++) {
        if (!$input.length) {
      counters[i] = 0;
            return;
    }
        }


        var title = $.trim($input.val());
    let number = counters
        if (!title) {
      .slice(0, level + 1)
            return;
      .filter(n => n > 0)
        }
      .join('.');


        // 해당 제목 문서의 URL
    headline.setAttribute('data-autonum', number);
        var url = mw.util.getUrl(title);
  });
 
}
        // 박스 생성
        var $box = $('<div>')
            .addClass('mw-search-go-box')
            .css({
                'border': '1px solid var(--border)',
                'padding': '0.75em 1em',
                'margin-bottom': '1em',
                'background': 'var(--altbg)',
                'color': 'var(--text)',
                'border-radius': '0.5rem',
                'display': 'flex',
                'justify-content': 'space-between',
                'align-content': 'baseline'
            });
 
        // 설명 텍스트
        var $text = $('<span>')
            .text('찾는 문서가 없나요? 문서로 바로 갈 수 있습니다.')
                    .css({
            'padding':'4px',
            });
 
        // 링크
        var $link = $('<a>')
            .attr('href', url)
            .text('"' + title + '" 문서')
            .css({
            'padding':'4px',
            });
 
        // 버튼
        var $button = $('<button>')
            .text("'" + title + "'" + ' 문서로 가기')
            .css({
                'margin-left': '0.75em',
                'cursor': 'pointer',
                'background': 'var(--bg)',
                'color': 'var(--text)',
                'border-radius': '0.5rem',
                'text-align': 'right',
                'border': '1px solid var(--border)',
            })
            .on('click', function () {
                location.href = url;
            });
 
        // 조립
        $box.append($text, $button);
 
        // .searchresults 위에 삽입
        $results.first().before($box);
    });
});

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