1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Markdown\Models; |
6
|
|
|
|
7
|
|
|
use Hyde\Framework\Services\MarkdownService; |
8
|
|
|
use Hyde\Markdown\MarkdownConverter; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
11
|
|
|
use Illuminate\Support\HtmlString; |
12
|
|
|
use Stringable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A simple object representation of a Markdown file, with helpful methods to interact with it. |
16
|
|
|
* |
17
|
|
|
* @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest |
18
|
|
|
*/ |
19
|
|
|
class Markdown implements Arrayable, Stringable, Htmlable |
20
|
|
|
{ |
21
|
|
|
public string $body; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new Markdown object from a string. |
25
|
|
|
*/ |
26
|
|
|
public function __construct(string $body = '') |
27
|
|
|
{ |
28
|
|
|
$this->body = $body; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the source Markdown body. |
33
|
|
|
*/ |
34
|
|
|
public function __toString(): string |
35
|
|
|
{ |
36
|
|
|
return $this->body; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the source Markdown body. |
41
|
|
|
*/ |
42
|
|
|
public function body(): string |
43
|
|
|
{ |
44
|
|
|
return $this->body; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Compile the Markdown body to a string of HTML. |
49
|
|
|
* |
50
|
|
|
* If the Markdown being compiled is from a page model, supply |
51
|
|
|
* model's class name here so the dynamic parser can be used. |
52
|
|
|
* |
53
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $sourceModel |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function compile(?string $sourceModel = null): string |
56
|
|
|
{ |
57
|
|
|
return static::render($this->body, $sourceModel); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Same as Markdown::compile(), but returns an HtmlString object. |
62
|
|
|
*/ |
63
|
|
|
public function toHtml(?string $sourceModel = null): HtmlString |
64
|
|
|
{ |
65
|
|
|
return new HtmlString($this->compile($sourceModel)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the Markdown document body as an array of lines. |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
public function toArray(): array |
74
|
|
|
{ |
75
|
|
|
return explode("\n", $this->body); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parse a Markdown file into a new Markdown object. |
80
|
|
|
*/ |
81
|
|
|
public static function fromFile(string $localFilepath): static |
82
|
|
|
{ |
83
|
|
|
return MarkdownDocument::parse($localFilepath)->markdown(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Render a Markdown string into HTML. |
88
|
|
|
* |
89
|
|
|
* If a source model is provided, the Markdown will be converted using the dynamic MarkdownService, |
90
|
|
|
* otherwise, the pre-configured singleton from the service container will be used instead. |
91
|
|
|
* |
92
|
|
|
* @return string $html |
93
|
|
|
*/ |
94
|
|
|
public static function render(string $markdown, ?string $sourceModel = null): string |
95
|
|
|
{ |
96
|
|
|
return $sourceModel !== null |
97
|
|
|
? (new MarkdownService($markdown, $sourceModel))->parse() |
98
|
|
|
: (string) app(MarkdownConverter::class)->convert($markdown); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|