markdown.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. api "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/pkg/context"
  8. "github.com/gogits/gogs/pkg/markup"
  9. )
  10. // https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document
  11. func Markdown(c *context.APIContext, form api.MarkdownOption) {
  12. if c.HasApiError() {
  13. c.Error(422, "", c.GetErrMsg())
  14. return
  15. }
  16. if len(form.Text) == 0 {
  17. c.Write([]byte(""))
  18. return
  19. }
  20. switch form.Mode {
  21. case "gfm":
  22. c.Write(markup.Markdown([]byte(form.Text), form.Context, nil))
  23. default:
  24. c.Write(markup.RawMarkdown([]byte(form.Text), ""))
  25. }
  26. }
  27. // https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-a-markdown-document-in-raw-mode
  28. func MarkdownRaw(c *context.APIContext) {
  29. body, err := c.Req.Body().Bytes()
  30. if err != nil {
  31. c.Error(422, "", err)
  32. return
  33. }
  34. c.Write(markup.RawMarkdown(body, ""))
  35. }