FIX updating function build_sommaire_fromindex and handling only h2

This commit is contained in:
Nonimart 2025-06-26 17:35:44 +02:00
parent 3195587c11
commit 46de3ba8f2

View File

@ -86,7 +86,22 @@ function build_sommaire_from_content($postID)
$titleBlocks = array_filter(
$blocks,
function ($block) {
return $block['blockName'] === 'core/heading' && isset($block['attrs']['level']) && in_array($block['attrs']['level'], array(2, 3), true);
// Vérifier si c'est un bloc heading
if ($block['blockName'] !== 'core/heading') {
return false;
}
// Extraire le niveau depuis le HTML si les attributs sont vides
if (empty($block['attrs']['level'])) {
// Chercher seulement h2 dans le HTML
if (preg_match('/<h2[^>]*>/i', $block['innerHTML'], $matches)) {
return true;
}
return false;
}
// Utiliser le niveau des attributs s'il existe
return $block['attrs']['level'] === 2;
}
);
@ -94,17 +109,24 @@ function build_sommaire_from_content($postID)
foreach ($titleBlocks as $block) {
$title = strip_tags($block['innerHTML']);
// Extraire le niveau depuis le HTML ou les attributs
$level = $block['attrs']['level'] ?? null;
if (!$level && preg_match('/<h2[^>]*>/i', $block['innerHTML'], $matches)) {
$level = 2;
}
if ($level !== 2) continue;
$anchor = $block['attrs']['idName'] ?? sanitize_title($title);
$level = $block['attrs']['level'];
$outputIndex[] = [
'title' => $title,
'anchor' => $anchor,
'level' => $level,
];
}
return $outputIndex;
}