mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-02 07:20:05 +00:00
parent
0ee9e5c4db
commit
896f88174a
3 changed files with 50 additions and 14 deletions
|
@ -422,25 +422,29 @@ class PageRepo extends EntityRepo
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$tree = collect([]);
|
$tree = collect($headers)->map(function($header) {
|
||||||
foreach ($headers as $header) {
|
$text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
|
||||||
$text = $header->nodeValue;
|
if (strlen($text) > 30) {
|
||||||
$tree->push([
|
$text = substr($text, 0, 27) . '...';
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
'nodeName' => strtolower($header->nodeName),
|
'nodeName' => strtolower($header->nodeName),
|
||||||
'level' => intval(str_replace('h', '', $header->nodeName)),
|
'level' => intval(str_replace('h', '', $header->nodeName)),
|
||||||
'link' => '#' . $header->getAttribute('id'),
|
'link' => '#' . $header->getAttribute('id'),
|
||||||
'text' => strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text
|
'text' => $text,
|
||||||
]);
|
];
|
||||||
}
|
})->filter(function($header) {
|
||||||
|
return strlen($header['text']) > 0;
|
||||||
|
});
|
||||||
|
|
||||||
// Normalise headers if only smaller headers have been used
|
// Normalise headers if only smaller headers have been used
|
||||||
if (count($tree) > 0) {
|
$minLevel = $tree->pluck('level')->min();
|
||||||
$minLevel = $tree->pluck('level')->min();
|
$tree = $tree->map(function ($header) use ($minLevel) {
|
||||||
$tree = $tree->map(function ($header) use ($minLevel) {
|
$header['level'] -= ($minLevel - 2);
|
||||||
$header['level'] -= ($minLevel - 2);
|
return $header;
|
||||||
return $header;
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
return $tree->toArray();
|
return $tree->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
tests/Unit/PageRepoTest.php
Normal file
32
tests/Unit/PageRepoTest.php
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use BookStack\Entities\Repos\PageRepo;
|
||||||
|
|
||||||
|
class PageRepoTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var PageRepo $pageRepo
|
||||||
|
*/
|
||||||
|
protected $pageRepo;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->pageRepo = app()->make(PageRepo::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_page_nav_does_not_show_empty_titles()
|
||||||
|
{
|
||||||
|
$content = '<h1 id="testa">Hello</h1><h2 id="testb"> </h2><h3 id="testc"></h3>';
|
||||||
|
$navMap = $this->pageRepo->getPageNav($content);
|
||||||
|
|
||||||
|
$this->assertCount(1, $navMap);
|
||||||
|
$this->assertArraySubset([
|
||||||
|
'nodeName' => 'h1',
|
||||||
|
'link' => '#testa',
|
||||||
|
'text' => 'Hello'
|
||||||
|
], $navMap[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue