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:
Yury Kotov
2023-07-04 13:32:37 -05:00
committed by Umputun
parent 497f3ce47f
commit add01455fb
2 changed files with 7 additions and 3 deletions
+5 -1
View File
@@ -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) + " ..."
}
+2 -2
View File
@@ -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 {