1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

Use truncated entry description as title if unavailable

This commit is contained in:
Frédéric Guillot 2022-03-04 16:49:44 -08:00
parent c9e0f0b3e4
commit 1eb01b39e7
10 changed files with 314 additions and 24 deletions

View file

@ -0,0 +1,23 @@
// Copyright 2022 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sanitizer
import "strings"
func TruncateHTML(input string, max int) string {
text := StripTags(input)
text = strings.ReplaceAll(text, "\n", " ")
text = strings.ReplaceAll(text, "\t", " ")
text = strings.ReplaceAll(text, " ", " ")
text = strings.TrimSpace(text)
// Convert to runes to be safe with unicode
runes := []rune(text)
if len(runes) > max {
return strings.TrimSpace(string(runes[:max])) + "…"
}
return text
}

View file

@ -0,0 +1,65 @@
// Copyright 2022 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sanitizer
import "testing"
func TestTruncateHTMWithTextLowerThanLimitL(t *testing.T) {
input := `This is a <strong>bug 🐛</strong>.`
expected := `This is a bug 🐛.`
output := TruncateHTML(input, 50)
if expected != output {
t.Errorf(`Wrong output: %q != %q`, expected, output)
}
}
func TestTruncateHTMLWithTextAboveLimit(t *testing.T) {
input := `This is <strong>HTML</strong>.`
expected := `This…`
output := TruncateHTML(input, 4)
if expected != output {
t.Errorf(`Wrong output: %q != %q`, expected, output)
}
}
func TestTruncateHTMLWithUnicodeTextAboveLimit(t *testing.T) {
input := `This is a <strong>bike 🚲</strong>.`
expected := `This…`
output := TruncateHTML(input, 4)
if expected != output {
t.Errorf(`Wrong output: %q != %q`, expected, output)
}
}
func TestTruncateHTMLWithMultilineTextAboveLimit(t *testing.T) {
input := `
This is a <strong>bike
🚲</strong>.
`
expected := `This is a bike…`
output := TruncateHTML(input, 15)
if expected != output {
t.Errorf(`Wrong output: %q != %q`, expected, output)
}
}
func TestTruncateHTMLWithMultilineTextLowerThanLimit(t *testing.T) {
input := `
This is a <strong>bike
🚲</strong>.
`
expected := `This is a bike 🚲.`
output := TruncateHTML(input, 20)
if expected != output {
t.Errorf(`Wrong output: %q != %q`, expected, output)
}
}