diff --git a/src/MarkdownPageParser.php b/src/MarkdownPageParser.php index 8d7f1f23..d5cff71e 100644 --- a/src/MarkdownPageParser.php +++ b/src/MarkdownPageParser.php @@ -4,6 +4,7 @@ use Hyde\Framework\Services\MarkdownFileService; use Hyde\Framework\Models\MarkdownPage; +use Illuminate\Support\Str; use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\Pure; use Exception; @@ -53,7 +54,8 @@ public function execute(): void if (isset($document->matter['title'])) { $this->title = $document->matter['title']; } else { - $this->title = $this->findTitleTag($document->body) ?? Str::title(str_replace('-', ' ', $this->slug)); + $this->title = $this->findTitleTag($document->body) ?? + Str::title(str_replace('-', ' ', $this->slug)); } $this->body = $document->body; diff --git a/src/MarkdownPostParser.php b/src/MarkdownPostParser.php index a998c3fb..73685628 100644 --- a/src/MarkdownPostParser.php +++ b/src/MarkdownPostParser.php @@ -2,8 +2,8 @@ namespace Hyde\Framework; +use Hyde\Framework\Services\MarkdownFileService; use Hyde\Framework\Models\MarkdownPost; -use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\Pure; use Exception; @@ -15,11 +15,6 @@ */ class MarkdownPostParser { - /** - * @var string the full path to the Markdown file - */ - private string $filepath; - /** * The extracted Front Matter * @var array @@ -39,8 +34,7 @@ class MarkdownPostParser */ public function __construct(protected string $slug) { - $this->filepath = Hyde::path("_posts/$slug.md"); - if (!file_exists($this->filepath)) { + if (!file_exists(Hyde::path("_posts/$slug.md"))) { throw new Exception("File _posts/$slug.md not found.", 404); } @@ -55,103 +49,13 @@ public function __construct(protected string $slug) public function execute(): void { // Get the text stream from the markdown file - $stream = file_get_contents($this->filepath); - - // Split out the front matter and markdown - $split = $this->split($stream); + $document = (new MarkdownFileService(Hyde::path("_posts/$this->slug.md")))->get(); - $this->matter = array_merge($this->parseFrontMatter($split['matter']), [ + $this->matter = array_merge($document->matter, [ 'slug' => $this->slug // Make sure to use the filename as the slug and not any potential override ]); - // Implode the line array back into a markdown string - $this->body = implode("\n", $split['markdown']); - } - - /** - * Split the front matter from the markdown. - * @param string $stream - * @return array - */ - #[ArrayShape(['matter' => "array", 'markdown' => "array"])] - public function split(string $stream): array - { - $lines = explode("\n", $stream); - - // Find the start and end position of the YAML block. - // Note that unless something is wrong with the file the start index should always be 0. - $matterSectionIndex = []; - foreach ($lines as $lineNumber => $lineContents) { - if (str_starts_with($lineContents, '---')) { - if (sizeof($matterSectionIndex) === 0) { - $matterSectionIndex['start'] = $lineNumber; - } elseif (sizeof($matterSectionIndex) === 1) { - $matterSectionIndex['end'] = $lineNumber; - break; - } - } - } - - // Construct the new line arrays - $matter = []; - $markdown = []; - foreach ($lines as $lineNumber => $lineContents) { - if ($lineNumber <= $matterSectionIndex['end']) { - $matter[] = $lineContents; - } else { - $markdown[] = $lineContents; - } - } - - // Remove the dashes - unset($matter[$matterSectionIndex['start']]); - unset($matter[$matterSectionIndex['end']]); - - return [ - 'matter' => $matter, - 'markdown' => $markdown, - ]; - } - - /** - * Parse lines of Front Matter YAML into an associative array. - * - * @deprecated 0.6.0 - Please use the MarkdownFileService for preprocessing - * - * @param array $lines - * @return array - */ - public function parseFrontMatter(array $lines): array - { - $matter = []; - foreach ($lines as $line) { - if (!str_contains($line, ':')) { - continue; // The front matter is invalid, so we skip the line. - } - - // Separate the key from the value - $array = (explode(': ', $line, 2)); - - // Assign the split values into variables, so it's easier to keep track of them. - $key = $array[0]; - $value = $array[1]; - - // Filter the value to ensure a predictable state - - // Remove quotes while allowing quotes within the actual text - if (str_starts_with($value, '"') && str_ends_with($value, '"')) { - $value = substr($value, 1); - $value = substr($value, 0, -1); - } - - // Trim trailing whitespace - $value = trim($value, ' '); - // Trim trailing return character - $value = trim($value, "\r"); - - $matter[$key] = $value; - } - return $matter; + $this->body = $document->body; } /**