refactoring avec claude + ajout scroll-margin-top et désaffichage du panel au click sur les liens du toc

This commit is contained in:
antonin gallon 2026-02-17 18:10:04 +01:00
parent d51fc592ed
commit 01c5b098e4
14 changed files with 149 additions and 121 deletions

View file

@ -0,0 +1,47 @@
<?php
Kirby::plugin('actuel-inactuel/toc', [
'pageMethods' => [
/**
* 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('/<h3>/', $this->bodyBlocks()->toBlocks());
},
/**
* Retourne les items de la TOC
*/
'tocItems' => function(): array {
if (!$this->bodyBlocks()?->isNotEmpty()) {
return [];
}
preg_match_all('/<h3>(.*?)<\/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>(.*?)<\/h3>/',
fn($m) => '<h3 id="' . Str::slug($m[1]) . '">' . $m[1] . '</h3>',
$this->bodyBlocks()->toBlocks()
);
}
]
]);