api.go 723 B

1234567891011121314151617181920212223242526272829303132
  1. package app
  2. import (
  3. "net/http"
  4. "github.com/flamego/flamego"
  5. "github.com/microcosm-cc/bluemonday"
  6. )
  7. func ipynbSanitizer() *bluemonday.Policy {
  8. p := bluemonday.UGCPolicy()
  9. p.AllowAttrs("class", "data-prompt-number").OnElements("div")
  10. p.AllowAttrs("class").OnElements("img")
  11. p.AllowURLSchemes("data")
  12. return p
  13. }
  14. func SanitizeIpynb() flamego.Handler {
  15. p := ipynbSanitizer()
  16. return func(c flamego.Context) {
  17. body, err := c.Request().Body().Bytes()
  18. if err != nil {
  19. c.ResponseWriter().WriteHeader(http.StatusInternalServerError)
  20. c.ResponseWriter().Write([]byte("read body"))
  21. return
  22. }
  23. c.ResponseWriter().WriteHeader(http.StatusOK)
  24. c.ResponseWriter().Write([]byte(p.Sanitize(string(body))))
  25. }
  26. }