post HTML regeneration
This commit is contained in:
parent
77011c1fe0
commit
aded565374
48
app/Console/Commands/RegenerateHtml.php
Normal file
48
app/Console/Commands/RegenerateHtml.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue