markdown.go 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package misc
  7. import (
  8. "gitote/gitote/pkg/context"
  9. "gitote/gitote/pkg/markup"
  10. api "gitlab.com/gitote/go-gitote-client"
  11. )
  12. // Markdown render markdown document to HTML
  13. func Markdown(c *context.APIContext, form api.MarkdownOption) {
  14. if c.HasAPIError() {
  15. c.Error(422, "", c.GetErrMsg())
  16. return
  17. }
  18. if len(form.Text) == 0 {
  19. c.Write([]byte(""))
  20. return
  21. }
  22. switch form.Mode {
  23. case "gfm":
  24. c.Write(markup.Markdown([]byte(form.Text), form.Context, nil))
  25. default:
  26. c.Write(markup.RawMarkdown([]byte(form.Text), ""))
  27. }
  28. }
  29. // MarkdownRaw render raw markdown HTML
  30. func MarkdownRaw(c *context.APIContext) {
  31. body, err := c.Req.Body().Bytes()
  32. if err != nil {
  33. c.Error(422, "", err)
  34. return
  35. }
  36. c.Write(markup.RawMarkdown(body, ""))
  37. }