| Total Complexity | 6 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | class MarkdownFileParser |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The extracted Front Matter. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | public array $matter = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The extracted Markdown body. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public string $markdown = ''; |
||
| 29 | |||
| 30 | public function __construct(string $localFilepath) |
||
| 31 | { |
||
| 32 | $stream = file_get_contents(Hyde::path($localFilepath)); |
||
| 33 | |||
| 34 | // Check if the file has Front Matter. |
||
| 35 | if (str_starts_with($stream, '---')) { |
||
| 36 | $object = YamlFrontMatter::markdownCompatibleParse($stream); |
||
| 37 | |||
| 38 | if ($object->matter()) { |
||
| 39 | $this->matter = $object->matter(); |
||
| 40 | |||
| 41 | // Unset the slug from the matter, as it can cause problems if it exists. |
||
| 42 | unset($this->matter['slug']); |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($object->body()) { |
||
| 46 | $this->markdown = $object->body(); |
||
| 47 | } |
||
| 48 | } else { |
||
| 49 | $this->markdown = $stream; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the processed Markdown file as a MarkdownDocument. |
||
| 55 | */ |
||
| 56 | public function get(): MarkdownDocument |
||
| 59 | } |
||
| 60 | |||
| 61 | public static function parse(string $filepath): MarkdownDocument |
||
| 66 |