refactoring avec claude + ajout scroll-margin-top et désaffichage du panel au click sur les liens du toc
This commit is contained in:
parent
d51fc592ed
commit
01c5b098e4
14 changed files with 149 additions and 121 deletions
47
site/plugins/toc/index.php
Normal file
47
site/plugins/toc/index.php
Normal 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()
|
||||
);
|
||||
}
|
||||
]
|
||||
]);
|
||||
Loading…
Add table
Add a link
Reference in a new issue