diff --git a/app/Models/Article.php b/app/Models/Article.php index 001efc6..af13995 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -22,7 +23,7 @@ class Article extends Model 'body', 'published_at', 'header_image_url', - 'path_to_markdown_source', + 'markdown_body', 'processed_at', ]; @@ -31,4 +32,12 @@ class Article extends Model { return $this->belongsTo(Author::class); } + + public function convertMarkdownToMarkup() + { + $this->body = Str::of($this->markdown_body)->markdown([ + 'html_input' => 'strip', + 'allow_unsafe_links' => false, + ]); + } } diff --git a/database/factories/ArticleFactory.php b/database/factories/ArticleFactory.php index 232de60..63f5d27 100644 --- a/database/factories/ArticleFactory.php +++ b/database/factories/ArticleFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Author; +use App\Models\Article; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -10,11 +11,28 @@ use Illuminate\Database\Eloquent\Factories\Factory; */ class ArticleFactory extends Factory { + /** + * Configure the model factory. + */ + public function configure(): static + { + return $this->afterMaking(function (Article $article) { + $article->convertMarkdownToMarkup(); + })->afterCreating(function (Article $article) { + // ... + }); + } + /** * The current User being used by the factory. */ protected static Author $author; + /** + * The current Markdown body being used by the factory. + */ + protected static string $markdown_body; + /** * Define the model's default state. * @@ -27,10 +45,10 @@ class ArticleFactory extends Factory 'slug' => fake()->unique()->slug(), 'author_id' => static::$author->id ??= Author::factory()->create()->id, 'subtitle' => fake()->sentence(), - 'body' => fake()->paragraphs(2, true), + 'body' => '', // We'll update it after making using the on-Model method 'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))), 'header_image_url' => fake()->imageUrl(), - 'path_to_markdown_source' => fake()->name(), + 'markdown_body' => $this->generateMarkdown(), 'processed_at' => fake()->date('Y-m-d H:m:s'), ]; } @@ -49,4 +67,24 @@ class ArticleFactory extends Factory 'author_id' => $author->id, ]); } + + private function generateMarkdown() + { + $markdown_result = ''; + + foreach(range(1,rand(15,45)) as $index) { + $dice_roll = rand(0,100); + + if($dice_roll <= 50) { // Make a SubTitle + $markdown_result .= '# ' . fake()->words(rand(1,5), true) . '\n'; + } elseif ($dice_roll <= 100) { // Make a paragraph + $markdown_result .= fake()->sentences(rand(2,5), true) . '\n'; + } + + // Always append a new line + $markdown_result .= '\n'; + } + + return $markdown_result; + } } diff --git a/database/migrations/2024_02_05_014414_create_articles_table.php b/database/migrations/2024_02_05_014414_create_articles_table.php index 4c77c9b..5b81d37 100644 --- a/database/migrations/2024_02_05_014414_create_articles_table.php +++ b/database/migrations/2024_02_05_014414_create_articles_table.php @@ -37,8 +37,8 @@ return new class extends Migration $table->string('header_image_url', 255)->nullable(); - // The path on the server disk to the original markdown file. - $table->string('path_to_markdown_source'); + // The original, markdown version of the article body + $table->longText('markdown_body'); // The dateTime when the original markdown file was parsed and the // HTML body was generated (among other things)