You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.go 2.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/util"
  13. "mvdan.cc/xurls/v2"
  14. )
  15. // Markdown render markdown document to HTML
  16. func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
  17. // swagger:operation POST /markdown miscellaneous renderMarkdown
  18. // ---
  19. // summary: Render a markdown document as HTML
  20. // parameters:
  21. // - name: body
  22. // in: body
  23. // schema:
  24. // "$ref": "#/definitions/MarkdownOption"
  25. // consumes:
  26. // - application/json
  27. // produces:
  28. // - text/html
  29. // responses:
  30. // "200":
  31. // "$ref": "#/responses/MarkdownRender"
  32. // "422":
  33. // "$ref": "#/responses/validationError"
  34. if ctx.HasAPIError() {
  35. ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
  36. return
  37. }
  38. if len(form.Text) == 0 {
  39. _, _ = ctx.Write([]byte(""))
  40. return
  41. }
  42. switch form.Mode {
  43. case "gfm":
  44. md := []byte(form.Text)
  45. urlPrefix := form.Context
  46. var meta map[string]string
  47. if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
  48. // check if urlPrefix is already set to a URL
  49. linkRegex, _ := xurls.StrictMatchingScheme("https?://")
  50. m := linkRegex.FindStringIndex(urlPrefix)
  51. if m == nil {
  52. urlPrefix = util.URLJoin(setting.AppURL, form.Context)
  53. }
  54. }
  55. if ctx.Repo != nil && ctx.Repo.Repository != nil {
  56. meta = ctx.Repo.Repository.ComposeMetas()
  57. }
  58. if form.Wiki {
  59. _, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta)))
  60. if err != nil {
  61. ctx.InternalServerError(err)
  62. return
  63. }
  64. } else {
  65. _, err := ctx.Write(markdown.Render(md, urlPrefix, meta))
  66. if err != nil {
  67. ctx.InternalServerError(err)
  68. return
  69. }
  70. }
  71. default:
  72. _, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
  73. if err != nil {
  74. ctx.InternalServerError(err)
  75. return
  76. }
  77. }
  78. }
  79. // MarkdownRaw render raw markdown HTML
  80. func MarkdownRaw(ctx *context.APIContext) {
  81. // swagger:operation POST /markdown/raw miscellaneous renderMarkdownRaw
  82. // ---
  83. // summary: Render raw markdown as HTML
  84. // parameters:
  85. // - name: body
  86. // in: body
  87. // description: Request body to render
  88. // required: true
  89. // schema:
  90. // type: string
  91. // consumes:
  92. // - text/plain
  93. // produces:
  94. // - text/html
  95. // responses:
  96. // "200":
  97. // "$ref": "#/responses/MarkdownRender"
  98. // "422":
  99. // "$ref": "#/responses/validationError"
  100. body, err := ctx.Req.Body().Bytes()
  101. if err != nil {
  102. ctx.Error(http.StatusUnprocessableEntity, "", err)
  103. return
  104. }
  105. _, err = ctx.Write(markdown.RenderRaw(body, "", false))
  106. if err != nil {
  107. ctx.InternalServerError(err)
  108. return
  109. }
  110. }