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.

44 lines
1.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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 misc
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/markdown"
  9. )
  10. // Markdown render markdown document to HTML
  11. // see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document
  12. func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
  13. if ctx.HasAPIError() {
  14. ctx.Error(422, "", ctx.GetErrMsg())
  15. return
  16. }
  17. if len(form.Text) == 0 {
  18. ctx.Write([]byte(""))
  19. return
  20. }
  21. switch form.Mode {
  22. case "gfm":
  23. ctx.Write(markdown.Render([]byte(form.Text), form.Context, nil))
  24. default:
  25. ctx.Write(markdown.RenderRaw([]byte(form.Text), ""))
  26. }
  27. }
  28. // MarkdownRaw render raw markdown HTML
  29. // see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-a-markdown-document-in-raw-mode
  30. func MarkdownRaw(ctx *context.APIContext) {
  31. body, err := ctx.Req.Body().Bytes()
  32. if err != nil {
  33. ctx.Error(422, "", err)
  34. return
  35. }
  36. ctx.Write(markdown.RenderRaw(body, ""))
  37. }