Skip to content

Commit

Permalink
Migrate to spatie/yaml-front-matter for YAML parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelambert committed Oct 25, 2023
1 parent c611108 commit 69406d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "rareloop/primer-core",
"require": {
"php": ">=7.4",
"php": ">=8.0",
"symfony/finder": "^4.2.5|^5.0",
"twig/twig": "^2.6",
"illuminate/collections": "^8.53.1||^9.0.0",
"league/commonmark": "^1.5",
"mnapoli/front-yaml": "^1.6"
"spatie/yaml-front-matter": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
18 changes: 7 additions & 11 deletions src/DocumentParsers/YAMLDocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

namespace Rareloop\Primer\DocumentParsers;

use Mni\FrontYAML\Parser;
use Rareloop\Primer\Contracts\DocumentParser;
use Rareloop\Primer\Document;
use Spatie\YamlFrontMatter\YamlFrontMatter;

class YAMLDocumentParser implements DocumentParser
{
public function parse(Document $document) : Document
public function parse(Document $document): Document
{
$parser = new Parser;
$parsedDocument = $parser->parse($document->content(), false);
$yaml = $parsedDocument->getYAML();
$parsedDocument = YamlFrontMatter::parse($document->content());

$newDoc = new Document($document->id(), $parsedDocument->getContent());
$newDoc->setMeta($yaml ?? []);
$newDoc = new Document($document->id(), $parsedDocument->body());
$newDoc->setMeta($parsedDocument->matter() ?? []);

if (!empty($yaml['title'])) {
$newDoc->setTitle($yaml['title']);
if (!empty($parsedDocument->matter('title'))) {
$newDoc->setTitle($parsedDocument->matter('title'));
}

$newDoc->setDescription($yaml['description'] ?? '');

$newDoc->setDescription($parsedDocument->matter('description') ?? '');
return $newDoc;
}
}
1 change: 1 addition & 0 deletions src/FileSystemPatternProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FileSystemPatternProvider implements PatternProvider, TemplateProvider
{
protected $paths;
protected $fileExtension;
protected ?DataParser $dataParser;

protected $patternPaths = [];

Expand Down

0 comments on commit 69406d4

Please sign in to comment.