gitea源码

markup.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package misc
  4. import (
  5. "net/http"
  6. "code.gitea.io/gitea/modules/markup"
  7. "code.gitea.io/gitea/modules/markup/markdown"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/modules/util"
  10. "code.gitea.io/gitea/modules/web"
  11. "code.gitea.io/gitea/routers/common"
  12. "code.gitea.io/gitea/services/context"
  13. )
  14. // Markup render markup document to HTML
  15. func Markup(ctx *context.APIContext) {
  16. // swagger:operation POST /markup miscellaneous renderMarkup
  17. // ---
  18. // summary: Render a markup document as HTML
  19. // parameters:
  20. // - name: body
  21. // in: body
  22. // schema:
  23. // "$ref": "#/definitions/MarkupOption"
  24. // consumes:
  25. // - application/json
  26. // produces:
  27. // - text/html
  28. // responses:
  29. // "200":
  30. // "$ref": "#/responses/MarkupRender"
  31. // "422":
  32. // "$ref": "#/responses/validationError"
  33. form := web.GetForm(ctx).(*api.MarkupOption)
  34. if ctx.HasAPIError() {
  35. ctx.APIError(http.StatusUnprocessableEntity, ctx.GetErrMsg())
  36. return
  37. }
  38. mode := util.Iif(form.Wiki, "wiki", form.Mode) //nolint:staticcheck // form.Wiki is deprecated
  39. common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, form.FilePath)
  40. }
  41. // Markdown render markdown document to HTML
  42. func Markdown(ctx *context.APIContext) {
  43. // swagger:operation POST /markdown miscellaneous renderMarkdown
  44. // ---
  45. // summary: Render a markdown document as HTML
  46. // parameters:
  47. // - name: body
  48. // in: body
  49. // schema:
  50. // "$ref": "#/definitions/MarkdownOption"
  51. // consumes:
  52. // - application/json
  53. // produces:
  54. // - text/html
  55. // responses:
  56. // "200":
  57. // "$ref": "#/responses/MarkdownRender"
  58. // "422":
  59. // "$ref": "#/responses/validationError"
  60. form := web.GetForm(ctx).(*api.MarkdownOption)
  61. if ctx.HasAPIError() {
  62. ctx.APIError(http.StatusUnprocessableEntity, ctx.GetErrMsg())
  63. return
  64. }
  65. mode := util.Iif(form.Wiki, "wiki", form.Mode) //nolint:staticcheck // form.Wiki is deprecated
  66. common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "")
  67. }
  68. // MarkdownRaw render raw markdown HTML
  69. func MarkdownRaw(ctx *context.APIContext) {
  70. // swagger:operation POST /markdown/raw miscellaneous renderMarkdownRaw
  71. // ---
  72. // summary: Render raw markdown as HTML
  73. // parameters:
  74. // - name: body
  75. // in: body
  76. // description: Request body to render
  77. // required: true
  78. // schema:
  79. // type: string
  80. // consumes:
  81. // - text/plain
  82. // produces:
  83. // - text/html
  84. // responses:
  85. // "200":
  86. // "$ref": "#/responses/MarkdownRender"
  87. // "422":
  88. // "$ref": "#/responses/validationError"
  89. defer ctx.Req.Body.Close()
  90. if err := markdown.RenderRaw(markup.NewRenderContext(ctx), ctx.Req.Body, ctx.Resp); err != nil {
  91. ctx.APIErrorInternal(err)
  92. return
  93. }
  94. }