1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-04 20:27:35 +02:00

feat(serializers): add ability to set custom frontmatter header parser flag

This commit is contained in:
Awilum
2022-05-13 18:56:44 +03:00
parent b70e32d6ed
commit 2dc20b9f54
9 changed files with 66 additions and 10 deletions

View File

@@ -86,7 +86,7 @@ class Frontmatter
$headerSerializer = 'yaml';
}
$decode = static function (string $input) use ($headerSerializer) {
$decode = static function (string $input) use ($headerSerializer, $allowed) {
// Remove UTF-8 BOM if it exists.
$input = ltrim($input, "\xef\xbb\xbf");
@@ -96,11 +96,27 @@ class Frontmatter
// Parse Frontmatter and Body
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . strings($input)->trimLeft()->toString());
if (count($parts) < 3) {
return ['content' => strings($input)->trim()->toString()];
// Replace empty array item with empty string and reindex array.
if (empty($parts[0])) {
unset($parts[0]);
$parts = array_values(array_filter($parts));
}
// Check for custom frontmatter header serializers
if (strings(strings($parts[0])->lines()[1])->trim()->contains('---')) {
$headerSerializer = strings(strings($parts[0])->lines()[1])->trim()->after('---')->toString();
$parts[0] = strings($parts[0])->replaceFirst('---' . $headerSerializer, '')->toString();
return serializers()->{$headerSerializer}()->decode(strings($parts[1])->trim()->toString(), false) + ['content' => strings(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))->trim()->toString()];
if (! in_array($headerSerializer, $allowed)) {
$headerSerializer = 'yaml';
}
}
$frontmatter = serializers()->{$headerSerializer}()->decode(strings($parts[0])->trim()->toString(), false);
$content = ['content' => strings($parts[1] ?? '')->trim()->toString()];
return (is_array($frontmatter) ? $frontmatter : []) + $content;
};
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {

View File

@@ -454,8 +454,6 @@ slugify:
# neon.encode.flags: The flag can be 1, which will create multiline output.
#
# phparray.decode.cache: Cache result data or no. Default is true.
#
# phpcode.decode.cache: Cache result data or no. Default is true.
serializers:
json:
decode:
@@ -489,11 +487,11 @@ serializers:
cache: true
header:
serializer: yaml
allowed: ['yaml', 'json', 'neon']
allowed: ['yaml', 'json', 'json5', 'neon']
encode:
header:
serializer: yaml
allowed: ['yaml', 'json', 'neon']
allowed: ['yaml', 'json', 'json5', 'neon']
neon:
decode:
cache: true

View File

@@ -0,0 +1,4 @@
---json
{"title": "Frontmatter JSON"}
---
Content is here.

View File

@@ -0,0 +1,4 @@
---json5
{title: "Frontmatter JSON5"}
---
Content is here.

View File

@@ -0,0 +1,4 @@
---neon
title: "Frontmatter NEON"
---
Content is here.

View File

@@ -0,0 +1,4 @@
---yaml
title: "Frontmatter YAML"
---
Content is here.

View File

@@ -476,11 +476,11 @@ serializers:
cache: true
header:
serializer: yaml
allowed: ['yaml', 'json', 'neon']
allowed: ['yaml', 'json', 'json5', 'neon']
encode:
header:
serializer: yaml
allowed: ['yaml', 'json', 'neon']
allowed: ['yaml', 'json', 'json5', 'neon']
neon:
decode:
cache: true

View File

@@ -8,6 +8,7 @@ test('encode', function () {
serializers()->frontmatter()
->encode(['title' => 'Foo',
'content' => 'Content is here.']));
});
test('decode', function () {
@@ -15,6 +16,26 @@ test('decode', function () {
'content' => 'Content is here.'],
serializers()->frontmatter()
->decode($string = filesystem()->file(ROOT_DIR . '/tests/fixtures/serializers/frontmatter.md')->get()));
$this->assertEquals(['title' => 'Frontmatter YAML',
'content' => 'Content is here.'],
serializers()->frontmatter()
->decode($string = filesystem()->file(ROOT_DIR . '/tests/fixtures/serializers/frontmatter-yaml.md')->get()));
$this->assertEquals(['title' => 'Frontmatter JSON',
'content' => 'Content is here.'],
serializers()->frontmatter()
->decode($string = filesystem()->file(ROOT_DIR . '/tests/fixtures/serializers/frontmatter-json.md')->get()));
$this->assertEquals(['title' => 'Frontmatter JSON5',
'content' => 'Content is here.'],
serializers()->frontmatter()
->decode($string = filesystem()->file(ROOT_DIR . '/tests/fixtures/serializers/frontmatter-json5.md')->get()));
$this->assertEquals(['title' => 'Frontmatter NEON',
'content' => 'Content is here.'],
serializers()->frontmatter()
->decode($string = filesystem()->file(ROOT_DIR . '/tests/fixtures/serializers/frontmatter-neon.md')->get()));
});
test('get cache ID', function () {

View File

@@ -14,6 +14,11 @@ test('decode', function () {
'content' => 'Bar'],
serializers()->yaml()
->decode("title: Foo\ncontent: Bar"));
$this->assertEquals(['title' => 'Foo',
'content' => 'Bar'],
serializers()->yaml()
->decode("{\"title\": \"Foo\", \"content\": \"Bar\"}"));
});
test('get cache ID', function () {