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

Support meta fields as canonical publication fields #764

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
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 @@ -53,19 +53,19 @@ public function testCreate()
', file_get_contents(Hyde::path('test-publication/hello-world.md')));
}

protected function makePublicationType(): PublicationType
protected function makePublicationType(array $fields = [
[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
],
]): PublicationType
{
return new PublicationType(
'test',
'title',
fields: [
[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
],
],
fields: $fields,
directory: 'test-publication',
);
}
Expand Down
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