You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

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