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.6 kB

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