Normalised page nav header inset when only small headers are used

This commit is contained in:
Dan Brown 2017-01-02 12:13:03 +00:00
parent 3595ac2551
commit 7c8c4c2a05
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 14 additions and 5 deletions

View File

@ -836,7 +836,7 @@ class EntityRepo
/**
* Parse the headers on the page to get a navigation menu
* @param Page $page
* @return array
* @return Collection
*/
public function getPageNav(Page $page)
{
@ -849,15 +849,24 @@ class EntityRepo
if (is_null($headers)) return null;
$tree = [];
$tree = collect([]);
foreach ($headers as $header) {
$text = $header->nodeValue;
$tree[] = [
$tree->push([
'nodeName' => strtolower($header->nodeName),
'level' => intval(str_replace('h', '', $header->nodeName)),
'link' => '#' . $header->getAttribute('id'),
'text' => strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text
];
]);
}
// Normalise headers if only smaller headers have been used
if (count($tree) > 0) {
$minLevel = $tree->pluck('level')->min();
$tree = $tree->map(function($header) use ($minLevel) {
$header['level'] -= ($minLevel - 2);
return $header;
});
}
return $tree;
}

View File

@ -30,7 +30,7 @@
<h6 class="text-muted">{{ trans('entities.pages_navigation') }}</h6>
<div class="sidebar-page-nav menu">
@foreach($pageNav as $navItem)
<li class="page-nav-item {{ $navItem['nodeName'] }}">
<li class="page-nav-item h{{ $navItem['level'] }}">
<a href="{{ $navItem['link'] }}">{{ $navItem['text'] }}</a>
</li>
@endforeach