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.

83 lines
2.0 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/markup"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  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:operation POST /markdown miscellaneous renderMarkdown
  15. // ---
  16. // summary: Render a markdown document as HTML
  17. // parameters:
  18. // - name: body
  19. // in: body
  20. // schema:
  21. // "$ref": "#/definitions/MarkdownOption"
  22. // consumes:
  23. // - application/json
  24. // produces:
  25. // - text/html
  26. // responses:
  27. // "200":
  28. // "$ref": "#/responses/MarkdownRender"
  29. // "422":
  30. // "$ref": "#/responses/validationError"
  31. if ctx.HasAPIError() {
  32. ctx.Error(422, "", ctx.GetErrMsg())
  33. return
  34. }
  35. if len(form.Text) == 0 {
  36. ctx.Write([]byte(""))
  37. return
  38. }
  39. switch form.Mode {
  40. case "gfm":
  41. md := []byte(form.Text)
  42. context := markup.URLJoin(setting.AppURL, form.Context)
  43. if form.Wiki {
  44. ctx.Write([]byte(markdown.RenderWiki(md, context, nil)))
  45. } else {
  46. ctx.Write(markdown.Render(md, context, nil))
  47. }
  48. default:
  49. ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
  50. }
  51. }
  52. // MarkdownRaw render raw markdown HTML
  53. func MarkdownRaw(ctx *context.APIContext) {
  54. // swagger:operation POST /markdown/raw miscellaneous renderMarkdownRaw
  55. // ---
  56. // summary: Render raw markdown as HTML
  57. // parameters:
  58. // - name: body
  59. // in: body
  60. // type: string
  61. // consumes:
  62. // - text/plain
  63. // produces:
  64. // - text/html
  65. // responses:
  66. // "200":
  67. // "$ref": "#/responses/MarkdownRender"
  68. // "422":
  69. // "$ref": "#/responses/validationError"
  70. body, err := ctx.Req.Body().Bytes()
  71. if err != nil {
  72. ctx.Error(422, "", err)
  73. return
  74. }
  75. ctx.Write(markdown.RenderRaw(body, "", false))
  76. }