From 40fe75d9b7b0acfe38357a6493ad9424263f4bf8 Mon Sep 17 00:00:00 2001 From: w33b Date: Sun, 16 Aug 2026 10:15:08 +0200 Subject: [PATCH] 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. --- app/Models/Presenters/CommentPresenter.php | 10 ++++-- resources/views/partials/comment.blade.php | 2 +- tests/Unit/CommentPresenterTest.php | 40 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/CommentPresenterTest.php diff --git a/app/Models/Presenters/CommentPresenter.php b/app/Models/Presenters/CommentPresenter.php index a0787a8..3444677 100644 --- a/app/Models/Presenters/CommentPresenter.php +++ b/app/Models/Presenters/CommentPresenter.php @@ -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', ]); } diff --git a/resources/views/partials/comment.blade.php b/resources/views/partials/comment.blade.php index b5aaa83..c226d60 100644 --- a/resources/views/partials/comment.blade.php +++ b/resources/views/partials/comment.blade.php @@ -14,7 +14,7 @@ @endif
-
{!! $comment->presenter()->markdownBody() !!}
+
{!! $comment->presenter()->markdownBody($limit ?? 250) !!}
diff --git a/tests/Unit/CommentPresenterTest.php b/tests/Unit/CommentPresenterTest.php new file mode 100644 index 0000000..55296ec --- /dev/null +++ b/tests/Unit/CommentPresenterTest.php @@ -0,0 +1,40 @@ + '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); + } +}