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.

wiki_path.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package wiki
  4. import (
  5. "net/url"
  6. "path"
  7. "strings"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/util"
  10. )
  11. // To define the wiki related concepts:
  12. // * Display Segment: the text what user see for a wiki page (aka, the title):
  13. // - "Home Page"
  14. // - "100% Free"
  15. // - "2000-01-02 meeting"
  16. // * Web Path:
  17. // - "/wiki/Home-Page"
  18. // - "/wiki/100%25+Free"
  19. // - "/wiki/2000-01-02+meeting.-"
  20. // - If a segment has a suffix "DashMarker(.-)", it means that there is no dash-space conversion for this segment.
  21. // - If a WebPath is a "*.md" pattern, then use the unescaped value directly as GitPath, to make users can access the raw file.
  22. // * Git Path (only space doesn't need to be escaped):
  23. // - "/.wiki.git/Home-Page.md"
  24. // - "/.wiki.git/100%25 Free.md"
  25. // - "/.wiki.git/2000-01-02 meeting.-.md"
  26. // TODO: support subdirectory in the future
  27. //
  28. // Although this package now has the ability to support subdirectory, but the route package doesn't:
  29. // * Double-escaping problem: the URL "/wiki/abc%2Fdef" becomes "/wiki/abc/def" by ctx.Params, which is incorrect
  30. // * This problem should have been 99% fixed, but it needs more tests.
  31. // * The old wiki code's behavior is always using %2F, instead of subdirectory, so there are a lot of legacy "%2F" files in user wikis.
  32. type WebPath string
  33. var reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"}
  34. func validateWebPath(name WebPath) error {
  35. for _, s := range WebPathSegments(name) {
  36. if util.SliceContainsString(reservedWikiNames, s) {
  37. return repo_model.ErrWikiReservedName{Title: s}
  38. }
  39. }
  40. return nil
  41. }
  42. func hasDashMarker(s string) bool {
  43. return strings.HasSuffix(s, ".-")
  44. }
  45. func removeDashMarker(s string) string {
  46. return strings.TrimSuffix(s, ".-")
  47. }
  48. func addDashMarker(s string) string {
  49. return s + ".-"
  50. }
  51. func unescapeSegment(s string) (string, error) {
  52. if hasDashMarker(s) {
  53. s = removeDashMarker(s)
  54. } else {
  55. s = strings.ReplaceAll(s, "-", " ")
  56. }
  57. unescaped, err := url.QueryUnescape(s)
  58. if err != nil {
  59. return s, err // un-escaping failed, but it's still safe to return the original string, because it is only a title for end users
  60. }
  61. return unescaped, nil
  62. }
  63. func escapeSegToWeb(s string, hadDashMarker bool) string {
  64. if hadDashMarker || strings.Contains(s, "-") || strings.HasSuffix(s, ".md") {
  65. s = addDashMarker(s)
  66. } else {
  67. s = strings.ReplaceAll(s, " ", "-")
  68. }
  69. s = url.QueryEscape(s)
  70. return s
  71. }
  72. func WebPathSegments(s WebPath) []string {
  73. a := strings.Split(string(s), "/")
  74. for i := range a {
  75. a[i], _ = unescapeSegment(a[i])
  76. }
  77. return a
  78. }
  79. func WebPathToGitPath(s WebPath) string {
  80. if strings.HasSuffix(string(s), ".md") {
  81. ret, _ := url.PathUnescape(string(s))
  82. return util.PathJoinRelX(ret)
  83. }
  84. a := strings.Split(string(s), "/")
  85. for i := range a {
  86. shouldAddDashMarker := hasDashMarker(a[i])
  87. a[i], _ = unescapeSegment(a[i])
  88. a[i] = escapeSegToWeb(a[i], shouldAddDashMarker)
  89. a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path
  90. a[i] = strings.ReplaceAll(a[i], "+", " ")
  91. }
  92. return strings.Join(a, "/") + ".md"
  93. }
  94. func GitPathToWebPath(s string) (wp WebPath, err error) {
  95. if !strings.HasSuffix(s, ".md") {
  96. return "", repo_model.ErrWikiInvalidFileName{FileName: s}
  97. }
  98. s = strings.TrimSuffix(s, ".md")
  99. a := strings.Split(s, "/")
  100. for i := range a {
  101. shouldAddDashMarker := hasDashMarker(a[i])
  102. if a[i], err = unescapeSegment(a[i]); err != nil {
  103. return "", err
  104. }
  105. a[i] = escapeSegToWeb(a[i], shouldAddDashMarker)
  106. }
  107. return WebPath(strings.Join(a, "/")), nil
  108. }
  109. func WebPathToUserTitle(s WebPath) (dir, display string) {
  110. dir = path.Dir(string(s))
  111. display = path.Base(string(s))
  112. if strings.HasSuffix(display, ".md") {
  113. display = strings.TrimSuffix(display, ".md")
  114. display, _ = url.PathUnescape(display)
  115. }
  116. display, _ = unescapeSegment(display)
  117. return dir, display
  118. }
  119. func WebPathToURLPath(s WebPath) string {
  120. return string(s)
  121. }
  122. func WebPathFromRequest(s string) WebPath {
  123. s = util.PathJoinRelX(s)
  124. // The old wiki code's behavior is always using %2F, instead of subdirectory.
  125. s = strings.ReplaceAll(s, "/", "%2F")
  126. return WebPath(s)
  127. }
  128. func UserTitleToWebPath(base, title string) WebPath {
  129. // TODO: no support for subdirectory, because the old wiki code's behavior is always using %2F, instead of subdirectory.
  130. // So we do not add the support for writing slashes in title at the moment.
  131. title = strings.TrimSpace(title)
  132. title = util.PathJoinRelX(base, escapeSegToWeb(title, false))
  133. if title == "" || title == "." {
  134. title = "unnamed"
  135. }
  136. return WebPath(title)
  137. }