feat(comments): add character limit to comment markdown body
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.
This commit is contained in:
@@ -14,9 +14,15 @@ class CommentPresenter
|
||||
$this->comment = $comment;
|
||||
}
|
||||
|
||||
public function markdownBody()
|
||||
public function markdownBody(?int $limit = null)
|
||||
{
|
||||
return Str::of($this->comment->body)->markdown([
|
||||
$body = $this->comment->body;
|
||||
|
||||
if ($limit !== null) {
|
||||
$body = Str::limit($body, $limit);
|
||||
}
|
||||
|
||||
return Str::of($body)->markdown([
|
||||
'html_input' => 'strip',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
@endif
|
||||
</div>
|
||||
<div class="mt-1 flex-grow w-full">
|
||||
<div class="text-gray-700 dark:text-gray-200">{!! $comment->presenter()->markdownBody() !!}</div>
|
||||
<div class="text-gray-700 dark:text-gray-200">{!! $comment->presenter()->markdownBody($limit ?? 250) !!}</div>
|
||||
</div>
|
||||
<div class="mt-2 space-x-2">
|
||||
<span class="text-gray-500 dark:text-gray-300 font-medium">
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Comment;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CommentPresenterTest extends TestCase
|
||||
{
|
||||
public function test_markdown_body_renders_full_comment_without_limit(): void
|
||||
{
|
||||
$comment = new Comment(['body' => 'Hello **world**']);
|
||||
|
||||
$result = (string) $comment->presenter()->markdownBody();
|
||||
|
||||
$this->assertStringContainsString('Hello', $result);
|
||||
$this->assertStringContainsString('world', $result);
|
||||
}
|
||||
|
||||
public function test_markdown_body_is_truncated_when_limit_is_provided(): void
|
||||
{
|
||||
$body = str_repeat('a very long comment. ', 50);
|
||||
$comment = new Comment(['body' => $body]);
|
||||
|
||||
$result = (string) $comment->presenter()->markdownBody(40);
|
||||
|
||||
$this->assertLessThan(strlen($body), strlen($result));
|
||||
$this->assertStringContainsString('...', $result);
|
||||
}
|
||||
|
||||
public function test_markdown_body_is_not_truncated_when_body_is_shorter_than_limit(): void
|
||||
{
|
||||
$comment = new Comment(['body' => 'Short']);
|
||||
|
||||
$result = (string) $comment->presenter()->markdownBody(100);
|
||||
|
||||
$this->assertStringContainsString('Short', $result);
|
||||
$this->assertStringNotContainsString('...', $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user