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); + } +}