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.

302 lines
11 KiB

  1. // Copyright 2017 The Gitea 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 markdown_test
  5. import (
  6. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/modules/markup"
  9. . "code.gitea.io/gitea/modules/markup/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. const AppURL = "http://localhost:3000/"
  14. const Repo = "gogits/gogs"
  15. const AppSubURL = AppURL + Repo + "/"
  16. func TestRender_StandardLinks(t *testing.T) {
  17. setting.AppURL = AppURL
  18. setting.AppSubURL = AppSubURL
  19. test := func(input, expected, expectedWiki string) {
  20. buffer := RenderString(input, setting.AppSubURL, nil)
  21. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  22. bufferWiki := RenderWiki([]byte(input), setting.AppSubURL, nil)
  23. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(bufferWiki))
  24. }
  25. googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
  26. test("<https://google.com/>", googleRendered, googleRendered)
  27. lnk := markup.URLJoin(AppSubURL, "WikiPage")
  28. lnkWiki := markup.URLJoin(AppSubURL, "wiki", "WikiPage")
  29. test("[WikiPage](WikiPage)",
  30. `<p><a href="`+lnk+`" rel="nofollow">WikiPage</a></p>`,
  31. `<p><a href="`+lnkWiki+`" rel="nofollow">WikiPage</a></p>`)
  32. }
  33. func TestRender_ShortLinks(t *testing.T) {
  34. setting.AppURL = AppURL
  35. setting.AppSubURL = AppSubURL
  36. tree := markup.URLJoin(AppSubURL, "src", "master")
  37. test := func(input, expected, expectedWiki string) {
  38. buffer := RenderString(input, tree, nil)
  39. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  40. buffer = RenderWiki([]byte(input), setting.AppSubURL, nil)
  41. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  42. }
  43. rawtree := markup.URLJoin(AppSubURL, "raw", "master")
  44. url := markup.URLJoin(tree, "Link")
  45. otherUrl := markup.URLJoin(tree, "OtherLink")
  46. imgurl := markup.URLJoin(rawtree, "Link.jpg")
  47. urlWiki := markup.URLJoin(AppSubURL, "wiki", "Link")
  48. otherUrlWiki := markup.URLJoin(AppSubURL, "wiki", "OtherLink")
  49. imgurlWiki := markup.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  50. favicon := "http://google.com/favicon.ico"
  51. test(
  52. "[[Link]]",
  53. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  54. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  55. test(
  56. "[[Link.jpg]]",
  57. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Link.jpg" title="Link.jpg"/></a></p>`,
  58. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Link.jpg" title="Link.jpg"/></a></p>`)
  59. test(
  60. "[["+favicon+"]]",
  61. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  62. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  63. test(
  64. "[[Name|Link]]",
  65. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  66. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  67. test(
  68. "[[Name|Link.jpg]]",
  69. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Name" title="Name"/></a></p>`,
  70. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Name" title="Name"/></a></p>`)
  71. test(
  72. "[[Name|Link.jpg|alt=AltName]]",
  73. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="AltName"/></a></p>`,
  74. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="AltName"/></a></p>`)
  75. test(
  76. "[[Name|Link.jpg|title=Title]]",
  77. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Title" title="Title"/></a></p>`,
  78. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Title" title="Title"/></a></p>`)
  79. test(
  80. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  81. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  82. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  83. test(
  84. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  85. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  86. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  87. test(
  88. "[[Link]] [[OtherLink]]",
  89. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a></p>`,
  90. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherUrlWiki+`" rel="nofollow">OtherLink</a></p>`)
  91. }
  92. func TestMisc_IsMarkdownFile(t *testing.T) {
  93. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  94. trueTestCases := []string{
  95. "test.md",
  96. "wow.MARKDOWN",
  97. "LOL.mDoWn",
  98. }
  99. falseTestCases := []string{
  100. "test",
  101. "abcdefg",
  102. "abcdefghijklmnopqrstuvwxyz",
  103. "test.md.test",
  104. }
  105. for _, testCase := range trueTestCases {
  106. assert.True(t, IsMarkdownFile(testCase))
  107. }
  108. for _, testCase := range falseTestCases {
  109. assert.False(t, IsMarkdownFile(testCase))
  110. }
  111. }
  112. func TestRender_Images(t *testing.T) {
  113. setting.AppURL = AppURL
  114. setting.AppSubURL = AppSubURL
  115. test := func(input, expected string) {
  116. buffer := RenderString(input, setting.AppSubURL, nil)
  117. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  118. }
  119. url := "../../.images/src/02/train.jpg"
  120. title := "Train"
  121. result := markup.URLJoin(AppSubURL, url)
  122. test(
  123. "!["+title+"]("+url+")",
  124. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"></a></p>`)
  125. test(
  126. "[["+title+"|"+url+"]]",
  127. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`" title="`+title+`"/></a></p>`)
  128. }
  129. func TestRegExp_ShortLinkPattern(t *testing.T) {
  130. trueTestCases := []string{
  131. "[[stuff]]",
  132. "[[]]",
  133. "[[stuff|title=Difficult name with spaces*!]]",
  134. }
  135. falseTestCases := []string{
  136. "test",
  137. "abcdefg",
  138. "[[]",
  139. "[[",
  140. "[]",
  141. "]]",
  142. "abcdefghijklmnopqrstuvwxyz",
  143. }
  144. for _, testCase := range trueTestCases {
  145. assert.True(t, markup.ShortLinkPattern.MatchString(testCase))
  146. }
  147. for _, testCase := range falseTestCases {
  148. assert.False(t, markup.ShortLinkPattern.MatchString(testCase))
  149. }
  150. }
  151. func testAnswers(baseURLContent, baseURLImages string) []string {
  152. return []string{
  153. `<p>Wiki! Enjoy :)</p>
  154. <ul>
  155. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  156. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  157. </ul>
  158. <p>Ideas and codes</p>
  159. <ul>
  160. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" rel="nofollow">#786</a></li>
  161. <li>Node graph editors https://github.com/ocornut/imgui/issues/306</li>
  162. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  163. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  164. </ul>
  165. `,
  166. `<h2>What is Wine Staging?</h2>
  167. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  168. <h2>Quick Links</h2>
  169. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  170. <table>
  171. <thead>
  172. <tr>
  173. <th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
  174. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  175. </tr>
  176. </thead>
  177. <tbody>
  178. <tr>
  179. <td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
  180. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. `,
  185. `<p><a href="http://www.excelsiorjet.com/" rel="nofollow">Excelsior JET</a> allows you to create native executables for Windows, Linux and Mac OS X.</p>
  186. <ol>
  187. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  188. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
  189. <li>Perform a test run by hitting the Run! button.
  190. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
  191. </ol>
  192. `,
  193. }
  194. }
  195. // Test cases without ambiguous links
  196. var sameCases = []string{
  197. // dear imgui wiki markdown extract: special wiki syntax
  198. `Wiki! Enjoy :)
  199. - [[Links, Language bindings, Engine bindings|Links]]
  200. - [[Tips]]
  201. Ideas and codes
  202. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  203. - Node graph editors https://github.com/ocornut/imgui/issues/306
  204. - [[Memory Editor|memory_editor_example]]
  205. - [[Plot var helper|plot_var_example]]`,
  206. // wine-staging wiki home extract: tables, special wiki syntax, images
  207. `## What is Wine Staging?
  208. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  209. ## Quick Links
  210. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  211. | [[images/icon-install.png]] | [[Installation]] |
  212. |--------------------------------|----------------------------------------------------------|
  213. | [[images/icon-usage.png]] | [[Usage]] |
  214. `,
  215. // libgdx wiki page: inline images with special syntax
  216. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  217. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  218. [[images/1.png]]
  219. 2. Perform a test run by hitting the Run! button.
  220. [[images/2.png]]`,
  221. }
  222. func TestTotal_RenderWiki(t *testing.T) {
  223. answers := testAnswers(markup.URLJoin(AppSubURL, "wiki/"), markup.URLJoin(AppSubURL, "wiki", "raw/"))
  224. for i := 0; i < len(sameCases); i++ {
  225. line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
  226. assert.Equal(t, answers[i], line)
  227. }
  228. testCases := []string{
  229. // Guard wiki sidebar: special syntax
  230. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  231. // rendered
  232. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  233. `,
  234. // special syntax
  235. `[[Name|Link]]`,
  236. // rendered
  237. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  238. `,
  239. }
  240. for i := 0; i < len(testCases); i += 2 {
  241. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  242. assert.Equal(t, testCases[i+1], line)
  243. }
  244. }
  245. func TestTotal_RenderString(t *testing.T) {
  246. answers := testAnswers(markup.URLJoin(AppSubURL, "src", "master/"), markup.URLJoin(AppSubURL, "raw", "master/"))
  247. for i := 0; i < len(sameCases); i++ {
  248. line := RenderString(sameCases[i], markup.URLJoin(AppSubURL, "src", "master/"), nil)
  249. assert.Equal(t, answers[i], line)
  250. }
  251. testCases := []string{}
  252. for i := 0; i < len(testCases); i += 2 {
  253. line := RenderString(testCases[i], AppSubURL, nil)
  254. assert.Equal(t, testCases[i+1], line)
  255. }
  256. }