post HTML regeneration

This commit is contained in:
Alexander Yakovlev 2022-08-09 07:07:41 +07:00
parent 77011c1fe0
commit aded565374
Signed by: oreolek
GPG key ID: 8D24103F5EE2A6C0
2 changed files with 54 additions and 3 deletions

View file

@ -0,0 +1,48 @@
<?php
namespace App\Console\Commands;
use App\Models\Post;
use Illuminate\Console\Command;
use League\CommonMark\CommonMarkConverter;
use Illuminate\Support\Str;
class RegenerateHtml extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'posts:regenerate_html';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Recalculate the HTML excerpts and content fields from original Markdown';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$markdown = new CommonMarkConverter([
'allow_unsafe_links' => true,
]);
Post::chunk(100, function($posts) use($markdown) {
foreach($posts as $post) {
$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);
}
$post->save();
}
});
return 0;
}
}

View file

@ -14,11 +14,11 @@ return new class extends Migration
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->text('content_html')->nullable();
$table->text('excerpt_html')->nullable();
$table->longText('content_html')->nullable();
$table->longText('excerpt_html')->nullable();
});
Schema::table('pages', function (Blueprint $table) {
$table->text('content_html')->nullable();
$table->longText('content_html')->nullable();
});
}
@ -33,5 +33,8 @@ return new class extends Migration
$table->dropColumn('content_html');
$table->dropColumn('excerpt_html');
});
Schema::table('pages', function (Blueprint $table) {
$table->dropColumn('content_html');
});
}
};