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_test.go 11 kB

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