markdown.go 976 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package misc
  5. import (
  6. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/markup"
  10. )
  11. func Markdown(c *context.APIContext, form api.MarkdownOption) {
  12. if c.HasApiError() {
  13. c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg())
  14. return
  15. }
  16. if len(form.Text) == 0 {
  17. _, _ = c.Write([]byte(""))
  18. return
  19. }
  20. var md []byte
  21. switch form.Mode {
  22. case "gfm":
  23. md = markup.Markdown([]byte(form.Text), form.Context, nil)
  24. default:
  25. md = markup.SanitizeBytes(markup.RawMarkdown([]byte(form.Text), ""))
  26. }
  27. _, _ = c.Write(md)
  28. }
  29. func MarkdownRaw(c *context.APIContext) {
  30. body, err := c.Req.Body().Bytes()
  31. if err != nil {
  32. c.Error(http.StatusUnprocessableEntity, "", err)
  33. return
  34. }
  35. _, _ = c.Write(markup.SanitizeBytes(markup.RawMarkdown(body, "")))
  36. }