MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 445: | Строка 445: | ||
// Функции для добавления кастомных заголовков в TOC | // Функции для добавления кастомных заголовков в TOC | ||
function addHeadingsWithTOC() { | function addHeadingsWithTOC() { | ||
mw.hook('wikipage.content').add(function($content) { | |||
var $toc = $('#toc ul'); // Находим TOC | |||
$toc.empty(); | |||
var $headings = $content.find('h1, h2, h3, h4, h5, h6, .custom-heading'); | |||
var tocCounters = []; | |||
var $currentList = $toc; // Текущий список для вставки элементов | |||
var levelStack = []; // Стек для отслеживания уровней заголовков | |||
function updateCounters(level) { | |||
if (level > tocCounters.length) { | |||
tocCounters.push(0); | |||
} else if (level < tocCounters.length) { | |||
tocCounters = tocCounters.slice(0, level); | |||
} | |||
tocCounters[level - 1]++; | |||
} | |||
function getSectionNumber() { | |||
return tocCounters.join('.'); | |||
} | |||
function createNestedList($parent) { | |||
var $nestedList = $('<ul>'); | |||
$parent.append($nestedList); | |||
return $nestedList; | |||
} | |||
function closeNestedLists(targetLevel) { | |||
while (levelStack.length > 0 && levelStack[levelStack.length - 1] >= targetLevel) { | |||
$currentList = $currentList.parent(); // Закрываем последний уровень | |||
levelStack.pop(); | |||
} | |||
} | |||
$headings.each(function() { | |||
var $heading = $(this); | |||
var level; | |||
var $headlineSpan = $heading.find('span.mw-headline'); | |||
if ($heading.hasClass('custom-heading')) { | |||
level = 1; | |||
var customId = $heading.attr('id') || 'custom-heading-' + getSectionNumber(); | |||
$heading.attr('id', customId); | |||
} else if ($headlineSpan.length > 0) { | |||
var tagName = $heading.prop('tagName').toLowerCase(); | |||
level = parseInt(tagName.charAt(1), 10); | |||
var existingId = $headlineSpan.attr('id'); | |||
if (!existingId) { | |||
var sectionId = 'heading-' + getSectionNumber(); | |||
$headlineSpan.attr('id', sectionId); | |||
} | |||
} else { | |||
return; | |||
} | |||
updateCounters(level); | |||
var headingText = $headlineSpan.text().trim() || $heading.text().trim(); | |||
if (headingText.length > 0) { | |||
// Закрываем вложенные списки, если текущий уровень меньше | |||
closeNestedLists(level); | |||
// Если уровень больше последнего уровня в стеке, создаем вложенный список | |||
if (levelStack.length === 0 || level > levelStack[levelStack.length - 1]) { | |||
$currentList = createNestedList($currentList); | |||
levelStack.push(level); | |||
} | |||
// Создаем элемент TOC | |||
var currentId = $headlineSpan.attr('id') || customId; | |||
var tocItem = $('<li>').addClass('toclevel-' + (level - 1)).append( | |||
$('<a>').attr('href', '#' + currentId).append( | |||
$('<span>').addClass('tocnumber').text(getSectionNumber()), | |||
$('<span>').addClass('toctext').text(headingText) | |||
) | |||
); | |||
$currentList.append(tocItem); | |||
} | |||
}); | |||
}); | |||
} | } | ||
// Функция для логики меню создаваемым модулем CategoryMenu | // Функция для логики меню создаваемым модулем CategoryMenu | ||