Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
HasDynamicTitle | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
findTitleForDocument | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
findTitleTagInMarkdown | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Concerns; |
4 | |
5 | use Hyde\Framework\Hyde; |
6 | |
7 | /** |
8 | * Find and get the title to use for a Markdown Document. |
9 | * |
10 | * First check the front matter for a title. If one is not found, |
11 | * it searches the Markdown for a level one heading. Falls back to |
12 | * generating a title from the slug if no other title could be found. |
13 | */ |
14 | trait HasDynamicTitle |
15 | { |
16 | public function findTitleForDocument(): string |
17 | { |
18 | if (isset($this->matter['title'])) { |
19 | return $this->matter['title']; |
20 | } |
21 | |
22 | return $this->findTitleTagInMarkdown($this->body) |
23 | ?: Hyde::titleFromSlug($this->slug); |
24 | } |
25 | |
26 | /** |
27 | * Attempt to find the title based on the first H1 tag. |
28 | */ |
29 | protected function findTitleTagInMarkdown(string $stream): string|false |
30 | { |
31 | $lines = explode("\n", $stream); |
32 | |
33 | foreach ($lines as $line) { |
34 | if (str_starts_with($line, '# ')) { |
35 | return trim(substr($line, 2), ' '); |
36 | } |
37 | } |
38 | |
39 | return false; |
40 | } |
41 | } |