Skip to content

Commit

Permalink
Merge pull request #449 from hydephp/refactor-author-system
Browse files Browse the repository at this point in the history
Refactor author configuration system
  • Loading branch information
caendesilva authored May 23, 2022
2 parents f0d2553 + a58408f commit ce3c110
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 343 deletions.
2 changes: 1 addition & 1 deletion .github/dev-docs/RELEASE-NOTES-DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ The deprecated option named `hyde.docs_directory` has been removed.

Use `docs.output_directory` instead.

The authors.yml has been deprecated, and will be refactored in an upcoming release.
The authors.yml and related services have been removed. Define authors in the main Hyde config instead.
6 changes: 3 additions & 3 deletions .github/dev-docs/blog-posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,16 @@ date: "2022-01-01"
```yaml
author: "Mr. Hyde" # Arbitrary name displayed "as is"
author: mr_hyde # Username defined in `authors.yml` config
author: mr_hyde # Username defined in `authors` config
author: # Array of author data
name: "Mr. Hyde"
username: mr_hyde
website: https://mrhyde.example.com
website: https://twitter.com/hyde_php
```
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 the values in the `authors.yml` config.
will override all the values in the `authors` config entry.

### Image

Expand Down
35 changes: 35 additions & 0 deletions .github/dev-docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ With a concept directly inspired by [Laravel Jetstream](https://jetstream.larave
],
```

### 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.

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.

#### Example
// torchlight! {"lineNumbers": false}
```php
'authors' => [
Author::create(
username: 'mr_hyde', // Required username
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com' // Optional website URL
),
],
```

This is equivalent to the following front matter:
```yaml
author:
username: mr_hyde
name: Mr. Hyde
website: https://hydephp.com
```
But you only have to specify the username:
```yaml
author: mr_hyde
```
### Footer
The footer can be customized using Markdown, and even disabled completely.
Expand Down
25 changes: 25 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
|
*/

use Hyde\Framework\Helpers\Author;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Helpers\Meta;

Expand Down Expand Up @@ -123,6 +124,30 @@
Features::torchlight(),
],

/*
|--------------------------------------------------------------------------
| Blog Post 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.
|
| 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.
|
*/

'authors' => [
Author::create(
username: 'mr_hyde', // Required username
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com' // Optional website URL
),
],

/*
|--------------------------------------------------------------------------
| Footer Text
Expand Down
18 changes: 16 additions & 2 deletions src/Concerns/GeneratesPageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Services\AuthorService;
use Tests\TestCase;

/**
Expand Down Expand Up @@ -48,7 +47,7 @@ protected function parseFrontMatterMetadata(): void
}

if (isset($this->matter['author'])) {
$this->metadata['author'] = AuthorService::getAuthorName($this->matter['author']);
$this->metadata['author'] = $this->getAuthorName($this->matter['author']);
}

if (isset($this->matter['category'])) {
Expand Down Expand Up @@ -87,4 +86,19 @@ protected function makeOpenGraphPropertiesForArticle(): void
}
}
}

/**
* Parse the author name string from front matter with support for both flat and array notation.
*
* @param string|array $author
* @return string
*/
protected function getAuthorName(string|array $author): string
{
if (is_string($author)) {
return $author;
}

return $author['name'] ?? $author['username'] ?? 'Guest';
}
}
9 changes: 7 additions & 2 deletions src/Concerns/HasAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Hyde\Framework\Concerns;

use Hyde\Framework\Helpers\Author as AuthorHelper;
use Hyde\Framework\Models\Author;
use Hyde\Framework\Services\AuthorService;

/**
* Handle logic for Page models that have an Author.
Expand All @@ -19,17 +19,22 @@ public function constructAuthor(): void
{
if (isset($this->matter['author'])) {
if (is_string($this->matter['author'])) {
// If the author is a string, we assume it's a username
// and we'll try to find the author in the config
$this->author = $this->findAuthor($this->matter['author']);
}
if (is_array($this->matter['author'])) {
// If the author is an array, we'll assume it's a user
// with one-off custom data, so we create a new author.
// In the future we may want to merge config data with custom data
$this->author = $this->createAuthor($this->matter['author']);
}
}
}

protected function findAuthor(string $author): Author
{
return AuthorService::find($author) ?: new Author($author);
return AuthorHelper::get($author);
}

protected function createAuthor(array $data): Author
Expand Down
31 changes: 31 additions & 0 deletions src/Helpers/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Hyde\Framework\Helpers;

use Hyde\Framework\Models\Author as AuthorModel;
use Illuminate\Support\Collection;

/**
* @see \Tests\Feature\AuthorHelperTest
*/
class Author
{
public static function create(string $username, ?string $name = null, ?string $website = null): AuthorModel
{
return new AuthorModel($username, [
'name' => $name,
'website'=> $website,
]);
}

public static function all(): Collection
{
return new Collection(config('authors', []));
}

public static function get(string $username): AuthorModel
{
return static::all()->firstWhere('username', $username)
?? static::create($username);
}
}
11 changes: 11 additions & 0 deletions src/Helpers/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Hyde\Framework\Helpers;

class Image
{
public static function get(string $path): string
{
return $path;
}
}
12 changes: 8 additions & 4 deletions src/Models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

/**
* The Post Author Object Model.
*
* The Author is parsed from the config/authors.yml file using the AuthorService class
*/
class Author
{
/**
* The username (slug) of the author.
* The username of the author.
* This is the key used to find authors in the config.
*
* @var string
*/
Expand All @@ -34,6 +33,11 @@ class Author
public ?string $website = null;

/**
* Construct a new Author object.
*
* Parameters are supplied through an array to make it
* easy to load data from Markdown post front matter.
*
* @param string $username
* @param array|null $data
*/
Expand All @@ -49,7 +53,7 @@ public function __construct(string $username, ?array $data = [])
}

/**
* Get the author's name.
* Get the author's preferred name.
*
* @see \Tests\Unit\AuthorGetNameTest
*
Expand Down
Loading

0 comments on commit ce3c110

Please sign in to comment.