Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

markdown.go 1.6KB

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