diff --git a/composer.json b/composer.json index 206e56e..21d7e7d 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,11 @@ ], "require": { "php": "^8.1", + "ext-yaml": "*", "illuminate/database": "^10.0|^11.0", "illuminate/support": "^10.0|^11.0", - "spatie/yaml-front-matter": "^2.0", - "ext-yaml": "*" + "spatie/laravel-markdown": "^2.5", + "spatie/yaml-front-matter": "^2.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Models/MarkdownModel.php b/src/Models/MarkdownModel.php index 68fef55..047f862 100644 --- a/src/Models/MarkdownModel.php +++ b/src/Models/MarkdownModel.php @@ -10,6 +10,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use Spatie\LaravelMarkdown\MarkdownRenderer; use Spatie\YamlFrontMatter\YamlFrontMatter; use TheBatClaudio\EloquentMarkdown\Support\MarkdownCollection; @@ -57,11 +58,17 @@ public function __construct(?string $filePath = null) } } + /** + * Set filesystem. + */ public static function setFilesystem(Filesystem $filesystem): void { static::$filesystem = $filesystem; } + /** + * Get filesystem. + */ public static function getFilesystem(): Filesystem { return static::$filesystem; @@ -89,7 +96,7 @@ private static function removeFileExtension(string $filename): string protected static function extractFileId(string $filePath): string { return static::removeFileExtension( - Str::replace(static::getContentPath() . static::DIR_SEPARATOR, '', $filePath) + Str::replace(static::getContentPath().static::DIR_SEPARATOR, '', $filePath) ); } @@ -125,7 +132,7 @@ private static function retrieveMarkdownFiles(): void $allFiles = static::getFilesystem()->allFiles($contentPath); // Check if we already retrieved all files - if (!static::$allMarkdownFiles || count($allFiles) !== count(static::$allMarkdownFiles)) { + if (! static::$allMarkdownFiles || count($allFiles) !== count(static::$allMarkdownFiles)) { static::$allMarkdownFiles = (new MarkdownCollection($allFiles)) ->mapWithKeys(function ($file) { return [ @@ -142,11 +149,11 @@ private static function retrieveMarkdownFile(string $id): void { $contentPath = static::getContentPath(); - if (!static::$allMarkdownFiles) { + if (! static::$allMarkdownFiles) { static::$allMarkdownFiles = new MarkdownCollection(); } - $filePath = $contentPath . static::DIR_SEPARATOR . $id . static::FILE_EXTENSION; + $filePath = $contentPath.static::DIR_SEPARATOR.$id.static::FILE_EXTENSION; static::$allMarkdownFiles[static::removeFileExtension($id)] = (static::getFilesystem()->exists($filePath)) ? new static($filePath) : null; } @@ -154,7 +161,7 @@ private static function retrieveMarkdownFile(string $id): void /** * Get all markdown files. * - * @param array $columns + * @param array $columns */ public static function all($columns = ['*']): MarkdownCollection { @@ -189,7 +196,7 @@ private function getFileContent(): string 'content', ] ) - ) . "\n" + )."\n" ); $content .= $this->content; @@ -205,7 +212,7 @@ private function getFilePath(): string return $this->file_path; } - return static::getContentPath() . static::DIR_SEPARATOR . $this->id . static::FILE_EXTENSION; + return static::getContentPath().static::DIR_SEPARATOR.$this->id.static::FILE_EXTENSION; } /** @@ -229,4 +236,12 @@ protected function performDeleteOnModel(): void { static::getFilesystem()->delete($this->getFilePath()); } + + /** + * Render markdown content as HTML. + */ + public function toHtml(): string + { + return app(MarkdownRenderer::class)->toHtml($this->content); + } } diff --git a/src/Providers/EloquentMarkdownServiceProvider.php b/src/Providers/EloquentMarkdownServiceProvider.php index abbebfd..e1514c6 100644 --- a/src/Providers/EloquentMarkdownServiceProvider.php +++ b/src/Providers/EloquentMarkdownServiceProvider.php @@ -16,7 +16,7 @@ final class EloquentMarkdownServiceProvider extends ServiceProvider public function boot(): void { $this->publishes([ - __DIR__ . '/../../config/markdown.php' => config_path('markdown.php'), + __DIR__.'/../../config/markdown.php' => config_path('markdown.php'), ], 'config'); } diff --git a/src/Support/MarkdownCollection.php b/src/Support/MarkdownCollection.php index f8af5bc..32c0b8f 100644 --- a/src/Support/MarkdownCollection.php +++ b/src/Support/MarkdownCollection.php @@ -4,9 +4,9 @@ namespace TheBatClaudio\EloquentMarkdown\Support; -use Illuminate\Support\Facades\Request; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Request; /** * Class MarkdownCollection (used to add paginate method to a simple collection). diff --git a/tests/Models/MarkdownModelTest.php b/tests/Models/MarkdownModelTest.php index 7bc0e84..04eb53e 100644 --- a/tests/Models/MarkdownModelTest.php +++ b/tests/Models/MarkdownModelTest.php @@ -3,7 +3,6 @@ declare(strict_types=1); use Illuminate\Filesystem\FilesystemManager; -use Illuminate\Support\Facades\Config; use TheBatClaudio\EloquentMarkdown\Models\MarkdownModel; class TestModel extends MarkdownModel @@ -18,7 +17,7 @@ protected static function getContentPath(): string TestModel::setFilesystem( (new FilesystemManager(null)) ->createLocalDriver([ - 'root' => __DIR__ . '/../content', + 'root' => __DIR__.'/../content', ]) ); }); @@ -28,14 +27,14 @@ protected static function getContentPath(): string expect($markdowns) ->not->toBeEmpty() - ->toHaveCount(4) - ->and(array_keys($markdowns)) - ->toContain( - 'test', - 'test2', - 'test3', - 'folder/test' - ); + ->toHaveCount(4) + ->and(array_keys($markdowns)) + ->toContain( + 'test', + 'test2', + 'test3', + 'folder/test' + ); }); it('returns the right file calling find method', function () { @@ -46,33 +45,33 @@ protected static function getContentPath(): string expect($markdown) ->toBeInstanceOf(TestModel::class) ->not->toBeEmpty() - ->and($markdown?->toArray()) - ->toHaveKeys([ - // YAML Front Matter attributes - 'first_attribute', - 'second_attribute', - 'third_attribute', - // Content - 'content', - // Default keys: file_path, file_name, id - 'file_path', - 'file_name', - 'id', - ]) - ->and($markdown?->first_attribute) - ->toBe('First attribute') - ->and($markdown?->second_attribute) - ->toBe('Second attribute') - ->and($markdown?->third_attribute) - ->toBe('Third attribute') - ->and($markdown?->content) - ->toContain('The time has come') - ->and($markdown?->file_path) - ->toBe('pages' . '/' . $fileId . MarkdownModel::FILE_EXTENSION) - ->and($markdown?->file_name) - ->toBe($fileId . MarkdownModel::FILE_EXTENSION) - ->and($markdown?->id) - ->toBe($fileId); + ->and($markdown?->toArray()) + ->toHaveKeys([ + // YAML Front Matter attributes + 'first_attribute', + 'second_attribute', + 'third_attribute', + // Content + 'content', + // Default keys: file_path, file_name, id + 'file_path', + 'file_name', + 'id', + ]) + ->and($markdown?->first_attribute) + ->toBe('First attribute') + ->and($markdown?->second_attribute) + ->toBe('Second attribute') + ->and($markdown?->third_attribute) + ->toBe('Third attribute') + ->and($markdown?->content) + ->toContain('The time has come') + ->and($markdown?->file_path) + ->toBe('pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION) + ->and($markdown?->file_name) + ->toBe($fileId.MarkdownModel::FILE_EXTENSION) + ->and($markdown?->id) + ->toBe($fileId); }); it('should save file', function () { @@ -87,7 +86,7 @@ protected static function getContentPath(): string $markdown->save(); - $filepath = __DIR__ . '/../content/' . 'pages' . '/' . $fileId . MarkdownModel::FILE_EXTENSION; + $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; expect(file_exists($filepath)) ->toBeTrue(); @@ -107,7 +106,7 @@ protected static function getContentPath(): string $markdown->save(); - $filepath = __DIR__ . '/../content/' . 'pages' . '/' . $fileId . MarkdownModel::FILE_EXTENSION; + $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; expect(file_exists($filepath)) ->toBeTrue(); @@ -120,7 +119,7 @@ protected static function getContentPath(): string it('should update file', function () { $fileId = 'update-test'; - $filepath = __DIR__ . '/../content/' . 'pages' . '/' . $fileId . MarkdownModel::FILE_EXTENSION; + $filepath = __DIR__.'/../content/'.'pages'.'/'.$fileId.MarkdownModel::FILE_EXTENSION; $markdown = new TestModel(); @@ -142,8 +141,8 @@ protected static function getContentPath(): string expect($markdown) ->not->toBeNull() - ->and($markdown->attribute) - ->toBe('changed'); + ->and($markdown->attribute) + ->toBe('changed'); $markdown->delete(); }); diff --git a/tests/Models/MarkdownWithDateModelTest.php b/tests/Models/MarkdownWithDateModelTest.php index 7af3806..1904e3e 100644 --- a/tests/Models/MarkdownWithDateModelTest.php +++ b/tests/Models/MarkdownWithDateModelTest.php @@ -4,7 +4,6 @@ use Carbon\Carbon; use Illuminate\Filesystem\FilesystemManager; -use Illuminate\Support\Facades\Config; use TheBatClaudio\EloquentMarkdown\Models\MarkdownWithDateModel; class TestWithDateModel extends MarkdownWithDateModel @@ -19,7 +18,7 @@ protected static function getContentPath(): string TestModel::setFilesystem( (new FilesystemManager(null)) ->createLocalDriver([ - 'root' => __DIR__ . '/../content', + 'root' => __DIR__.'/../content', ]) ); });