From c7378ec4f9a7adadbf87dd4528897da96032ca50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Mon, 22 Apr 2024 10:45:29 +0100 Subject: [PATCH] feat: add content export command --- app/Console/Commands/ExportContentCommand.php | 205 ++++++++++++++++++ composer.json | 1 + composer.lock | 91 +++++++- 3 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/ExportContentCommand.php diff --git a/app/Console/Commands/ExportContentCommand.php b/app/Console/Commands/ExportContentCommand.php new file mode 100644 index 00000000..366b52e5 --- /dev/null +++ b/app/Console/Commands/ExportContentCommand.php @@ -0,0 +1,205 @@ +htmlConverter = new HtmlConverter([ + 'header_style' => 'atx', + 'strip_tags' => true, + ]); + } + + /** + * Execute the console command. + */ + public function handle(): int + { + $this->exportPages(); + + $this->exportPosts(); + + $this->exportForms(); + + $this->exportPeople(); + + return self::SUCCESS; + } + + protected function writeToFile(string $filename, Collection $content): void + { + Storage::disk('local')->put("exports/{$filename}", $content->toJson(\JSON_PRETTY_PRINT)); + } + + protected function html2md(?string $content): ?string + { + if (\is_string($content)) { + return $this->htmlConverter->convert($content); + } + + return $content; + } + + protected function mapBlocks(Collection $blocks) + { + return $blocks + ->map( + fn (Block $block) => collect($block->content) + ->reject(fn ($field) => \in_array($field, $this->validBlockFields)) + ->map(function ($field) { + $content = data_get($field, $this->option('locale')); + + if (\is_string($content)) { + $content = $this->html2md($content); + } + + return $content; + }) + ->put('children', $this->mapBlocks($block->children)) + ->filter() + ->all() + ) + ->filter() + ->dot() + ->all(); + } + + protected function exportPages(): void + { + $this->writeToFile( + 'pages.json', + Page::query() + ->with('blocks') + ->get() + ->mapWithKeys(fn (Page $model) => [ + "{$model->id}.{$model->title}" => [ + 'title' => $this->html2md($model->title), + 'description' => $this->html2md($model->description), + 'blocks' => $this->mapBlocks($model->blocks), + ], + ]) + ->dot() + ->filter() + ); + } + + protected function exportPosts(): void + { + $this->writeToFile( + 'post-categories.json', + PostCategory::query() + ->get() + ->mapWithKeys(fn (PostCategory $model) => [ + "{$model->id}.{$model->title}" => [ + 'title' => $this->html2md($model->title), + 'description' => $this->html2md($model->description), + ], + ]) + ->dot() + ->filter() + ); + + $this->writeToFile( + 'posts.json', + Post::query() + ->with('blocks') + ->get() + ->mapWithKeys(fn (Post $model) => [ + "{$model->id}.{$model->title}" => [ + 'title' => $this->html2md($model->title), + 'description' => $this->html2md($model->description), + 'blocks' => $this->mapBlocks($model->blocks), + ], + ]) + ->dot() + ->filter() + ); + } + + protected function exportForms(): void + { + $this->writeToFile( + 'forms.json', + Form::query() + ->with('blocks') + ->get() + ->mapWithKeys(fn (Form $model) => [ + "{$model->id}.{$model->title}" => [ + 'title' => $this->html2md($model->title), + 'description' => $this->html2md($model->description), + 'blocks' => $this->mapBlocks($model->blocks), + ], + ]) + ->dot() + ->filter() + ); + } + + protected function exportPeople(): void + { + $this->writeToFile( + 'people.json', + Person::query() + ->with('blocks') + ->get() + ->mapWithKeys(fn (Person $model) => [ + "{$model->id}.{$model->name}" => [ + 'title' => $this->html2md($model->title), + 'description' => $this->html2md($model->description), + 'blocks' => $this->mapBlocks($model->blocks), + ], + ]) + ->dot() + ->filter() + ); + } +} diff --git a/composer.json b/composer.json index 896273f2..53e0580d 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "laravel/framework": "^10.46", "laravel/tinker": "^2.9", "league/flysystem-aws-s3-v3": "^3.24", + "league/html-to-markdown": "^5.1", "league/omnipay": "^3.2", "maatwebsite/excel": "^3.1", "matthewbdaly/laravel-azure-storage": "^2.0", diff --git a/composer.lock b/composer.lock index 7fd730fa..20477e2e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "33c667b5fdcf43c9acf6f94a9b5d4ee1", + "content-hash": "d75552b79eca449762d6c5cc35dbd5c4", "packages": [ { "name": "andreiio/blade-remix-icon", @@ -2954,6 +2954,95 @@ ], "time": "2024-01-26T18:25:23+00:00" }, + { + "name": "league/html-to-markdown", + "version": "5.1.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/html-to-markdown.git", + "reference": "0b4066eede55c48f38bcee4fb8f0aa85654390fd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/html-to-markdown/zipball/0b4066eede55c48f38bcee4fb8f0aa85654390fd", + "reference": "0b4066eede55c48f38bcee4fb8f0aa85654390fd", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xml": "*", + "php": "^7.2.5 || ^8.0" + }, + "require-dev": { + "mikehaertl/php-shellcommand": "^1.1.0", + "phpstan/phpstan": "^1.8.8", + "phpunit/phpunit": "^8.5 || ^9.2", + "scrutinizer/ocular": "^1.6", + "unleashedtech/php-coding-standard": "^2.7 || ^3.0", + "vimeo/psalm": "^4.22 || ^5.0" + }, + "bin": [ + "bin/html-to-markdown" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.2-dev" + } + }, + "autoload": { + "psr-4": { + "League\\HTMLToMarkdown\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Colin O'Dell", + "email": "colinodell@gmail.com", + "homepage": "https://www.colinodell.com", + "role": "Lead Developer" + }, + { + "name": "Nick Cernis", + "email": "nick@cern.is", + "homepage": "http://modernnerd.net", + "role": "Original Author" + } + ], + "description": "An HTML-to-markdown conversion helper for PHP", + "homepage": "https://github.com/thephpleague/html-to-markdown", + "keywords": [ + "html", + "markdown" + ], + "support": { + "issues": "https://github.com/thephpleague/html-to-markdown/issues", + "source": "https://github.com/thephpleague/html-to-markdown/tree/5.1.1" + }, + "funding": [ + { + "url": "https://www.colinodell.com/sponsor", + "type": "custom" + }, + { + "url": "https://www.paypal.me/colinpodell/10.00", + "type": "custom" + }, + { + "url": "https://github.com/colinodell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/league/html-to-markdown", + "type": "tidelift" + } + ], + "time": "2023-07-12T21:21:09+00:00" + }, { "name": "league/mime-type-detection", "version": "1.15.0",