12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package markup
- import (
- "regexp"
- "sync"
- "github.com/microcosm-cc/bluemonday"
- "gogs.io/gogs/internal/setting"
- )
- type Sanitizer struct {
- policy *bluemonday.Policy
- init sync.Once
- }
- var sanitizer = &Sanitizer{
- policy: bluemonday.UGCPolicy(),
- }
- func NewSanitizer() {
- sanitizer.init.Do(func() {
-
- sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code")
-
- sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
- sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
-
- sanitizer.policy.AllowURLSchemes("data")
-
- sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
- })
- }
- func Sanitize(s string) string {
- return sanitizer.policy.Sanitize(s)
- }
- func SanitizeBytes(b []byte) []byte {
- return sanitizer.policy.SanitizeBytes(b)
- }
|