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