1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Contracts; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\SourceFileParser; |
6
|
|
|
use Hyde\Framework\Models\FrontMatter; |
7
|
|
|
use Hyde\Framework\Models\Markdown; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The base class for all Markdown-based Page Models. |
11
|
|
|
* |
12
|
|
|
* Normally, you would use the SourceFileParser to construct a MarkdownPage object. |
13
|
|
|
* |
14
|
|
|
* Extends the AbstractPage class to provide relevant |
15
|
|
|
* helpers for Markdown-based page model classes. |
16
|
|
|
* |
17
|
|
|
* @see \Hyde\Framework\Models\Pages\MarkdownPage |
18
|
|
|
* @see \Hyde\Framework\Models\Pages\MarkdownPost |
19
|
|
|
* @see \Hyde\Framework\Models\Pages\DocumentationPage |
20
|
|
|
* @see \Hyde\Framework\Contracts\AbstractPage |
21
|
|
|
* @see \Hyde\Framework\Testing\Feature\AbstractPageTest |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownDocumentContract, MarkdownPageContract |
24
|
|
|
{ |
25
|
|
|
public Markdown $markdown; |
26
|
|
|
|
27
|
|
|
public FrontMatter $matter; |
28
|
|
|
|
29
|
|
|
/** @deprecated */ |
30
|
|
|
public string $body; |
31
|
|
|
public string $title; |
32
|
|
|
public string $identifier; |
33
|
|
|
|
34
|
|
|
public static string $fileExtension = '.md'; |
35
|
|
|
|
36
|
|
|
/** @interitDoc */ |
37
|
|
|
public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null) |
38
|
|
|
{ |
39
|
|
|
$this->identifier = $identifier; |
40
|
|
|
$this->matter = $matter ?? new FrontMatter(); |
41
|
|
|
$this->markdown = $markdown ?? new Markdown(); |
42
|
|
|
|
43
|
|
|
$this->body = $this->markdown->body; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** Alternative to constructor, using primitive data types */ |
47
|
|
|
public static function make(string $identifier = '', array $matter = [], string $body = ''): static |
48
|
|
|
{ |
49
|
|
|
return tap(new static($identifier, new FrontMatter($matter), new Markdown($body)), function (self $page) { |
50
|
|
|
$page->title = SourceFileParser::findTitleForPage($page, $page->identifier); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function markdown(): Markdown |
55
|
|
|
{ |
56
|
|
|
return $this->markdown; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function matter(string $key = null, mixed $default = null): mixed |
60
|
|
|
{ |
61
|
|
|
return $this->matter->get($key, $default); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @inheritDoc */ |
65
|
|
|
public function compile(): string |
66
|
|
|
{ |
67
|
|
|
return view($this->getBladeView())->with([ |
68
|
|
|
'title' => $this->title, |
69
|
|
|
'markdown' => $this->markdown->compile(static::class), |
70
|
|
|
])->render(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|