Feat: Define basic mardown-to-markup functionality including factories

This commit is contained in:
Jonathan Nielson 2024-08-04 12:32:32 -05:00
parent 2b677ef1e5
commit 0f2fb26651
3 changed files with 52 additions and 5 deletions

View File

@ -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,
]);
}
}

View File

@ -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;
}
}

View File

@ -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)