Skip to content

Commit

Permalink
feat: add toHtml method to render markdown content
Browse files Browse the repository at this point in the history
  • Loading branch information
thebatclaudio committed May 27, 2024
1 parent 2856095 commit 43c9227
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Easily manage Markdown files with YAML Front Matter section using Eloquent Model

2. By default, markdown files will be retrieved from `markdown` storage disk, so you need to define it in your `config/filesystems.php` file:

```
```php
<?php
return [
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 22 additions & 7 deletions src/Models/MarkdownModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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 [
Expand All @@ -142,19 +149,19 @@ 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;
}

/**
* Get all markdown files.
*
* @param array $columns
* @param array $columns
*/
public static function all($columns = ['*']): MarkdownCollection
{
Expand Down Expand Up @@ -189,7 +196,7 @@ private function getFileContent(): string
'content',
]
)
) . "\n"
)."\n"
);
$content .= $this->content;

Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Providers/EloquentMarkdownServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Support/MarkdownCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
83 changes: 41 additions & 42 deletions tests/Models/MarkdownModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +17,7 @@ protected static function getContentPath(): string
TestModel::setFilesystem(
(new FilesystemManager(null))
->createLocalDriver([
'root' => __DIR__ . '/../content',
'root' => __DIR__.'/../content',
])
);
});
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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();
});
Expand Down
3 changes: 1 addition & 2 deletions tests/Models/MarkdownWithDateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +18,7 @@ protected static function getContentPath(): string
TestModel::setFilesystem(
(new FilesystemManager(null))
->createLocalDriver([
'root' => __DIR__ . '/../content',
'root' => __DIR__.'/../content',
])
);
});
Expand Down

0 comments on commit 43c9227

Please sign in to comment.