Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
BaseMarkdownPage
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 make
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 markdown
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 compile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Pages\Concerns;
6
7use Hyde\Facades\Filesystem;
8use Hyde\Markdown\Contracts\MarkdownDocumentContract;
9use Hyde\Markdown\Models\FrontMatter;
10use Hyde\Markdown\Models\Markdown;
11use Illuminate\Support\Facades\View;
12
13use function dirname;
14use function ltrim;
15use function trim;
16
17/**
18 * The base class for all Markdown-based page models.
19 *
20 * @see \Hyde\Pages\MarkdownPage
21 * @see \Hyde\Pages\MarkdownPost
22 * @see \Hyde\Pages\DocumentationPage
23 * @see \Hyde\Pages\Concerns\HydePage
24 */
25abstract class BaseMarkdownPage extends HydePage implements MarkdownDocumentContract
26{
27    public Markdown $markdown;
28
29    public static string $fileExtension = '.md';
30
31    /** @inheritDoc */
32    public static function make(string $identifier = '', FrontMatter|array $matter = [], Markdown|string $markdown = ''): static
33    {
34        return new static($identifier, $matter, $markdown);
35    }
36
37    /** @inheritDoc */
38    public function __construct(string $identifier = '', FrontMatter|array $matter = [], Markdown|string $markdown = '')
39    {
40        $this->markdown = $markdown instanceof Markdown ? $markdown : new Markdown($markdown);
41
42        parent::__construct($identifier, $matter);
43    }
44
45    /** @inheritDoc */
46    public function markdown(): Markdown
47    {
48        return $this->markdown;
49    }
50
51    /** @inheritDoc */
52    public function compile(): string
53    {
54        return View::make($this->getBladeView())->with([
55            'title' => $this->title,
56            'content' => $this->markdown->toHtml(static::class),
57        ])->render();
58    }
59
60    /**
61     * Save the Markdown page object to disk by compiling the
62     * front matter array to YAML and writing the body to the file.
63     *
64     * @return $this
65     */
66    public function save(): static
67    {
68        Filesystem::ensureDirectoryExists(dirname($this->getSourcePath()));
69
70        Filesystem::putContents($this->getSourcePath(), ltrim(trim("$this->matter\n$this->markdown")."\n"));
71
72        return $this;
73    }
74}