[
/**
* Vérifie si la page doit afficher une TOC
*/
'hasToc' => function(): bool {
if (!$this->parent()?->parent()?->is('textes')) {
return false;
}
if (!$this->bodyBlocks()?->isNotEmpty()) {
return false;
}
return (bool) preg_match('/
/', $this->bodyBlocks()->toBlocks());
},
/**
* Retourne les items de la TOC
*/
'tocItems' => function(): array {
if (!$this->bodyBlocks()?->isNotEmpty()) {
return [];
}
preg_match_all('/(.*?)<\/h3>/', $this->bodyBlocks()->toBlocks(), $matches);
return array_map(fn($title) => [
'title' => $title,
'slug' => Str::slug($title)
], $matches[1]);
},
/**
* Retourne le contenu avec les ancres ajoutées aux h3
*/
'bodyWithAnchors' => function(): string {
if (!$this->bodyBlocks()?->isNotEmpty()) {
return '';
}
return preg_replace_callback(
'/(.*?)<\/h3>/',
fn($m) => '' . $m[1] . '
',
$this->bodyBlocks()->toBlocks()
);
}
]
]);