]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/EntityRepo.php
Updated issue template and added TinyMCE autolinking
[bookstack] / app / Repos / EntityRepo.php
index 95666a66a6ce36bdb5f69dfb30cb9099ac16b9d1..6eaf0169ab8ed3c8bd3d3588b44b015644746399 100644 (file)
@@ -318,15 +318,15 @@ class EntityRepo
      */
     public function getBookChildren(Book $book, $filterDrafts = false)
     {
-        $q = $this->permissionService->bookChildrenQuery($book->id, $filterDrafts);
+        $q = $this->permissionService->bookChildrenQuery($book->id, $filterDrafts)->get();
         $entities = [];
         $parents = [];
         $tree = [];
 
         foreach ($q as $index => $rawEntity) {
-            if ($rawEntity->entity_type === 'Bookstack\\Page') {
+            if ($rawEntity->entity_type === 'BookStack\\Page') {
                 $entities[$index] = $this->page->newFromBuilder($rawEntity);
-            } else if ($rawEntity->entity_type === 'Bookstack\\Chapter') {
+            } else if ($rawEntity->entity_type === 'BookStack\\Chapter') {
                 $entities[$index] = $this->chapter->newFromBuilder($rawEntity);
                 $key = $entities[$index]->entity_type . ':' . $entities[$index]->id;
                 $parents[$key] = $entities[$index];
@@ -338,7 +338,7 @@ class EntityRepo
 
         foreach ($entities as $entity) {
             if ($entity->chapter_id === 0) continue;
-            $parentKey = 'Bookstack\\Chapter:' . $entity->chapter_id;
+            $parentKey = 'BookStack\\Chapter:' . $entity->chapter_id;
             $chapter = $parents[$parentKey];
             $chapter->pages->push($entity);
         }
@@ -840,26 +840,35 @@ class EntityRepo
      */
     public function getPageNav(Page $page)
     {
-        if ($page->html == '') return null;
+        if ($page->html == '') return [];
         libxml_use_internal_errors(true);
         $doc = new DOMDocument();
         $doc->loadHTML(mb_convert_encoding($page->html, 'HTML-ENTITIES', 'UTF-8'));
         $xPath = new DOMXPath($doc);
         $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
 
-        if (is_null($headers)) return null;
+        if (is_null($headers)) return [];
 
-        $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;
+        return $tree->toArray();
     }
 
     /**