|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Actions; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Facades\Config; |
|
8
|
|
|
use Hyde\Framework\Concerns\InteractsWithDirectories; |
|
9
|
|
|
use Hyde\Pages\DocumentationPage; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
|
|
12
|
|
|
use function basename; |
|
13
|
|
|
use function in_array; |
|
14
|
|
|
use function trim; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @internal Generate a JSON string that can be used as a search index for documentation pages. |
|
18
|
|
|
*/ |
|
19
|
|
|
class GeneratesDocumentationSearchIndex |
|
20
|
|
|
{ |
|
21
|
|
|
use InteractsWithDirectories; |
|
22
|
|
|
|
|
23
|
|
|
protected Collection $index; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @since v2.x This method returns the JSON string instead of saving it to disk and returning the path. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string The path to the generated file. |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function handle(): string |
|
31
|
|
|
{ |
|
32
|
|
|
$service = new static(); |
|
33
|
|
|
$service->run(); |
|
34
|
|
|
|
|
35
|
|
|
return $service->index->toJson(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->index = new Collection(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function run(): void |
|
44
|
|
|
{ |
|
45
|
|
|
DocumentationPage::all()->each(function (DocumentationPage $page): void { |
|
46
|
|
|
if (! in_array($page->identifier, $this->getPagesToExcludeFromSearch())) { |
|
47
|
|
|
$this->index->push($this->generatePageEntry($page)); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array{slug: string, title: string, content: string, destination: string} |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function generatePageEntry(DocumentationPage $page): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'slug' => basename($page->identifier), |
|
59
|
|
|
'title' => $page->title, |
|
60
|
|
|
'content' => trim($this->getSearchContentForDocument($page)), |
|
61
|
|
|
'destination' => $this->formatDestination(basename($page->identifier)), |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getSearchContentForDocument(DocumentationPage $page): string |
|
66
|
|
|
{ |
|
67
|
|
|
return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function formatDestination(string $slug): string |
|
71
|
|
|
{ |
|
72
|
|
|
if (Config::getBool('hyde.pretty_urls', false) === true) { |
|
73
|
|
|
return $slug === 'index' ? '' : $slug; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return "$slug.html"; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getPagesToExcludeFromSearch(): array |
|
80
|
|
|
{ |
|
81
|
|
|
return array_merge(Config::getArray('docs.exclude_from_search', []), |
|
82
|
|
|
Config::getBool('docs.create_search_page', true) ? ['search'] : [] |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|