Highlight the active page in the navbar

Closes #6

Also dropped PHPStan back down to level 5 because I decided level 6 was a bit much
This commit is contained in:
Belle Aerni 2023-01-12 19:08:35 -08:00
parent 325221f527
commit 4f96f5c496
7 changed files with 42 additions and 22 deletions

24
composer.lock generated
View file

@ -1351,16 +1351,16 @@
},
{
"name": "phpstan/phpstan",
"version": "1.9.9",
"version": "1.9.11",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "f68d7cc3d0638a01bc6321cb826e4cae7fa6884d"
"reference": "60f3d68481eef216199eae7a2603cd5fe124d464"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/f68d7cc3d0638a01bc6321cb826e4cae7fa6884d",
"reference": "f68d7cc3d0638a01bc6321cb826e4cae7fa6884d",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/60f3d68481eef216199eae7a2603cd5fe124d464",
"reference": "60f3d68481eef216199eae7a2603cd5fe124d464",
"shasum": ""
},
"require": {
@ -1390,7 +1390,7 @@
],
"support": {
"issues": "https://github.com/phpstan/phpstan/issues",
"source": "https://github.com/phpstan/phpstan/tree/1.9.9"
"source": "https://github.com/phpstan/phpstan/tree/1.9.11"
},
"funding": [
{
@ -1406,7 +1406,7 @@
"type": "tidelift"
}
],
"time": "2023-01-11T14:39:22+00:00"
"time": "2023-01-12T14:04:13+00:00"
},
{
"name": "phpunit/php-code-coverage",
@ -1830,16 +1830,16 @@
},
{
"name": "rector/rector",
"version": "0.15.4",
"version": "0.15.5",
"source": {
"type": "git",
"url": "https://github.com/rectorphp/rector.git",
"reference": "1dcdf54ebf502d9f7cf9b5f89df613da1774d050"
"reference": "b27945f63527b540dba09a4f4612bc738da9c285"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/rectorphp/rector/zipball/1dcdf54ebf502d9f7cf9b5f89df613da1774d050",
"reference": "1dcdf54ebf502d9f7cf9b5f89df613da1774d050",
"url": "https://api.github.com/repos/rectorphp/rector/zipball/b27945f63527b540dba09a4f4612bc738da9c285",
"reference": "b27945f63527b540dba09a4f4612bc738da9c285",
"shasum": ""
},
"require": {
@ -1874,7 +1874,7 @@
"description": "Instant Upgrade and Automated Refactoring of any PHP code",
"support": {
"issues": "https://github.com/rectorphp/rector/issues",
"source": "https://github.com/rectorphp/rector/tree/0.15.4"
"source": "https://github.com/rectorphp/rector/tree/0.15.5"
},
"funding": [
{
@ -1882,7 +1882,7 @@
"type": "github"
}
],
"time": "2023-01-10T11:09:49+00:00"
"time": "2023-01-12T19:13:08+00:00"
},
{
"name": "sebastian/cli-parser",

View file

@ -1,5 +1,5 @@
parameters:
level: 6
level: 5
paths:
- src
excludePaths:

View file

@ -27,7 +27,7 @@ class AntCMS
$markdown = AntMarkdown::renderMarkdown($content['content']);
$pageTemplate = $this->getPageLayout();
$pageTemplate = $this->getPageLayout(null, $page);
$params = array(
'AntCMSTitle' => $content['title'],
@ -52,14 +52,15 @@ class AntCMS
* Returns the default layout of the active theme unless otherwise specified.
*
* @param string|null $theme optional - the theme to get the page layout for.
* @param string $currentPage optional - What page is the active page.
* @return string the default page layout
*/
public function getPageLayout(string $theme = null)
public function getPageLayout(string $theme = null, string $currentPage = '')
{
$siteInfo = AntCMS::getSiteInfo();
$pageTemplate = $this->getThemeTemplate('default_layout', $theme);
$pageTemplate = str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation($this->getThemeTemplate('nav_layout', $theme)), $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation($this->getThemeTemplate('nav_layout', $theme), $currentPage), $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteTitle-->', $siteInfo['siteTitle'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteLink-->', '//' . AntConfig::currentConfig('baseURL'), $pageTemplate);

View file

@ -40,16 +40,17 @@ class AntPages
}
/**
* @param string $navTemplate
* @param string $navTemplate
* @param string $currentPage optional - What page is the active page. Used for highlighting the active page in the navbar
* @return string
*/
public static function generateNavigation(string $navTemplate = '')
public static function generateNavigation(string $navTemplate = '', string $currentPage = '')
{
$pages = AntPages::getPages();
$cache = new AntCache;
$theme = AntConfig::currentConfig('activeTheme');
$cacheKey = $cache->createCacheKey(json_encode($pages), $theme);
$cacheKey = $cache->createCacheKey(json_encode($pages), $theme . $currentPage);
if ($cache->isCached($cacheKey)) {
$cachedContent = $cache->getCache($cacheKey);
@ -59,14 +60,20 @@ class AntPages
}
}
$currentPage = strtolower($currentPage);
if (str_ends_with($currentPage, '/')) {
$currentPage = $currentPage . 'index.md';
}
$baseURL = AntConfig::currentConfig('baseURL');
foreach ($pages as $key => $page) {
$url = "//" . AntTools::repairURL($baseURL . $page['functionalPagePath']);
$pages[$key]['url'] = $url;
$pages[$key]['active'] = ($currentPage == strtolower($page['functionalPagePath'])) ? true : false;
}
$antTwig = new AntTwig;
$navHTML = $antTwig->renderWithTiwg($navTemplate, array('pages' =>$pages));
$navHTML = $antTwig->renderWithTiwg($navTemplate, array('pages' => $pages));
$cache->setCache($cacheKey, $navHTML);
return $navHTML;

View file

@ -1,5 +1,11 @@
{% for page in pages %}
{% if page.active %}
<li class="nav-item active">
<a class="nav-link" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% endif %}
{% endfor %}

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,11 @@
{% for page in pages %}
<li class="nav-item active">
{% if page.active %}
<li>
<a class="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-gray-200 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% else %}
<li>
<a class="block py-2 pl-3 pr-4 text-gray-700 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% endif %}
{% endfor %}