diff options
author | Savely Krasovsky <savely@krasovsky.me> | 2018-09-21 05:11:13 +0300 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-09-20 19:11:13 -0700 |
commit | d79bab2997350de8f7a8f7216eaa0599567775ae (patch) | |
tree | e01e0863422b3e4453bfa8cca1507b0b3a4e205b /template/functions.go | |
parent | 2538eea1776e1d03d33465ad2001512caca93937 (diff) |
Fix invalid output when truncating Unicode text in templates
Diffstat (limited to 'template/functions.go')
-rw-r--r-- | template/functions.go | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/template/functions.go b/template/functions.go index d20b445..13c57aa 100644 --- a/template/functions.go +++ b/template/functions.go @@ -5,7 +5,6 @@ package template // import "miniflux.app/template" import ( - "bytes" "html/template" "net/mail" "strings" @@ -80,15 +79,13 @@ func (f *funcMap) Map() template.FuncMap { }, "dict": dict, "truncate": func(str string, max int) string { - if len(str) > max { - var buffer bytes.Buffer - - buffer.WriteString(str[:max-1]) - buffer.WriteString("…") - - return buffer.String() + runes := 0 + for i := range str { + runes++ + if runes > max { + return str[:i] + "…" + } } - return str }, "theme_color": func(theme string) string { |