Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| Markdown | |
100.00% |
11 / 11 |
|
100.00% |
8 / 8 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| body | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | class Markdown implements Arrayable, Stringable, Htmlable |
| 18 | { |
| 19 | public string $body; |
| 20 | |
| 21 | /** |
| 22 | * Create a new Markdown object from a string. |
| 23 | */ |
| 24 | public function __construct(string $body = '') |
| 25 | { |
| 26 | $this->body = str_replace("\r\n", "\n", rtrim($body)); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the source Markdown body. |
| 31 | */ |
| 32 | public function __toString(): string |
| 33 | { |
| 34 | return $this->body; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get the source Markdown body. |
| 39 | */ |
| 40 | public function body(): string |
| 41 | { |
| 42 | return $this->body; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Compile the Markdown body to a string of HTML. |
| 47 | * |
| 48 | * If the Markdown being compiled is from a page model, supply |
| 49 | * model's class name here so the dynamic parser can be used. |
| 50 | * |
| 51 | * @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass |
| 52 | */ |
| 53 | public function compile(?string $pageClass = null): string |
| 54 | { |
| 55 | return static::render($this->body, $pageClass); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Same as Markdown::compile(), but returns an HtmlString object. |
| 60 | */ |
| 61 | public function toHtml(?string $pageClass = null): HtmlString |
| 62 | { |
| 63 | return new HtmlString($this->compile($pageClass)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the Markdown document body as an array of lines. |
| 68 | * |
| 69 | * @return string[] |
| 70 | */ |
| 71 | public function toArray(): array |
| 72 | { |
| 73 | return explode("\n", $this->body); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Parse a Markdown file into a new Markdown object. |
| 78 | */ |
| 79 | public static function fromFile(string $path): static |
| 80 | { |
| 81 | return MarkdownDocument::parse($path)->markdown(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Render a Markdown string into HTML. |
| 86 | * |
| 87 | * If a source model is provided, the Markdown will be converted using the dynamic MarkdownService, |
| 88 | * otherwise, the pre-configured singleton from the service container will be used instead. |
| 89 | * |
| 90 | * @return string $html |
| 91 | */ |
| 92 | public static function render(string $markdown, ?string $pageClass = null): string |
| 93 | { |
| 94 | if ($pageClass !== null) { |
| 95 | return (new MarkdownService($markdown, $pageClass))->parse(); |
| 96 | } else { |
| 97 | /** @var MarkdownConverter $converter */ |
| 98 | $converter = app(MarkdownConverter::class); |
| 99 | |
| 100 | return (string) $converter->convert($markdown); |
| 101 | } |
| 102 | } |
| 103 | } |