Skip to content

Commit

Permalink
Refactor to use the MarkdownFileService
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 27, 2022
1 parent e32502e commit 48a27a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 102 deletions.
4 changes: 3 additions & 1 deletion src/MarkdownPageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
106 changes: 5 additions & 101 deletions src/MarkdownPostParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,11 +15,6 @@
*/
class MarkdownPostParser
{
/**
* @var string the full path to the Markdown file
*/
private string $filepath;

/**
* The extracted Front Matter
* @var array
Expand All @@ -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);
}

Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 48a27a2

Please sign in to comment.