Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
48beca6
Add missing array keys to mocked config values
emmadesilva Jul 7, 2024
b2dcd7a
Clarify test name and expect the described result
emmadesilva Jul 7, 2024
f43a3af
Remove readonly modifier from author username
emmadesilva Jul 7, 2024
26be37d
Update parser to tap author to set the username to the array key
emmadesilva Jul 7, 2024
fb12b6b
Throw an InvalidArgumentException when missing the array key
emmadesilva Jul 7, 2024
fbfa507
Deprecate the `Author::create` method's `$username` parameter
emmadesilva Jul 7, 2024
a1b9cfe
Update the `Author::create` method to support all parameters
emmadesilva Jul 7, 2024
f5a66ee
Create InvalidConfigurationException.php
emmadesilva Jul 7, 2024
26aeea7
Update InvalidConfigurationException to extend InvalidArgumentException
emmadesilva Jul 7, 2024
1bdda78
Throw custom InvalidConfigurationException
emmadesilva Jul 7, 2024
fb029e1
Override constructor
emmadesilva Jul 7, 2024
771d84d
Create initial logic to find the errored config line
emmadesilva Jul 7, 2024
e7748bc
Replace break with early return
emmadesilva Jul 7, 2024
5f414e5
Add spacing
emmadesilva Jul 7, 2024
e57592a
Annotate return types
emmadesilva Jul 7, 2024
d343f0a
Remove graceful handling as internal developer needs to use proper paths
emmadesilva Jul 7, 2024
901b058
Cleanup and improve the line finder code
emmadesilva Jul 7, 2024
56306f7
Inline local variables
emmadesilva Jul 7, 2024
ba2a352
Mark helper as experimental
emmadesilva Jul 7, 2024
46d0bd2
Clarify trait scope
emmadesilva Jul 7, 2024
4b369ec
Specify the exception location
emmadesilva Jul 7, 2024
ff935aa
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 7, 2024
34d6d0b
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 7, 2024
49f3be1
Add missing generics closing tag
emmadesilva Jul 7, 2024
89a3eb7
Make the socials array nullable to match other properties in class
emmadesilva Jul 7, 2024
239e67c
Document the deprecation reason
emmadesilva Jul 7, 2024
1564619
Make deprecated parameter optional
emmadesilva Jul 7, 2024
93b812a
Document remaining constructor properties
emmadesilva Jul 7, 2024
9c7c4f4
Add name identifiers to the author fields in the config example
emmadesilva Jul 7, 2024
4500a5f
Fill in all the author fields in the config example
emmadesilva Jul 7, 2024
b0d9115
Comment out extra author fields
emmadesilva Jul 7, 2024
a6263d1
Improve the author configuration documentation
emmadesilva Jul 7, 2024
960989c
Improve inline author config documentation
emmadesilva Jul 7, 2024
c780fc8
Return new static
emmadesilva Jul 7, 2024
8030c18
Test against new post author
emmadesilva Jul 7, 2024
4ade214
Annotate the closure return
emmadesilva Jul 7, 2024
dd08262
Remove unused use statement
emmadesilva Jul 7, 2024
5639b1b
Set default blank username to guest
emmadesilva Jul 7, 2024
e9731c9
Test can define author with no data in config
emmadesilva Jul 7, 2024
ac9f5f7
Capitalize the username when making the name fallback
emmadesilva Jul 7, 2024
94588e4
Revert "Capitalize the username when making the name fallback"
emmadesilva Jul 7, 2024
aa81aba
Revert "Set default blank username to guest"
emmadesilva Jul 7, 2024
8ea1b91
Update PostAuthorTest.php
emmadesilva Jul 7, 2024
b9235dc
Create a new and improved Author facade system
emmadesilva Jul 7, 2024
b8dc3a4
Update Author getters to return null when username is not found
emmadesilva Jul 7, 2024
fabf404
Simplify get or create method logic
emmadesilva Jul 7, 2024
05c5254
Revert "Simplify get or create method logic"
emmadesilva Jul 7, 2024
66212b8
Reapply "Simplify get or create method logic"
emmadesilva Jul 7, 2024
a892c1b
Move assignment out of the condition
emmadesilva Jul 7, 2024
dca9991
Remove unnecessary parentheses
emmadesilva Jul 7, 2024
65c7dec
New ternary logic
emmadesilva Jul 7, 2024
43dda0b
Revert "New ternary logic"
emmadesilva Jul 7, 2024
7872a23
Revert "Remove unnecessary parentheses"
emmadesilva Jul 7, 2024
5eb61be
Revert "Move assignment out of the condition"
emmadesilva Jul 7, 2024
e41853c
Revert "Reapply "Simplify get or create method logic""
emmadesilva Jul 7, 2024
53f61cf
Fall back to method retry with username
emmadesilva Jul 7, 2024
d9e7a8b
Revert "Fall back to method retry with username"
emmadesilva Jul 7, 2024
473a625
Reapply "Fall back to method retry with username"
emmadesilva Jul 7, 2024
57ab544
Clarify test method names
emmadesilva Jul 7, 2024
9d689b4
Extract new more scoped create method
emmadesilva Jul 7, 2024
1e9f09d
Deprecate the `PostAuthor::getOrCreate` method
emmadesilva Jul 7, 2024
bf10755
Use the create method as there should be no models before parsing
emmadesilva Jul 7, 2024
b673d6b
Add todo
emmadesilva Jul 7, 2024
a2d15ee
Add todo
emmadesilva Jul 7, 2024
cf4831d
Extract helper method
emmadesilva Jul 7, 2024
fdc4d83
Introduce local variable
emmadesilva Jul 7, 2024
ceec673
Inline the helper method usage into last remaining usage
emmadesilva Jul 7, 2024
fa4213f
Ignore coverage for deprecated method
emmadesilva Jul 7, 2024
672cfb5
Use static instead of self
emmadesilva Jul 7, 2024
9fa15e6
Update documentation for replacement method
emmadesilva Jul 7, 2024
bac4eb2
Remove the `PostAuthor::getOrCreate` method
emmadesilva Jul 7, 2024
557168f
Normalize documentation formatting
emmadesilva Jul 7, 2024
c527046
Document that username will never be null
emmadesilva Jul 7, 2024
2f16657
Document where the key comes from
emmadesilva Jul 7, 2024
7290ccd
Document string to string map type
emmadesilva Jul 7, 2024
f86d01c
Update fallback username to be lowercase
emmadesilva Jul 7, 2024
b8bad25
Normalize post author usernames to lowercase
emmadesilva Jul 7, 2024
33a75b2
Normalize post author usernames to snake case
emmadesilva Jul 7, 2024
1fd4ef2
Extract helper method
emmadesilva Jul 7, 2024
b41c4a6
Test the username is normalized
emmadesilva Jul 7, 2024
c5c7611
Remove fixture with no bearing on test
emmadesilva Jul 7, 2024
f7fac01
Generate the name as a proper title
emmadesilva Jul 7, 2024
88303fc
Normalize name before making it into a title
emmadesilva Jul 7, 2024
27225c0
Revert "Normalize name before making it into a title"
emmadesilva Jul 7, 2024
6c30e0a
Use more complex title parsing
emmadesilva Jul 7, 2024
a0c832e
Initial Yaml config error rethrow proof of concept
emmadesilva Jul 7, 2024
4c7cd81
Revert "Initial Yaml config error rethrow proof of concept"
emmadesilva Jul 7, 2024
9bb7853
Rethrow type errors as configuration exceptions
emmadesilva Jul 8, 2024
2ad5de2
Update custom exception class to accept previous throwable
emmadesilva Jul 8, 2024
106b4c1
Custom exception testing as default breaks file memory
emmadesilva Jul 8, 2024
41e0d19
Fix wrong test names
emmadesilva Jul 8, 2024
58711e6
Improve formatting
emmadesilva Jul 8, 2024
c4f0cd6
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 8, 2024
1279d87
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 8, 2024
2643303
Merge branch 'master' into 2.x-dev
emmadesilva Jul 8, 2024
91e300f
Merge branch 'master' into 2.x-dev
emmadesilva Jul 8, 2024
86e3f47
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 8, 2024
88fd588
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 8, 2024
faac499
Merge branch 'master' of github.com:hydephp/develop into 2.x-dev
emmadesilva Jul 8, 2024
7ad322d
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 8, 2024
41f5a91
Normalize the usernames when retrieving them
emmadesilva Jul 8, 2024
fc9bd9e
Move up test
emmadesilva Jul 8, 2024
9cdbed3
Make protected normalization helper public internal
emmadesilva Jul 8, 2024
d9ceeef
Normalize the usernames when parsing the author config
emmadesilva Jul 8, 2024
196ceb7
Test covers HasKernelData trait
emmadesilva Jul 8, 2024
a035415
Add a filter pass to only keep the first author with matching username
emmadesilva Jul 8, 2024
e91e646
Revert "Add a filter pass to only keep the first author"
emmadesilva Jul 8, 2024
838fa42
Remove todo
emmadesilva Jul 8, 2024
86f9025
Write new release notes for the new author system
emmadesilva Jul 8, 2024
7d9db6a
Merge branch 'master' into 2.x-dev
emmadesilva Jul 9, 2024
1136e0a
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 9, 2024
f25e442
Document all breaking author changes in changelist
emmadesilva Jul 9, 2024
0db6918
Merge branch '2.x-dev' into reduce-author-username-duplication
emmadesilva Jul 9, 2024
50de689
Test all post author fields can be set in front matter
emmadesilva Jul 9, 2024
6c3f56d
Update customization documentation for new author system
emmadesilva Jul 9, 2024
c788172
Improve wording
emmadesilva Jul 9, 2024
9510e21
Link to related docs
emmadesilva Jul 9, 2024
33d786b
Test configured post author fields can be customized in front matter
emmadesilva Jul 9, 2024
b1bb99a
Fix documented discrepancy against actuality as changes are not merged
emmadesilva Jul 9, 2024
b8a2b10
Improve documentation section wording
emmadesilva Jul 9, 2024
5fda706
Document the optional fields better
emmadesilva Jul 9, 2024
a5fe0e0
Document fields and notes
emmadesilva Jul 9, 2024
60b6831
Link to the source code
emmadesilva Jul 9, 2024
931f429
Link to the facade
emmadesilva Jul 9, 2024
d1927e5
Update blog post documentation for new author system
emmadesilva Jul 9, 2024
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
54 changes: 43 additions & 11 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This serves two purposes:
- **Breaking:** The `hyde.features` configuration format has changed to use Enums instead of static method calls. For more information, see below.
- **Breaking:** Renamed class `DataCollections` to `DataCollection`. For more information, see below.
- **Breaking:** The `hyde.authors` config setting should now be keyed by the usernames. For more information, see below.
- **Breaking:** The `Author::create()` method now returns an array instead of a `PostAuthor` instance. For more information, see below.
- **Breaking:** The `Author::get()` method now returns `null` if an author is not found, rather than creating a new instance. For more information, see below.
- Medium: The `route` function will now throw a `RouteNotFoundException` if the route does not exist in https://github.com/hydephp/develop/pull/1741
- Minor: Navigation menu items are now no longer filtered by duplicates (meaning two items with the same label can now exist in the same menu) in https://github.com/hydephp/develop/pull/1573
- Minor: Due to changes in the navigation system, it is possible that existing configuration files will need to be adjusted in order for menus to look the same (in terms of ordering etc.)
Expand Down Expand Up @@ -152,20 +154,50 @@ Of course, if you have disabled any of the features, do not include them in the

### Post Author changes

This release makes major improvements into the usability and design of the blog post author feature.
This release makes major improvements to the usability and design of the blog post author feature.

Here is the full list of changes:

- Breaking: The `hyde.authors` config setting should now be keyed by the usernames
- Removed: The deprecated `PostAuthor::getName()` method is now removed (use `$author->name`)
- Feature: We now support setting authors in the YAML configuration! Fixes `#1719`
- Feature: Added a `$author->getPosts()` method to get all author's posts
- Feature: Authors now can also have custom biographies and social media links
- The PostAuthor class is now Arrayable and JsonSerializable
- The collection of site authors are now stored in the HydeKernel
- Authors can additionally be accessed through `Hyde::authors()`

For more information, see https://github.com/hydephp/develop/pull/1782
- Breaking: The `hyde.authors` config setting must now be keyed by the usernames, instead of providing the username in the author facade constructor.
- Breaking: The `Author::create()` method now returns an array instead of a `PostAuthor` instance. This only affects custom code that uses the `Author` facade.
- Breaking: The `Author::get()` method now returns `null` if an author is not found, rather than creating a new instance. This only affects custom code that uses the `Author` facade.
- Removed: The deprecated `PostAuthor::getName()` method has been removed (use `$author->name` instead).
- Changed: Author usernames are now automatically normalized (converted to lowercase and spaces replaced with underscores in order to ensure URL routability).
- Changed: If an author display name is not provided, it is now intelligently generated from the username.
- Feature: Authors can now be set in the YAML configuration.
- Feature: Added a `$author->getPosts()` method to get all of an author's posts.
- Feature: Authors now support custom biographies, avatars, and social media links. Note that these are not currently used in any of the default templates, but you can use them in your custom views.
- The collection of site authors is now stored in the HydeKernel, meaning authors can be accessed through `Hyde::authors()`.
- The `PostAuthor` class is now Arrayable and JsonSerializable.

#### Upgrade guide:

1. Update your `config/hyde.php` file to use the new author configuration format:
```php
'authors' => [
'username' => Author::create(
name: 'Display Name',
website: 'https://example.com',
bio: 'Author bio',
avatar: 'avatar.png',
socials: ['twitter' => '@username']
),
],
```

2. Review and update any code that uses the `Author` facade:
- The `create()` method now returns an array instead of a `PostAuthor` instance.
- The `get()` method may return `null`, so handle this case in your code.

3. Check your blog post front matter and ensure that `author` fields match the new username keys in your configuration.

4. If you have custom templates that use author data, update them to:
- Optional: Feel free to use the new available fields: `bio`, `avatar`, and `socials`.
- Account for usernames now being lowercase with underscores which may lead to changed HTML or URL paths.

5. If you were relying on `Author::get()` to create new authors on the fly, update your code to handle `null` returns or create authors explicitly.

For more information, see https://github.com/hydephp/develop/pull/1782 and https://github.com/hydephp/develop/pull/1798

### Documentation search page changes

Expand Down
21 changes: 15 additions & 6 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,26 @@
| However, it's tedious to have to add those to each and every
| post you make, and keeping them updated is even harder.
|
| Here you can add predefined authors. When writing posts,
| just specify the username in the front matter, and the
| rest of the data will be pulled from a matching entry.
| To solve this problem, you can add predefined authors with this setting.
| When writing posts just specify the author's username (the array key).
| Hyde will pull the matching data from here and fill in the blanks.
|
*/

'authors' => [
'mr_hyde' => Author::create(
'mr_hyde', // Required username
'Mr. Hyde', // Optional display name
'https://hydephp.com' // Optional website URL
// The following settings are used in the default blog post template.
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com', // Optional website URL

// The following settings are not used in the bundled templates,
// but you can use them in your own custom views, for example.
// bio: 'The mysterious author of HydePHP',
// avatar: 'avatar.png',
// socials: [
// 'twitter' => 'HydeFramework',
// 'github' => 'hydephp',
// ],
Comment on lines +291 to +299
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we do not need this here, as they are not yet used, and they are in the documentation. However, if https://github.com/hydephp/develop/issues/1762 will use these they can be kept, hence why I'm holding off on removing them.

),
],

Expand Down
14 changes: 12 additions & 2 deletions docs/creating-content/blog-posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,23 @@ author: mr_hyde

```yaml
author:
# These are used in the default author templates
name: "Mr. Hyde"
username: mr_hyde
website: https://twitter.com/HydeFramework

# These are not used in the default author templates, but can be used in your custom views
bio: "The mysterious author of HydePHP"
avatar: avatar.png
socials:
twitter: "@HydeFramework"
github: "hydephp"
```

When specifying an array you don't need all the sub-properties. The example just shows all the supported values.
Array values here will override all the values in the `authors` config entry, if one exists.

When using an array, you don't need to include all properties. Specified values will override the corresponding entries in the `authors` config.

Note: Author usernames are automatically normalized (converted to lowercase with spaces replaced by underscores).

### Image

Expand Down
51 changes: 43 additions & 8 deletions docs/digging-deeper/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,32 @@ Here are the default settings:

### Authors

Hyde has support for adding authors in front matter, for example to automatically add a link to your website or social media profiles.
However, it's tedious to have to add those to each and every post you make, and keeping them updated is even harder.
Hyde supports adding authors to blog posts, allowing you to automatically include information like display names and website links.
We even support fields for avatars, biographies, and social media profiles, which you can use in your custom Blade templates.

While you can set all this data directly in the [front matter](blog-posts#author), that quickly becomes tedious and hard to maintain.
Instead, you can predefine authors in the Hyde config. When writing posts, just specify the username in the front matter,
and the rest of the data will be pulled from a matching entry found in the configuration file.

#### Example
#### Configuration

Authors are defined in the `config/hyde.php` file under the `authors` key. Each author is keyed by their username and is configured using the `Author::create()` method:

```php
// filepath: config/hyde.php
'authors' => [
Author::create(
username: 'mr_hyde', // Required username
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com' // Optional website URL
'mr_hyde' => Author::create(
// The following fields, along with the username, are used by the default blog post templates.
name: 'Mr. Hyde',
website: 'https://hydephp.com',

// These fields are not currently used in the default templates, but you can use them in your custom views.
bio: 'The mysterious author of HydePHP',
avatar: 'avatar.png',
socials: [
'twitter' => '@HydeFramework',
'github' => 'hydephp',
],
),
],
```
Expand All @@ -150,14 +161,38 @@ author:
username: mr_hyde
name: Mr. Hyde
website: https://hydephp.com
bio: The mysterious author of HydePHP
avatar: avatar.png
socials:
twitter: "@HydeFramework"
github: hydephp
```

But you only have to specify the username:
But you only have to specify the username to get all the other data.

```yaml
author: mr_hyde
```

If you want to override the data for a specific post, you can do so in the [front matter](blog-posts#author) which is great for guest authors or one-off posts.


#### Available Fields

- `name`: The author's display name (optional, generated from username if not provided)
- `website`: The author's website URL (optional)
- `bio`: A short biography (optional, not used in default templates)
- `avatar`: Path to the author's avatar image (optional, not used in default templates)
- `socials`: An array of social media links (optional, not used in default templates)

#### Notes

- Usernames are automatically normalized (converted to lowercase with spaces replaced by underscores)
- The `PostAuthor` class includes a `getPosts()` method to retrieve all posts by an author
- Authors can be accessed through `Hyde::authors()`

For more advanced usage and customization, refer to the [source code](https://github.com/hydephp/framework/blob/master/src/Framework/Features/Blogging/Models/PostAuthor.php) which is well documented.

### Footer

Most websites have a footer with copyright details and contact information. You probably want to change the Markdown to include your information, though you are of course welcome to keep the default attribution link!
Expand Down
21 changes: 15 additions & 6 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,26 @@
| However, it's tedious to have to add those to each and every
| post you make, and keeping them updated is even harder.
|
| Here you can add predefined authors. When writing posts,
| just specify the username in the front matter, and the
| rest of the data will be pulled from a matching entry.
| To solve this problem, you can add predefined authors with this setting.
| When writing posts just specify the author's username (the array key).
| Hyde will pull the matching data from here and fill in the blanks.
|
*/

'authors' => [
'mr_hyde' => Author::create(
'mr_hyde', // Required username
'Mr. Hyde', // Optional display name
'https://hydephp.com' // Optional website URL
// The following settings are used in the default blog post template.
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com', // Optional website URL

// The following settings are not used in the bundled templates,
// but you can use them in your own custom views, for example.
// bio: 'The mysterious author of HydePHP',
// avatar: 'avatar.png',
// socials: [
// 'twitter' => 'HydeFramework',
// 'github' => 'hydephp',
// ],
),
],

Expand Down
18 changes: 10 additions & 8 deletions packages/framework/src/Facades/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@
class Author
{
/**
* Construct a new Post Author. For Hyde to discover this author,
* you must call this method from your hyde.php config file.
* Configuration helper method to define a new blog post author, with better IDE support.
*
* The returned array will then be used by the framework to create a new PostAuthor instance.
*
* @see https://hydephp.com/docs/1.x/customization.html#authors
*
* @param string $username The username of the author. This is the key used to find authors in the config.
* @param string|null $name The optional display name of the author, leave blank to use the username.
* @param string|null $website The author's optional website URL. Website, Twitter, etc.
* @param string|null $bio The author's optional biography text. Markdown supported.
* @param string|null $avatar The author's optional avatar image. Supports both image names and full URIs.
* @param array<string, string>|null $socials The author's optional social media links/handles.
*/
public static function create(string $username, ?string $name = null, ?string $website = null): PostAuthor
public static function create(?string $name = null, ?string $website = null, ?string $bio = null, ?string $avatar = null, ?array $socials = null): array
{
return new PostAuthor($username, $name, $website);
return compact('name', 'website', 'bio', 'avatar', 'socials');
}

/**
* Get a Post Author instance from the config. If no author matching the username is found,
* a new Post Author instance will be created with just username supplied to the method.
* Get a Post Author instance by username, or null if not found.
*/
public static function get(string $username): PostAuthor
public static function get(string $username): ?PostAuthor
{
return PostAuthor::get($username);
}
Expand Down
13 changes: 11 additions & 2 deletions packages/framework/src/Foundation/Concerns/HasKernelData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Facades\Config;
use Illuminate\Support\Collection;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Framework\Exceptions\InvalidConfigurationException;

use function collect;

Expand Down Expand Up @@ -49,8 +50,16 @@ public function authors(): Collection

protected function parseConfigurationAuthors(Collection $authors): Collection
{
return $authors->mapWithKeys(function (PostAuthor $author, string $username): array {
return [$username ?: $author->username => $author];
return $authors->mapWithKeys(function (array $author, string $username): array {
if (! $username) {
throw new InvalidConfigurationException('Author username cannot be empty. Did you forget to set the author\'s array key?', 'hyde', 'authors');
}

$username = PostAuthor::normalizeUsername($username);

$author['username'] = $username;

return [$username => PostAuthor::create($author)];
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Hyde\Foundation\Internal;

use Throwable;
use Illuminate\Support\Arr;
use Hyde\Foundation\Application;
use Illuminate\Config\Repository;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Framework\Exceptions\InvalidConfigurationException;

use function array_merge;

Expand Down Expand Up @@ -64,7 +66,14 @@ protected function mergeConfiguration(string $namespace, array $yaml): void
protected function parseAuthors(array $authors): array
{
return Arr::mapWithKeys($authors, function (array $author, string $username): array {
return [$username => PostAuthor::getOrCreate($author)];
try {
return [$username => PostAuthor::create($author)];
} catch (Throwable $exception) {
throw new InvalidConfigurationException(
'Invalid author configuration detected in the YAML config file. Please double check the syntax.',
previous: $exception
);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Hyde\Markdown\Models\Markdown;
use Hyde\Support\Models\DateString;

use function is_string;

/**
* Streamlines the data construction specific to a blog post.
*
Expand Down Expand Up @@ -91,7 +93,7 @@ protected function makeDate(): ?DateString
protected function makeAuthor(): ?PostAuthor
{
if ($this->getMatter('author')) {
return PostAuthor::getOrCreate($this->getMatter('author'));
return $this->getOrCreateAuthor();
}

return null;
Expand All @@ -111,6 +113,17 @@ private function makeDescriptionFromMarkdownBody(): string
return Str::limit((new ConvertsMarkdownToPlainText($this->markdown->body()))->execute(), 125);
}

private function getOrCreateAuthor(): PostAuthor
{
$data = $this->getMatter('author');

if (is_string($data)) {
return PostAuthor::get($data) ?? PostAuthor::create(['username' => $data]);
}

return PostAuthor::create($data);
}

protected function getMatter(string $key): string|null|array
{
/** @var string|null|array $value */
Expand Down
Loading