Skip to content

Commit

Permalink
Merge pull request #764 from hydephp/support-meta-fields-as-canonical…
Browse files Browse the repository at this point in the history
…-publication-fields

Support meta fields as canonical publication fields
  • Loading branch information
caendesilva authored Dec 18, 2022
2 parents 6ecfcdd + 43a9460 commit 166baff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,9 @@ protected function captureTextFieldInput(PublicationFieldType $field): string

protected function captureArrayFieldInput(PublicationFieldType $field): array
{
$lines = [];
$this->output->writeln($field->name.' (end with an empty line)');
do {
$line = Str::replace("\n", '', fgets(STDIN));
if ($line === '') {
break;
}
$lines[] = trim($line);
} while (true);

return $lines;
return InputStreamHandler::call();
}

protected function captureImageFieldInput(PublicationFieldType $field, PublicationType $pubType): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Support\Str;
use Rgasch\Collection\Collection;
use RuntimeException;
use function str_starts_with;

/**
* Scaffold a publication file.
Expand All @@ -29,7 +30,9 @@ public function __construct(
protected ?OutputStyle $output = null,
) {
$canonicalFieldName = $this->pubType->canonicalField;
$canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? throw new RuntimeException("Could not find field definition for '$canonicalFieldName' which is required for this type as it's the canonical field");
$canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? $this->handleMissingCanonicalField(
$canonicalFieldName
);
$canonicalValue = $canonicalFieldDefinition->type !== 'array' ? $this->fieldData->{$canonicalFieldName} : $this->fieldData->{$canonicalFieldName}[0];
$canonicalStr = Str::of($canonicalValue)->substr(0, 64);

Expand Down Expand Up @@ -72,4 +75,15 @@ protected function handleCreate(): void

$this->save($output);
}

protected function handleMissingCanonicalField(string $canonicalFieldName): PublicationFieldType
{
if (str_starts_with($canonicalFieldName, '__')) {
return new PublicationFieldType('text', $canonicalFieldName, '0', '0');
}

return throw new RuntimeException(
"Could not find field value for '$canonicalFieldName' which is required for this type as it's the canonical field"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function test_command_with_invalid_publication_type_passed_as_argument()
->assertExitCode(1);
}

// text
public function test_command_with_text_input()
{
InputStreamHandler::mockInput("Hello\nWorld");
Expand All @@ -156,6 +157,27 @@ public function test_command_with_text_input()
$this->assertStringContainsString("Hello\nWorld", file_get_contents(Hyde::path('test-publication/hello-world.md')));
}

// array
public function test_command_with_array_input()
{
InputStreamHandler::mockInput("Foo\nBar");
$this->makeSchemaFile([
'fields' => [[
'type' => 'array',
'name' => 'tags',
'min' => '0',
'max' => '0',
],
],
]);

$this->artisan('make:publication test-publication')
->assertExitCode(0);

$this->assertTrue(File::exists(Hyde::path('test-publication/hello-world.md')));
$this->assertStringContainsString("Foo\nBar", file_get_contents(Hyde::path('test-publication/hello-world.md')));
}

protected function makeSchemaFile(array $merge = []): void
{
file_put_contents(
Expand Down

0 comments on commit 166baff

Please sign in to comment.