Fix snippet generation
1) Current implementation simply removes the last word, without truncating up to limit length. 2) In case if even the first word (magnet link or some base64?) is too long don't add extra space.
This commit is contained in:
@@ -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) + " ..."
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user