markdown_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package markup_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gogs.io/gogs/internal/conf"
  7. . "gogs.io/gogs/internal/markup"
  8. )
  9. func Test_IsMarkdownFile(t *testing.T) {
  10. oldExts := conf.Markdown.FileExtensions
  11. defer func() { conf.Markdown.FileExtensions = oldExts }()
  12. conf.Markdown.FileExtensions = strings.Split(".md,.markdown,.mdown,.mkd", ",")
  13. tests := []struct {
  14. ext string
  15. expVal bool
  16. }{
  17. {ext: ".md", expVal: true},
  18. {ext: ".markdown", expVal: true},
  19. {ext: ".mdown", expVal: true},
  20. {ext: ".mkd", expVal: true},
  21. {ext: ".org", expVal: false},
  22. {ext: ".rst", expVal: false},
  23. {ext: ".asciidoc", expVal: false},
  24. }
  25. for _, test := range tests {
  26. assert.Equal(t, test.expVal, IsMarkdownFile(test.ext))
  27. }
  28. }
  29. func Test_RawMarkdown_AutoLink(t *testing.T) {
  30. oldURL := conf.Server.ExternalURL
  31. defer func() { conf.Server.ExternalURL = oldURL }()
  32. conf.Server.ExternalURL = "http://localhost:3000/"
  33. tests := []struct {
  34. name string
  35. input string
  36. want string
  37. }{
  38. {
  39. name: "issue URL from same instance",
  40. input: "http://localhost:3000/user/repo/issues/3333",
  41. want: "<p><a href=\"http://localhost:3000/user/repo/issues/3333\">#3333</a></p>\n",
  42. },
  43. {
  44. name: "non-matching issue-like URL",
  45. input: "http://1111/2222/ssss-issues/3333?param=blah&blahh=333",
  46. want: "<p><a href=\"http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333\">http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333</a></p>\n",
  47. },
  48. {
  49. name: "external issue URL",
  50. input: "http://test.com/issues/33333",
  51. want: "<p><a href=\"http://test.com/issues/33333\">http://test.com/issues/33333</a></p>\n",
  52. },
  53. {
  54. name: "commit URL from same instance",
  55. input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae",
  56. want: "<p> <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994ef24</a></code></p>\n",
  57. },
  58. {
  59. name: "commit URL with fragment from same instance",
  60. input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  61. want: "<p> <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994ef24</a></code></p>\n",
  62. },
  63. {
  64. name: "external commit URL",
  65. input: "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  66. want: "<p><a href=\"https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2</a></p>\n",
  67. },
  68. {
  69. name: "issue URL with single digit",
  70. input: "http://test.com/issues/3",
  71. want: "<p><a href=\"http://test.com/issues/3\">http://test.com/issues/3</a></p>\n",
  72. },
  73. {
  74. name: "host without dot in issue-like URL",
  75. input: "http://issues/333",
  76. want: "<p><a href=\"http://issues/333\">http://issues/333</a></p>\n",
  77. },
  78. {
  79. name: "https host without dot in issue-like URL",
  80. input: "https://issues/333",
  81. want: "<p><a href=\"https://issues/333\">https://issues/333</a></p>\n",
  82. },
  83. {
  84. name: "host without dot resembling keyword",
  85. input: "http://tissues/0",
  86. want: "<p><a href=\"http://tissues/0\">http://tissues/0</a></p>\n",
  87. },
  88. {
  89. name: "https commit-like URL without dot",
  90. input: "https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae",
  91. want: "<p><a href=\"https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae</a></p>\n",
  92. },
  93. }
  94. for _, test := range tests {
  95. t.Run(test.name, func(t *testing.T) {
  96. got := string(RawMarkdown([]byte(test.input), ""))
  97. assert.Equal(t, test.want, got)
  98. })
  99. }
  100. t.Run("cross-repo issue URL from same instance", func(t *testing.T) {
  101. got := string(RawMarkdown([]byte("http://localhost:3000/other/repo/issues/42"), "/user/myrepo"))
  102. assert.Equal(t, "<p><a href=\"http://localhost:3000/other/repo/issues/42\">other/repo#42</a></p>\n", got)
  103. })
  104. t.Run("same-repo issue URL with fragment", func(t *testing.T) {
  105. got := string(RawMarkdown([]byte("http://localhost:3000/user/myrepo/issues/42#issuecomment-1"), "/user/myrepo"))
  106. assert.Equal(t, "<p><a href=\"http://localhost:3000/user/myrepo/issues/42#issuecomment-1\">#42</a></p>\n", got)
  107. })
  108. }
  109. func Test_RawMarkdown_LinkRewriting(t *testing.T) {
  110. tests := []struct {
  111. name string
  112. input string
  113. urlPrefix string
  114. want string
  115. }{
  116. {
  117. name: "relative link with path-only prefix",
  118. input: "[text](other-file.md)",
  119. urlPrefix: "/user/repo/src/branch/main",
  120. want: "<p><a href=\"/user/repo/src/branch/main/other-file.md\">text</a></p>\n",
  121. },
  122. {
  123. name: "relative link with absolute URL prefix",
  124. input: "[text](other-file.md)",
  125. urlPrefix: "http://localhost:3000/user/repo/src/branch/main",
  126. want: "<p><a href=\"http://localhost:3000/user/repo/src/branch/main/other-file.md\">text</a></p>\n",
  127. },
  128. {
  129. name: "absolute link not rewritten",
  130. input: "[text](https://example.com/page)",
  131. urlPrefix: "/user/repo/src/branch/main",
  132. want: "<p><a href=\"https://example.com/page\">text</a></p>\n",
  133. },
  134. {
  135. name: "anchor-only link not rewritten",
  136. input: "[text](#section)",
  137. urlPrefix: "/user/repo/src/branch/main",
  138. want: "<p><a href=\"#section\">text</a></p>\n",
  139. },
  140. }
  141. for _, test := range tests {
  142. t.Run(test.name, func(t *testing.T) {
  143. got := string(RawMarkdown([]byte(test.input), test.urlPrefix))
  144. assert.Equal(t, test.want, got)
  145. })
  146. }
  147. }
  148. func Test_RawMarkdown_HTMLPassthrough(t *testing.T) {
  149. tests := []struct {
  150. name string
  151. input string
  152. want string
  153. }{
  154. {
  155. name: "inline HTML tags are stripped",
  156. input: "Hello <em>world</em>",
  157. want: "<p>Hello <!-- raw HTML omitted -->world<!-- raw HTML omitted --></p>\n",
  158. },
  159. {
  160. name: "block HTML tags are stripped",
  161. input: "<div>content</div>",
  162. want: "<!-- raw HTML omitted -->\n",
  163. },
  164. }
  165. for _, test := range tests {
  166. t.Run(test.name, func(t *testing.T) {
  167. got := string(RawMarkdown([]byte(test.input), ""))
  168. assert.Equal(t, test.want, got)
  169. })
  170. }
  171. }