diff --git a/backend/app/store/comment.go b/backend/app/store/comment.go index bd7c09f6..ad505163 100644 --- a/backend/app/store/comment.go +++ b/backend/app/store/comment.go @@ -146,7 +146,7 @@ func (c *Comment) Snippet(limit int) string { if size < limit { return cleanText } - snippet := []rune(cleanText)[:size] + snippet := []rune(cleanText)[:limit] // go back in snippet and found the first space for i := len(snippet) - 1; i >= 0; i-- { if snippet[i] == ' ' { @@ -154,6 +154,10 @@ func (c *Comment) Snippet(limit int) string { break } } + // Don't add a space if comment is just a one single word which has been truncated. + if len(snippet) == limit { + return string(snippet) + "..." + } return string(snippet) + " ..." } diff --git a/backend/app/store/comment_test.go b/backend/app/store/comment_test.go index f0043341..2716d056 100644 --- a/backend/app/store/comment_test.go +++ b/backend/app/store/comment_test.go @@ -196,8 +196,8 @@ func TestComment_Snippet(t *testing.T) { {0, "", ""}, {-1, "test\nblah", "test blah"}, {5, "test\nblah", "test ..."}, - {5, "xyz12345 xxx", "xyz12345 ..."}, - {10, "xyz12345 xxx\ntest 123456", "xyz12345 xxx test ..."}, + {5, "xyz12345 xxx", "xyz12..."}, + {10, "xyz12345 xxx\ntest 123456", "xyz12345 ..."}, } for i, tt := range tbl {