-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ryangjchandler/feature/markdown-json
feature: MarkdownJson driver
- Loading branch information
Showing
6 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Orbit\Drivers; | ||
|
||
use SplFileInfo; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Spatie\YamlFrontMatter\YamlFrontMatter; | ||
|
||
class MarkdownJson extends Markdown | ||
{ | ||
protected function dumpContent(Model $model): string | ||
{ | ||
$matter = array_filter($model->attributesToArray(), function ($value, $key) { | ||
return $key !== 'content' && $value !== null; | ||
}, ARRAY_FILTER_USE_BOTH); | ||
|
||
$json = json_encode($matter, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | ||
|
||
return implode(PHP_EOL, [ | ||
'---', | ||
rtrim($json, PHP_EOL), | ||
'---', | ||
$model->getAttribute('content'), | ||
]); | ||
} | ||
|
||
protected function parseContent(SplFileInfo $file): array | ||
{ | ||
$content = file_get_contents($file->getPathname()); | ||
|
||
$pattern = '/^[\s\r\n]?---[\s\r\n]?$/sm'; | ||
|
||
$parts = preg_split($pattern, PHP_EOL . ltrim($content)); | ||
|
||
$matter = count($parts) < 3 ? [] : json_decode(trim($parts[1]), true); | ||
|
||
$body = implode(PHP_EOL.'---'.PHP_EOL, array_slice($parts, 2)); | ||
|
||
return array_merge( | ||
$matter, | ||
['content' => trim($body)] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Orbit\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Orbit\Concerns\Orbital; | ||
|
||
class MarkdownJsonModel extends Model | ||
{ | ||
use Orbital; | ||
|
||
protected $guarded = []; | ||
|
||
protected static $driver = 'md_json'; | ||
|
||
public static function schema(Blueprint $table) | ||
{ | ||
$table->string('name'); | ||
} | ||
|
||
public function getKeyName() | ||
{ | ||
return 'name'; | ||
} | ||
|
||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |