mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-10-13 01:28:16 +02:00
[#6778] updated the excerpt modifier to properly account for multibyte characters
This commit is contained in:
1 parent
fac0d5b899
commit
c7c590bace
3 files changed
+24
-4
No files matched your search
@@ -135,10 +135,14 @@ func (m *excerptModifier) Modify(value any) (any, error) {
|
||||
result := strings.TrimSpace(builder.String())
|
||||
|
||||
if len(result) > m.max {
|
||||
result = strings.TrimSpace(result[:m.max])
|
||||
|
||||
if m.withEllipsis {
|
||||
result += "..."
|
||||
// note: casted to []rune to properly account for multi-byte chars
|
||||
runes := []rune(result)
|
||||
if len(runes) > m.max {
|
||||
result = string(runes[:m.max])
|
||||
result = strings.TrimSpace(result)
|
||||
if m.withEllipsis {
|
||||
result += "..."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +134,20 @@ func TestExcerptModifierModify(t *testing.T) {
|
||||
html,
|
||||
plainText,
|
||||
},
|
||||
|
||||
// multibyte chars
|
||||
{
|
||||
"mutibyte chars <= max",
|
||||
[]string{"4", "t"},
|
||||
"аб\nв ",
|
||||
"аб в",
|
||||
},
|
||||
{
|
||||
"mutibyte chars > max",
|
||||
[]string{"3", "t"},
|
||||
"аб\nв ",
|
||||
"аб...",
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
|
||||
Reference in new issue
Block a user