Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

markup.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2023 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package common
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/markup"
  11. "code.gitea.io/gitea/modules/markup/markdown"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. "mvdan.cc/xurls/v2"
  15. )
  16. // RenderMarkup renders markup text for the /markup and /markdown endpoints
  17. func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) {
  18. var markupType string
  19. relativePath := ""
  20. if len(text) == 0 {
  21. _, _ = ctx.Write([]byte(""))
  22. return
  23. }
  24. switch mode {
  25. case "markdown":
  26. // Raw markdown
  27. if err := markdown.RenderRaw(&markup.RenderContext{
  28. Ctx: ctx,
  29. Links: markup.Links{
  30. Base: urlPrefix,
  31. },
  32. }, strings.NewReader(text), ctx.Resp); err != nil {
  33. ctx.Error(http.StatusInternalServerError, err.Error())
  34. }
  35. return
  36. case "comment":
  37. // Comment as markdown
  38. markupType = markdown.MarkupName
  39. case "gfm":
  40. // Github Flavored Markdown as document
  41. markupType = markdown.MarkupName
  42. case "file":
  43. // File as document based on file extension
  44. markupType = ""
  45. relativePath = filePath
  46. default:
  47. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
  48. return
  49. }
  50. if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
  51. // check if urlPrefix is already set to a URL
  52. linkRegex, _ := xurls.StrictMatchingScheme("https?://")
  53. m := linkRegex.FindStringIndex(urlPrefix)
  54. if m == nil {
  55. urlPrefix = util.URLJoin(setting.AppURL, urlPrefix)
  56. }
  57. }
  58. meta := map[string]string{}
  59. if repo != nil && repo.Repository != nil {
  60. if mode == "comment" {
  61. meta = repo.Repository.ComposeMetas()
  62. } else {
  63. meta = repo.Repository.ComposeDocumentMetas()
  64. }
  65. }
  66. if mode != "comment" {
  67. meta["mode"] = "document"
  68. }
  69. if err := markup.Render(&markup.RenderContext{
  70. Ctx: ctx,
  71. Links: markup.Links{
  72. Base: urlPrefix,
  73. },
  74. Metas: meta,
  75. IsWiki: wiki,
  76. Type: markupType,
  77. RelativePath: relativePath,
  78. }, strings.NewReader(text), ctx.Resp); err != nil {
  79. if markup.IsErrUnsupportedRenderExtension(err) {
  80. ctx.Error(http.StatusUnprocessableEntity, err.Error())
  81. } else {
  82. ctx.Error(http.StatusInternalServerError, err.Error())
  83. }
  84. return
  85. }
  86. }