|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Framework\Actions\ConvertsMarkdownToPlainText; |
|
8
|
|
|
use Hyde\Framework\Concerns\InteractsWithDirectories; |
|
9
|
|
|
use Hyde\Hyde; |
|
10
|
|
|
use Hyde\Pages\DocumentationPage; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @internal Generate a JSON file that can be used as a search index for documentation pages. |
|
15
|
|
|
* |
|
16
|
|
|
* @see \Hyde\Framework\Testing\Feature\Services\DocumentationSearchServiceTest |
|
17
|
|
|
*/ |
|
18
|
|
|
final class DocumentationSearchService |
|
19
|
|
|
{ |
|
20
|
|
|
use InteractsWithDirectories; |
|
21
|
|
|
|
|
22
|
|
|
public Collection $searchIndex; |
|
23
|
|
|
protected string $filePath; |
|
24
|
|
|
|
|
25
|
|
|
public static function generate(): self |
|
26
|
|
|
{ |
|
27
|
|
|
return (new self())->execute(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function generateSearchPage(): string |
|
31
|
|
|
{ |
|
32
|
|
|
$outputDirectory = Hyde::sitePath(DocumentationPage::outputDirectory()); |
|
33
|
|
|
self::needsDirectory($outputDirectory); |
|
34
|
|
|
|
|
35
|
|
|
file_put_contents( |
|
36
|
|
|
"$outputDirectory/search.html", |
|
37
|
|
|
view('hyde::pages.documentation-search')->render() |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
return $outputDirectory; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->searchIndex = new Collection(); |
|
46
|
|
|
$this->filePath = $this->getFilePath(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function execute(): self |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->run()->save(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function run(): self |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var \Hyde\Pages\DocumentationPage $page */ |
|
57
|
|
|
foreach (DocumentationPage::all() as $page) { |
|
58
|
|
|
if (! in_array($page->identifier, config('docs.exclude_from_search', []))) { |
|
59
|
|
|
$this->searchIndex->push( |
|
60
|
|
|
$this->generatePageEntry($page) |
|
|
|
|
|
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array{slug: string, title: string, content: string, destination: string} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function generatePageEntry(DocumentationPage $page): array |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
'slug' => basename($page->identifier), |
|
75
|
|
|
'title' => $page->title, |
|
76
|
|
|
'content' => trim($this->getSearchContentForDocument($page)), |
|
77
|
|
|
'destination' => $this->formatDestination(basename($page->identifier)), |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function save(): self |
|
82
|
|
|
{ |
|
83
|
|
|
$this->needsParentDirectory(Hyde::path($this->filePath)); |
|
84
|
|
|
|
|
85
|
|
|
file_put_contents(Hyde::path($this->filePath), $this->searchIndex->toJson()); |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function getSearchContentForDocument(DocumentationPage $page): string |
|
91
|
|
|
{ |
|
92
|
|
|
return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function formatDestination(string $slug): string |
|
96
|
|
|
{ |
|
97
|
|
|
if (config('site.pretty_urls', false) === true) { |
|
98
|
|
|
return $slug === 'index' ? '' : $slug; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return "$slug.html"; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function getFilePath(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return Hyde::pathToRelative(Hyde::sitePath( |
|
107
|
|
|
DocumentationPage::outputDirectory().'/search.json' |
|
108
|
|
|
)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|