Skip to content

Commit

Permalink
Merge pull request #1733 from hydephp/datacollections-improvements
Browse files Browse the repository at this point in the history
[1.x] DataCollections improvements
  • Loading branch information
caendesilva authored Jun 25, 2024
2 parents 52ab0a1 + 2ad9e59 commit 3c7939b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
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

0 comments on commit 3c7939b

Please sign in to comment.