miscellaneous.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 v1
  5. import (
  6. "strings"
  7. "github.com/gogits/gogs/modules/auth/apiv1"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/middleware"
  10. "github.com/gogits/gogs/modules/setting"
  11. )
  12. // Render an arbitrary Markdown document.
  13. func Markdown(ctx *middleware.Context, form apiv1.MarkdownForm) {
  14. if ctx.HasApiError() {
  15. ctx.JSON(422, base.ApiJsonErr{ctx.GetErrMsg(), base.DOC_URL})
  16. return
  17. }
  18. if len(form.Text) == 0 {
  19. ctx.Write([]byte(""))
  20. return
  21. }
  22. switch form.Mode {
  23. case "gfm":
  24. ctx.Write(base.RenderMarkdown([]byte(form.Text),
  25. setting.AppUrl+strings.TrimPrefix(form.Context, "/")))
  26. default:
  27. ctx.Write(base.RenderRawMarkdown([]byte(form.Text), ""))
  28. }
  29. }
  30. // Render a Markdown document in raw mode.
  31. func MarkdownRaw(ctx *middleware.Context) {
  32. body, err := ctx.Req.Body().Bytes()
  33. if err != nil {
  34. ctx.JSON(422, base.ApiJsonErr{err.Error(), base.DOC_URL})
  35. return
  36. }
  37. ctx.Write(base.RenderRawMarkdown(body, ""))
  38. }