Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] DataCollections improvements #1733

Merged
merged 7 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions docs/digging-deeper/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"
```
>warning Note that the Yaml file should start with `---` to be parsed correctly.


## Json Collections
### Usage
Expand Down
13 changes: 12 additions & 1 deletion packages/framework/src/Support/DataCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}));
}

Expand Down
16 changes: 14 additions & 2 deletions packages/framework/tests/Feature/DataCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
Loading