nav - don't show empty categories

This commit is contained in:
isUnknown 2024-06-20 09:50:39 +02:00
parent 09e122af64
commit bdb8613be4
9 changed files with 295 additions and 276 deletions

View file

@ -1,27 +1,41 @@
<?php
return function ($site) {
$textsPage = $site->find('texts');
$years = $textsPage->children();
$texts = $years->children()->index();
function createCategories($textsPage) {
$categories = array();
foreach ($textsPage->categories()->split() as $category) {
$categories[$category] = array(
'title' => $category,
'texts' => array()
);
}
return $categories;
}
function fillCategoriesWithTexts($emptyCategories, $texts) {
$filledCategories = $emptyCategories;
foreach ($texts as $text) {
try {
$categories[$text->category()->value()]['texts'][] = $text;
$textCategory = $text->category()->value();
$filledCategories[$textCategory]['texts'][] = $text;
} catch (\Throwable $th) {
throw new Exception(json_encode($th->getFile() . ' : ' . $th->getMessage()));
}
}
return $categories;
// exclude empty categories
return array_filter($filledCategories, function($category) {
return count($category['texts']) > 0;
});
}
return function ($site) {
$textsPage = $site->find('texts');
$years = $textsPage->children();
$texts = $years->children()->index();
$emptyCategories = createCategories($textsPage);
$filledCategories = fillCategoriesWithTexts($emptyCategories, $texts);
return $filledCategories;
};