40fe75d9b7
Add an optional `$limit` parameter to `CommentPresenter::markdownBody()` that truncates the comment body before rendering Markdown. The comment partial now passes a default limit of 250 characters, preventing long comments from dominating the layout. Unit tests cover rendering with and without a limit.
35 lines
638 B
PHP
35 lines
638 B
PHP
<?php
|
|
|
|
namespace App\Models\Presenters;
|
|
|
|
use App\Models\Comment;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CommentPresenter
|
|
{
|
|
public $comment;
|
|
|
|
public function __construct(Comment $comment)
|
|
{
|
|
$this->comment = $comment;
|
|
}
|
|
|
|
public function markdownBody(?int $limit = null)
|
|
{
|
|
$body = $this->comment->body;
|
|
|
|
if ($limit !== null) {
|
|
$body = Str::limit($body, $limit);
|
|
}
|
|
|
|
return Str::of($body)->markdown([
|
|
'html_input' => 'strip',
|
|
]);
|
|
}
|
|
|
|
public function relativeCreatedAt()
|
|
{
|
|
return $this->comment->created_at->diffForHumans();
|
|
}
|
|
}
|