From d86fb35443627a6af8e4ab58f91288749d469275 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 16:31:53 +0200 Subject: [PATCH 1/6] Add additional fixture for test --- packages/framework/tests/Feature/DataCollectionTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/DataCollectionTest.php b/packages/framework/tests/Feature/DataCollectionTest.php index df35e788f3f..9f5479f4dee 100644 --- a/packages/framework/tests/Feature/DataCollectionTest.php +++ b/packages/framework/tests/Feature/DataCollectionTest.php @@ -34,11 +34,13 @@ 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')); } From 97513e870415064bc3004e1463bd0c277324efd8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 17:25:04 +0200 Subject: [PATCH 2/6] Support parsing Yaml data files with both dashes and without --- .../framework/src/Support/DataCollections.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Support/DataCollections.php b/packages/framework/src/Support/DataCollections.php index b5ab786edef..f683801a5ec 100644 --- a/packages/framework/src/Support/DataCollections.php +++ b/packages/framework/src/Support/DataCollections.php @@ -5,6 +5,9 @@ namespace Hyde\Support; use Hyde\Facades\Filesystem; +use Symfony\Component\Yaml\Yaml; +use Hyde\Markdown\Models\FrontMatter; +use Spatie\YamlFrontMatter\YamlFrontMatter; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Framework\Concerns\InteractsWithDirectories; use Illuminate\Support\Collection; @@ -68,7 +71,17 @@ 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, '---')) { + $document = YamlFrontMatter::markdownCompatibleParse($content); + $matter = new FrontMatter($document->matter()); + } else { + $parsed = Yaml::parse($content) ?: []; + $matter = new FrontMatter($parsed); + } + + return [static::makeIdentifier($file) => $matter]; })); } From 5639bc24af7b243298a5f891697d4b1608bacaeb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 16:48:34 +0200 Subject: [PATCH 3/6] Test YAML collections without triple dashes --- .../framework/tests/Feature/DataCollectionTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/tests/Feature/DataCollectionTest.php b/packages/framework/tests/Feature/DataCollectionTest.php index 9f5479f4dee..bea53f0ef6d 100644 --- a/packages/framework/tests/Feature/DataCollectionTest.php +++ b/packages/framework/tests/Feature/DataCollectionTest.php @@ -44,6 +44,16 @@ public function testYamlCollections() ]), 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')); + } + public function testJsonCollections() { $this->directory('resources/collections/foo'); From 9d3d224bb037ef81a9109195aa5846eb8790f082 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 17:33:49 +0200 Subject: [PATCH 4/6] Yaml data files no longer need to start with triple dashes --- docs/digging-deeper/collections.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/digging-deeper/collections.md b/docs/digging-deeper/collections.md index f0ebb7b79fb..f0224e85f23 100644 --- a/docs/digging-deeper/collections.md +++ b/docs/digging-deeper/collections.md @@ -129,14 +129,10 @@ Here is an approximation of the data types contained by the variable created abo Assuming the Yaml document looks like this: ```yaml ---- name: "John Doe" email: "john@example.org" ``` ->warning Note that the Yaml file should start with `---` to be parsed correctly. - - ## Json Collections ### Usage From fc7528568f5933a032b5ab82ed938137ffec4a75 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 17:37:17 +0200 Subject: [PATCH 5/6] Simplify Yaml data file parsing --- packages/framework/src/Support/DataCollections.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Support/DataCollections.php b/packages/framework/src/Support/DataCollections.php index f683801a5ec..ceba103e305 100644 --- a/packages/framework/src/Support/DataCollections.php +++ b/packages/framework/src/Support/DataCollections.php @@ -7,7 +7,6 @@ use Hyde\Facades\Filesystem; use Symfony\Component\Yaml\Yaml; use Hyde\Markdown\Models\FrontMatter; -use Spatie\YamlFrontMatter\YamlFrontMatter; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Framework\Concerns\InteractsWithDirectories; use Illuminate\Support\Collection; @@ -74,13 +73,12 @@ public static function yaml(string $name): static $content = Filesystem::get($file); if (str_starts_with($content, '---')) { - $document = YamlFrontMatter::markdownCompatibleParse($content); - $matter = new FrontMatter($document->matter()); - } else { - $parsed = Yaml::parse($content) ?: []; - $matter = new FrontMatter($parsed); + $content = Str::between($content, '---', '---'); } + $parsed = Yaml::parse($content) ?: []; + $matter = new FrontMatter($parsed); + return [static::makeIdentifier($file) => $matter]; })); } From f184d5864d00532fc03911802872aca947b23fea Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 23 Jun 2024 17:48:39 +0200 Subject: [PATCH 6/6] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 50f03d967ae..170550c7582 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -31,6 +31,7 @@ This serves two purposes: - Fixed explicitly set front matter navigation group behavior being dependent on subdirectory configuration, fixing https://github.com/hydephp/develop/issues/1515 in https://github.com/hydephp/develop/pull/1703 - Fixed DataCollections file finding method not being able to be overridden https://github.com/hydephp/develop/issues/1716 in https://github.com/hydephp/develop/pull/1717 - Fixed PHP warning when trying to parse a Markdown file with just front matter without body https://github.com/hydephp/develop/issues/1705 in https://github.com/hydephp/develop/pull/1728 +- Yaml data files no longer need to start with triple dashes to be parsed by DataCollections in https://github.com/hydephp/develop/pull/1733 ### Security - in case of vulnerabilities.