truncate.go 586 B

1234567891011121314151617181920212223
  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. text = strings.ReplaceAll(text, "\n", " ")
  8. text = strings.ReplaceAll(text, "\t", " ")
  9. // Collapse multiple spaces into a single space
  10. text = strings.Join(strings.Fields(text), " ")
  11. // Convert to runes to be safe with unicode
  12. runes := []rune(text)
  13. if len(runes) > max {
  14. return strings.TrimSpace(string(runes[:max])) + "…"
  15. }
  16. return text
  17. }