[#6778] updated the excerpt modifier to properly account for multibyte characters
This commit is contained in:
parent
fac0d5b899
commit
c7c590bace
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
- Updated the default `COUNT` list request query to use `rowid` when possible to minimize the need of having the `id` field in a covering index.
|
- Updated the default `COUNT` list request query to use `rowid` when possible to minimize the need of having the `id` field in a covering index.
|
||||||
|
|
||||||
|
- Updated the excerpt modifier to properly account for multibyte characters ([#6778](https://github.com/pocketbase/pocketbase/issues/6778)).
|
||||||
|
|
||||||
|
|
||||||
## v0.27.1
|
## v0.27.1
|
||||||
|
|
||||||
|
|
|
@ -135,12 +135,16 @@ func (m *excerptModifier) Modify(value any) (any, error) {
|
||||||
result := strings.TrimSpace(builder.String())
|
result := strings.TrimSpace(builder.String())
|
||||||
|
|
||||||
if len(result) > m.max {
|
if len(result) > m.max {
|
||||||
result = strings.TrimSpace(result[:m.max])
|
// 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 {
|
if m.withEllipsis {
|
||||||
result += "..."
|
result += "..."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,6 +134,20 @@ func TestExcerptModifierModify(t *testing.T) {
|
||||||
html,
|
html,
|
||||||
plainText,
|
plainText,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// multibyte chars
|
||||||
|
{
|
||||||
|
"mutibyte chars <= max",
|
||||||
|
[]string{"4", "t"},
|
||||||
|
"аб\nв ",
|
||||||
|
"аб в",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mutibyte chars > max",
|
||||||
|
[]string{"3", "t"},
|
||||||
|
"аб\nв ",
|
||||||
|
"аб...",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range scenarios {
|
for _, s := range scenarios {
|
||||||
|
|
Loading…
Reference in New Issue