MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 449: | Строка 449: | ||
$toc.empty(); | $toc.empty(); | ||
var $headings = $content.find('h1, h2, h3, h4, h5, h6, .custom-heading'); | var $headings = $content.find('h1, h2, h3, h4, h5, h6, .custom-heading'); | ||
var tocCounters = [0]; | var tocCounters = [0]; | ||
var lastLevel = 1; | var lastLevel = 1; | ||
var $currentList = $toc; | var $currentList = $toc; | ||
function updateCounters(level) { | function updateCounters(level) { | ||
if (level > lastLevel) { | if (level > lastLevel) { | ||
tocCounters.push(0); | tocCounters.push(0); | ||
} else if (level < lastLevel) { | } else if (level < lastLevel) { | ||
tocCounters = tocCounters.slice(0, level); | tocCounters = tocCounters.slice(0, level); | ||
} | } | ||
tocCounters[level - 1]++; | tocCounters[level - 1]++; | ||
| Строка 476: | Строка 476: | ||
var $heading = $(this); | var $heading = $(this); | ||
var level; | var level; | ||
var $headlineSpan = $heading.find('span.mw-headline'); | |||
if ($heading.hasClass('custom-heading')) { | if ($heading.hasClass('custom-heading')) { | ||
level = 1; | level = 1; | ||
var customId = $heading.attr('id') || 'custom-heading-' + getSectionNumber(); | var customId = $heading.attr('id') || 'custom-heading-' + getSectionNumber(); | ||
$heading.attr('id', customId); | $heading.attr('id', customId); | ||
} else | } else if ($headlineSpan.length > 0) { | ||
var tagName = $heading.prop('tagName').toLowerCase(); | var tagName = $heading.prop('tagName').toLowerCase(); | ||
level = parseInt(tagName.charAt(1), 10); | level = parseInt(tagName.charAt(1), 10); | ||
var sectionId = 'heading-' + getSectionNumber(); | var existingId = $headlineSpan.attr('id'); | ||
$headlineSpan.attr('id', | |||
// Генерация нового ID, если он уже существует | |||
if (!existingId) { | |||
var sectionId = 'heading-' + getSectionNumber(); | |||
$headlineSpan.attr('id', sectionId); | |||
} else { | |||
// Если ID существует, добавляем уникальный суффикс | |||
var uniqueId = existingId; | |||
var counter = 1; | |||
while ($(`#${uniqueId}`).length > 0) { | |||
uniqueId = existingId + '-' + counter++; | |||
} | |||
$headlineSpan.attr('id', uniqueId); | |||
} | |||
} else { | |||
return; | |||
} | } | ||
updateCounters(level); | updateCounters(level); | ||
var headingText = $headlineSpan.text().trim() || $heading.text().trim(); | var headingText = $headlineSpan.text().trim() || $heading.text().trim(); | ||
if (headingText.length > 0) { | if (headingText.length > 0) { | ||
if (level > 1) { | if (level > 1) { | ||
| Строка 515: | Строка 527: | ||
); | ); | ||
$currentList.append(tocItem); | $currentList.append(tocItem); | ||
} | } | ||
}); | }); | ||