12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package markup
- import (
- "sync"
- "github.com/microcosm-cc/bluemonday"
- "gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/lazyregexp"
- )
- 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(lazyregexp.New(`^language-\w+$`).Regexp()).OnElements("code")
-
- sanitizer.policy.AllowAttrs("type").Matching(lazyregexp.New(`^checkbox$`).Regexp()).OnElements("input")
- sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
-
- sanitizer.policy.AllowURLSchemes("data")
-
- sanitizer.policy.AllowURLSchemes(conf.Markdown.CustomURLSchemes...)
- })
- }
- func Sanitize(s string) string {
- return sanitizer.policy.Sanitize(s)
- }
- func SanitizeBytes(b []byte) []byte {
- return sanitizer.policy.SanitizeBytes(b)
- }
|