Skip to content

Commit

Permalink
Extract internal route key helper
Browse files Browse the repository at this point in the history
Reduces the amount of times and places we call the exact same logic
  • Loading branch information
caendesilva committed Dec 18, 2023
1 parent 0f8fe02 commit 9b00817
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/HydeSearch.min.js" defer></script>
<script>
window.addEventListener('load', function () {
const searchIndexLocation = '{{ Hyde::relativeLink(ltrim(DocumentationPage::outputDirectory().'/search.json', '/')) }}';
const searchIndexLocation = '{{ Hyde::relativeLink(\Hyde\Framework\Features\Documentation\DocumentationSearchIndex::routeKey()) }}';
const Search = new HydeSearch(searchIndexLocation);
Search.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Hyde\Pages\DocumentationPage;
use Illuminate\Support\Collection;

use Hyde\Framework\Features\Documentation\DocumentationSearchIndex;
use function basename;
use function in_array;
use function trim;
Expand Down Expand Up @@ -101,7 +102,7 @@ protected function formatDestination(string $slug): string

protected function getPath(): string
{
return Hyde::sitePath(DocumentationPage::outputDirectory().'/search.json');
return Hyde::sitePath(DocumentationSearchIndex::routeKey());
}

protected function getPagesToExcludeFromSearch(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Hyde\Pages\DocumentationPage;
use Hyde\Framework\Actions\GeneratesDocumentationSearchIndex;

use function ltrim;

/**
* @internal This page is used to render the search index for the documentation.
*/
Expand All @@ -18,7 +20,7 @@ class DocumentationSearchIndex extends InMemoryPage
*/
public function __construct()
{
parent::__construct(ltrim(DocumentationPage::outputDirectory().'/search.json', '/'), [
parent::__construct(static::routeKey(), [
'navigation' => ['hidden' => true],
]);
}
Expand All @@ -29,6 +31,11 @@ public function compile(): string
}

public function getOutputPath(): string
{
return static::routeKey();
}

public static function routeKey(): string
{
return ltrim(DocumentationPage::outputDirectory().'/search.json', '/');
}
Expand Down
17 changes: 17 additions & 0 deletions packages/framework/tests/Feature/DocumentationSearchIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ public function testRouteKeyIsSetToConfiguredDocumentationOutputDirectory()
$page = new DocumentationSearchIndex();
$this->assertSame('foo/search.json', $page->routeKey);
}

public function testStaticRouteKeyHelper()
{
$this->assertSame('docs/search.json', DocumentationSearchIndex::routeKey());
}

public function testStaticRouteKeyHelperWithCustomOutputDirectory()
{
DocumentationPage::setOutputDirectory('foo');
$this->assertSame('foo/search.json', DocumentationSearchIndex::routeKey());
}

public function testStaticRouteKeyHelperWithRootOutputDirectory()
{
DocumentationPage::setOutputDirectory('');
$this->assertSame('search.json', DocumentationSearchIndex::routeKey());
}
}

0 comments on commit 9b00817

Please sign in to comment.