Skip to content

Commit

Permalink
Change internal search generator method to return JSON directly
Browse files Browse the repository at this point in the history
Since saving to disk is handled by the build process, this class now just needs to be responsible for generating the JSON. See #1498
  • Loading branch information
caendesilva committed Dec 18, 2023
1 parent 02ed17d commit 1c37851
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,40 @@

namespace Hyde\Framework\Actions;

use Hyde\Hyde;
use Hyde\Facades\Config;
use Hyde\Facades\Filesystem;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Pages\DocumentationPage;
use Illuminate\Support\Collection;
use Hyde\Framework\Features\Documentation\DocumentationSearchIndex;

use function basename;
use function in_array;
use function trim;

/**
* @internal Generate a JSON file that can be used as a search index for documentation pages.
* @internal Generate a JSON string that can be used as a search index for documentation pages.
*/
class GeneratesDocumentationSearchIndex
{
use InteractsWithDirectories;

protected Collection $index;
protected string $path;

/**
* Generate the search index and save it to disk.
* @since v2.x This method returns the JSON string instead of saving it to disk and returning the path.
*
* @return string The path to the generated file.
*/
public static function handle(): string
{
$service = new static();
$service->run();
$service->save();

return $service->path;
return $service->index->toJson();
}

protected function __construct()
{
$this->index = new Collection();
$this->path = $this->getPath();
}

protected function run(): void
Expand All @@ -68,13 +62,6 @@ protected function generatePageEntry(DocumentationPage $page): array
];
}

protected function save(): void
{
$this->needsParentDirectory($this->path);

Filesystem::putContents($this->path, $this->index->toJson());
}

protected function getSearchContentForDocument(DocumentationPage $page): string
{
return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute();
Expand All @@ -89,11 +76,6 @@ protected function formatDestination(string $slug): string
return "$slug.html";
}

protected function getPath(): string
{
return Hyde::sitePath(DocumentationSearchIndex::outputPath());
}

protected function getPagesToExcludeFromSearch(): array
{
return array_merge(Config::getArray('docs.exclude_from_search', []), Config::getBool('docs.create_search_page', true) ? ['search'] : []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()

public function compile(): string
{
return GeneratesDocumentationSearchIndex::generate();
return GeneratesDocumentationSearchIndex::handle();
}

public static function outputPath(string $identifier = ''): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Hyde\Framework\Testing\Feature\Services;

use Hyde\Facades\Filesystem;
use Hyde\Framework\Actions\GeneratesDocumentationSearchIndex;
use Hyde\Hyde;
use Hyde\Testing\CreatesTemporaryFiles;
Expand Down Expand Up @@ -32,14 +31,12 @@ public function test_it_generates_a_json_file_with_a_search_index()
{
$this->file('_docs/foo.md');

GeneratesDocumentationSearchIndex::handle();

$this->assertSame(json_encode([[
'slug' => 'foo',
'title' => 'Foo',
'content' => '',
'destination' => 'foo.html',
]]), file_get_contents('_site/docs/search.json'));
]]), GeneratesDocumentationSearchIndex::handle());
}

public function test_it_adds_all_files_to_search_index()
Expand All @@ -53,20 +50,7 @@ public function test_it_adds_all_files_to_search_index()

public function test_it_handles_generation_even_when_there_are_no_pages()
{
GeneratesDocumentationSearchIndex::handle();

$this->assertSame('[]', file_get_contents('_site/docs/search.json'));

Filesystem::unlink('_site/docs/search.json');
}

public function test_save_method_saves_the_file_to_the_correct_location()
{
GeneratesDocumentationSearchIndex::handle();

$this->assertFileExists('_site/docs/search.json');

Filesystem::unlink('_site/docs/search.json');
$this->assertSame('[]', GeneratesDocumentationSearchIndex::handle());
}

public function test_it_generates_a_valid_JSON()
Expand Down Expand Up @@ -134,10 +118,6 @@ public function test_nested_source_files_do_not_retain_directory_name_in_search_

protected function getArray(): array
{
GeneratesDocumentationSearchIndex::handle();
$array = json_decode(file_get_contents('_site/docs/search.json'), true);
Filesystem::unlink('_site/docs/search.json');

return $array;
return json_decode(GeneratesDocumentationSearchIndex::handle(), true);
}
}

0 comments on commit 1c37851

Please sign in to comment.