From 02dc00808606943010aed40f2ed4d91d7d082d68 Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 25 Jun 2024 18:13:44 +0000 Subject: [PATCH] Merge pull request #1733 from hydephp/datacollections-improvements [1.x] DataCollections improvements https://github.com/hydephp/develop/commit/3c7939bdba2651a4c08cb9d886626691c1bd2e5f --- src/Support/DataCollections.php | 13 ++++++++++++- tests/Feature/DataCollectionTest.php | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Support/DataCollections.php b/src/Support/DataCollections.php index b5ab786e..ceba103e 100644 --- a/src/Support/DataCollections.php +++ b/src/Support/DataCollections.php @@ -5,6 +5,8 @@ namespace Hyde\Support; use Hyde\Facades\Filesystem; +use Symfony\Component\Yaml\Yaml; +use Hyde\Markdown\Models\FrontMatter; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Framework\Concerns\InteractsWithDirectories; use Illuminate\Support\Collection; @@ -68,7 +70,16 @@ public static function yaml(string $name): static static::needsDirectory(static::$sourceDirectory); return new static(static::findFiles($name, ['yaml', 'yml'])->mapWithKeys(function (string $file): array { - return [static::makeIdentifier($file) => MarkdownFileParser::parse($file)->matter()]; + $content = Filesystem::get($file); + + if (str_starts_with($content, '---')) { + $content = Str::between($content, '---', '---'); + } + + $parsed = Yaml::parse($content) ?: []; + $matter = new FrontMatter($parsed); + + return [static::makeIdentifier($file) => $matter]; })); } diff --git a/tests/Feature/DataCollectionTest.php b/tests/Feature/DataCollectionTest.php index df35e788..bea53f0e 100644 --- a/tests/Feature/DataCollectionTest.php +++ b/tests/Feature/DataCollectionTest.php @@ -34,11 +34,23 @@ public function testYamlCollections() { $this->directory('resources/collections/foo'); $this->markdown('resources/collections/foo/foo.yaml', matter: ['title' => 'Foo']); - $this->file('resources/collections/foo/bar.yml'); + $this->file('resources/collections/foo/bar.yml', "---\ntitle: Bar\n---"); + $this->file('resources/collections/foo/baz.yml'); $this->assertEquals(new DataCollections([ 'foo/foo.yaml' => new FrontMatter(['title' => 'Foo']), - 'foo/bar.yml' => new FrontMatter([]), + 'foo/bar.yml' => new FrontMatter(['title' => 'Bar']), + 'foo/baz.yml' => new FrontMatter([]), + ]), DataCollections::yaml('foo')); + } + + public function testYamlCollectionsWithoutTripleDashes() + { + $this->directory('resources/collections/foo'); + $this->file('resources/collections/foo/foo.yml', 'title: Foo'); + + $this->assertEquals(new DataCollections([ + 'foo/foo.yml' => new FrontMatter(['title' => 'Foo']), ]), DataCollections::yaml('foo')); }