1package text
2
3import (
4 "strings"
5 "unicode"
6
7 "golang.org/x/text/runes"
8 "golang.org/x/text/transform"
9)
10
11func Cleanup(text string) (string, error) {
12 // windows new line, Github, really ?
13 text = strings.Replace(text, "\r\n", "\n", -1)
14
15 // remove all unicode control characters except
16 // '\n', '\r' and '\t'
17 t := runes.Remove(runes.Predicate(func(r rune) bool {
18 switch r {
19 case '\r', '\n', '\t':
20 return false
21 }
22 return unicode.IsControl(r)
23 }))
24 sanitized, _, err := transform.String(t, text)
25 if err != nil {
26 return "", err
27 }
28
29 // trim extra new line not displayed in the github UI but still present in the data
30 return strings.TrimSpace(sanitized), nil
31}