gitea源码

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "regexp"
  6. "github.com/microcosm-cc/bluemonday"
  7. )
  8. // createRepoDescriptionPolicy returns a minimal more strict policy that is used for
  9. // repository descriptions.
  10. func (st *Sanitizer) createRepoDescriptionPolicy() *bluemonday.Policy {
  11. policy := bluemonday.NewPolicy()
  12. policy.AllowStandardURLs()
  13. // Allow italics and bold.
  14. policy.AllowElements("i", "b", "em", "strong")
  15. // Allow code.
  16. policy.AllowElements("code")
  17. // Allow links
  18. policy.AllowAttrs("href", "target", "rel").OnElements("a")
  19. // Allow classes for emojis
  20. policy.AllowAttrs("class").Matching(regexp.MustCompile(`^emoji$`)).OnElements("img", "span")
  21. policy.AllowAttrs("aria-label").OnElements("span")
  22. return policy
  23. }
  24. // SanitizeDescription sanitizes the HTML generated for a repository description.
  25. func SanitizeDescription(s string) string {
  26. return GetDefaultSanitizer().descriptionPolicy.Sanitize(s)
  27. }