truncate.go 498 B

123456789101112131415161718192021
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sanitizer
  4. import "strings"
  5. func TruncateHTML(input string, max int) string {
  6. text := StripTags(input)
  7. // Collapse multiple spaces into a single space
  8. text = strings.Join(strings.Fields(text), " ")
  9. // Convert to runes to be safe with unicode
  10. runes := []rune(text)
  11. if len(runes) > max {
  12. return strings.TrimSpace(string(runes[:max])) + "…"
  13. }
  14. return text
  15. }