You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package utils
  4. import (
  5. "html"
  6. "net/url"
  7. "strings"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. // RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username
  11. func RemoveUsernameParameterSuffix(name string) string {
  12. if index := strings.Index(name, " ("); index >= 0 {
  13. name = name[:index]
  14. }
  15. return name
  16. }
  17. // SanitizeFlashErrorString will sanitize a flash error string
  18. func SanitizeFlashErrorString(x string) string {
  19. return strings.ReplaceAll(html.EscapeString(x), "\n", "<br>")
  20. }
  21. // IsExternalURL checks if rawURL points to an external URL like http://example.com
  22. func IsExternalURL(rawURL string) bool {
  23. parsed, err := url.Parse(rawURL)
  24. if err != nil {
  25. return true
  26. }
  27. appURL, _ := url.Parse(setting.AppURL)
  28. if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) {
  29. return true
  30. }
  31. return false
  32. }