observers
This commit is contained in:
parent
6e958c0ed2
commit
0f2186a50c
22
app/Observers/PageObserver.php
Normal file
22
app/Observers/PageObserver.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Page;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PageObserver
|
||||
{
|
||||
public function saving(Page $page)
|
||||
{
|
||||
$markdown = new CommonMarkConverter([
|
||||
'allow_unsafe_links' => true,
|
||||
]);
|
||||
$page->content_html = $markdown->convert($page->content);
|
||||
$page->excerpt_html = Str::words($page->content_html, config('app.brief_limit'), '…');
|
||||
if (class_exists('\tidy')) {
|
||||
$page->excerpt_html = \tidy::repairString($page->excerpt_html);
|
||||
}
|
||||
}
|
||||
}
|
22
app/Observers/PostObserver.php
Normal file
22
app/Observers/PostObserver.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Post;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PostObserver
|
||||
{
|
||||
public function saving(Post $post)
|
||||
{
|
||||
$markdown = new CommonMarkConverter([
|
||||
'allow_unsafe_links' => true,
|
||||
]);
|
||||
$post->content_html = $markdown->convert($post->content);
|
||||
$post->excerpt_html = Str::words($post->content_html, config('app.brief_limit'), '…');
|
||||
if (class_exists('\tidy')) {
|
||||
$post->excerpt_html = \tidy::repairString($post->excerpt_html);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Page;
|
||||
use App\Observers\PostObserver;
|
||||
use App\Observers\PageObserver;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
|
@ -13,7 +17,8 @@ class AppServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
Post::observe(PostObserver::class);
|
||||
Page::observe(PageObserver::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue