mirror of
https://github.com/MichaelMure/git-bug.git
synced 2024-12-15 18:23:08 +03:00
2e17f37175
Move `cleanupText` to utils/text/transform.go `text.Cleanup`: removing unicode control characters except for those allowed by `text.Safe` Add golang.org/x/text dependencies fix text.Cleanup Fix import panic
32 lines
675 B
Go
32 lines
675 B
Go
package text
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
|
|
"golang.org/x/text/runes"
|
|
"golang.org/x/text/transform"
|
|
)
|
|
|
|
func Cleanup(text string) (string, error) {
|
|
// windows new line, Github, really ?
|
|
text = strings.Replace(text, "\r\n", "\n", -1)
|
|
|
|
// remove all unicode control characters except
|
|
// '\n', '\r' and '\t'
|
|
t := runes.Remove(runes.Predicate(func(r rune) bool {
|
|
switch r {
|
|
case '\r', '\n', '\t':
|
|
return false
|
|
}
|
|
return unicode.IsControl(r)
|
|
}))
|
|
sanitized, _, err := transform.String(t, text)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// trim extra new line not displayed in the github UI but still present in the data
|
|
return strings.TrimSpace(sanitized), nil
|
|
}
|