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.

72 lines
1.6 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. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // Markdown render markdown document to HTML
  12. func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
  13. // swagger:route POST /markdown renderMarkdown
  14. //
  15. // Consumes:
  16. // - application/json
  17. //
  18. // Produces:
  19. // - text/html
  20. //
  21. // Responses:
  22. // 200: MarkdownRender
  23. // 422: validationError
  24. if ctx.HasAPIError() {
  25. ctx.Error(422, "", ctx.GetErrMsg())
  26. return
  27. }
  28. if len(form.Text) == 0 {
  29. ctx.Write([]byte(""))
  30. return
  31. }
  32. switch form.Mode {
  33. case "gfm":
  34. md := []byte(form.Text)
  35. context := markdown.URLJoin(setting.AppURL, form.Context)
  36. if form.Wiki {
  37. ctx.Write([]byte(markdown.RenderWiki(md, context, nil)))
  38. } else {
  39. ctx.Write(markdown.Render(md, context, nil))
  40. }
  41. default:
  42. ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
  43. }
  44. }
  45. // MarkdownRaw render raw markdown HTML
  46. func MarkdownRaw(ctx *context.APIContext) {
  47. // swagger:route POST /markdown/raw renderMarkdownRaw
  48. //
  49. // Consumes:
  50. // - text/plain
  51. //
  52. // Produces:
  53. // - text/html
  54. //
  55. // Responses:
  56. // 200: MarkdownRender
  57. // 422: validationError
  58. body, err := ctx.Req.Body().Bytes()
  59. if err != nil {
  60. ctx.Error(422, "", err)
  61. return
  62. }
  63. ctx.Write(markdown.RenderRaw(body, "", false))
  64. }