Feat: Define basic mardown-to-markup functionality including factories
This commit is contained in:
parent
2b677ef1e5
commit
0f2fb26651
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
@ -22,7 +23,7 @@ class Article extends Model
|
||||||
'body',
|
'body',
|
||||||
'published_at',
|
'published_at',
|
||||||
'header_image_url',
|
'header_image_url',
|
||||||
'path_to_markdown_source',
|
'markdown_body',
|
||||||
'processed_at',
|
'processed_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
@ -31,4 +32,12 @@ class Article extends Model
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Author::class);
|
return $this->belongsTo(Author::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function convertMarkdownToMarkup()
|
||||||
|
{
|
||||||
|
$this->body = Str::of($this->markdown_body)->markdown([
|
||||||
|
'html_input' => 'strip',
|
||||||
|
'allow_unsafe_links' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Database\Factories;
|
namespace Database\Factories;
|
||||||
|
|
||||||
use App\Models\Author;
|
use App\Models\Author;
|
||||||
|
use App\Models\Article;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -10,11 +11,28 @@ use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
*/
|
*/
|
||||||
class ArticleFactory extends 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.
|
* The current User being used by the factory.
|
||||||
*/
|
*/
|
||||||
protected static Author $author;
|
protected static Author $author;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current Markdown body being used by the factory.
|
||||||
|
*/
|
||||||
|
protected static string $markdown_body;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define the model's default state.
|
* Define the model's default state.
|
||||||
*
|
*
|
||||||
|
|
@ -27,10 +45,10 @@ class ArticleFactory extends Factory
|
||||||
'slug' => fake()->unique()->slug(),
|
'slug' => fake()->unique()->slug(),
|
||||||
'author_id' => static::$author->id ??= Author::factory()->create()->id,
|
'author_id' => static::$author->id ??= Author::factory()->create()->id,
|
||||||
'subtitle' => fake()->sentence(),
|
'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))),
|
'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))),
|
||||||
'header_image_url' => fake()->imageUrl(),
|
'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'),
|
'processed_at' => fake()->date('Y-m-d H:m:s'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
@ -49,4 +67,24 @@ class ArticleFactory extends Factory
|
||||||
'author_id' => $author->id,
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,8 @@ return new class extends Migration
|
||||||
|
|
||||||
$table->string('header_image_url', 255)->nullable();
|
$table->string('header_image_url', 255)->nullable();
|
||||||
|
|
||||||
// The path on the server disk to the original markdown file.
|
// The original, markdown version of the article body
|
||||||
$table->string('path_to_markdown_source');
|
$table->longText('markdown_body');
|
||||||
|
|
||||||
// The dateTime when the original markdown file was parsed and the
|
// The dateTime when the original markdown file was parsed and the
|
||||||
// HTML body was generated (among other things)
|
// HTML body was generated (among other things)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue