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.

markdown_test.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/markdown"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/util"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. const AppURL = "http://localhost:3000/"
  14. const Repo = "gogits/gogs"
  15. const AppSubURL = AppURL + Repo + "/"
  16. // these values should match the Repo const above
  17. var localMetas = map[string]string{
  18. "user": "gogits",
  19. "repo": "gogs",
  20. "repoPath": "../../../integrations/gitea-repositories-meta/user13/repo11.git/",
  21. }
  22. func TestRender_StandardLinks(t *testing.T) {
  23. setting.AppURL = AppURL
  24. setting.AppSubURL = AppSubURL
  25. test := func(input, expected, expectedWiki string) {
  26. buffer := RenderString(input, setting.AppSubURL, nil)
  27. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  28. bufferWiki := RenderWiki([]byte(input), setting.AppSubURL, nil)
  29. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(bufferWiki))
  30. }
  31. googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
  32. test("<https://google.com/>", googleRendered, googleRendered)
  33. lnk := util.URLJoin(AppSubURL, "WikiPage")
  34. lnkWiki := util.URLJoin(AppSubURL, "wiki", "WikiPage")
  35. test("[WikiPage](WikiPage)",
  36. `<p><a href="`+lnk+`" rel="nofollow">WikiPage</a></p>`,
  37. `<p><a href="`+lnkWiki+`" rel="nofollow">WikiPage</a></p>`)
  38. }
  39. func TestMisc_IsMarkdownFile(t *testing.T) {
  40. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  41. trueTestCases := []string{
  42. "test.md",
  43. "wow.MARKDOWN",
  44. "LOL.mDoWn",
  45. }
  46. falseTestCases := []string{
  47. "test",
  48. "abcdefg",
  49. "abcdefghijklmnopqrstuvwxyz",
  50. "test.md.test",
  51. }
  52. for _, testCase := range trueTestCases {
  53. assert.True(t, IsMarkdownFile(testCase))
  54. }
  55. for _, testCase := range falseTestCases {
  56. assert.False(t, IsMarkdownFile(testCase))
  57. }
  58. }
  59. func TestRender_Images(t *testing.T) {
  60. setting.AppURL = AppURL
  61. setting.AppSubURL = AppSubURL
  62. test := func(input, expected string) {
  63. buffer := RenderString(input, setting.AppSubURL, nil)
  64. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  65. }
  66. url := "../../.images/src/02/train.jpg"
  67. title := "Train"
  68. href := "https://gitea.io"
  69. result := util.URLJoin(AppSubURL, url)
  70. test(
  71. "!["+title+"]("+url+")",
  72. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
  73. test(
  74. "[["+title+"|"+url+"]]",
  75. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
  76. test(
  77. "[!["+title+"]("+url+")]("+href+")",
  78. `<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
  79. }
  80. func testAnswers(baseURLContent, baseURLImages string) []string {
  81. return []string{
  82. `<p>Wiki! Enjoy :)</p>
  83. <ul>
  84. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  85. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  86. </ul>
  87. <p>See commit <a href="http://localhost:3000/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
  88. <p>Ideas and codes</p>
  89. <ul>
  90. <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">ocornut/imgui#786</a></li>
  91. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" rel="nofollow">#786</a></li>
  92. <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
  93. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  94. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  95. </ul>
  96. `,
  97. `<h2 id="what-is-wine-staging">What is Wine Staging?</h2>
  98. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  99. <h2 id="quick-links">Quick Links</h2>
  100. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  101. <table>
  102. <thead>
  103. <tr>
  104. <th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" title="icon-install.png" alt="images/icon-install.png"/></a></th>
  105. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  106. </tr>
  107. </thead>
  108. <tbody>
  109. <tr>
  110. <td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" title="icon-usage.png" alt="images/icon-usage.png"/></a></td>
  111. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  112. </tr>
  113. </tbody>
  114. </table>
  115. `,
  116. `<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>
  117. <ol>
  118. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  119. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
  120. <li>Perform a test run by hitting the Run! button.
  121. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
  122. </ol>
  123. <h2 id="custom-id">More tests</h2>
  124. <p>(from <a href="https://www.markdownguide.org/extended-syntax/" rel="nofollow">https://www.markdownguide.org/extended-syntax/</a>)</p>
  125. <h3 id="definition-list">Definition list</h3>
  126. <dl>
  127. <dt>First Term</dt>
  128. <dd>This is the definition of the first term.</dd>
  129. <dt>Second Term</dt>
  130. <dd>This is one definition of the second term.</dd>
  131. <dd>This is another definition of the second term.</dd>
  132. </dl>
  133. <h3 id="footnotes">Footnotes</h3>
  134. <p>Here is a simple footnote,<sup id="fnref:1"><a href="#fn:1" rel="nofollow">1</a></sup> and here is a longer one.<sup id="fnref:bignote"><a href="#fn:bignote" rel="nofollow">2</a></sup></p>
  135. <div>
  136. <hr/>
  137. <ol>
  138. <li id="fn:1">This is the first footnote.</li>
  139. <li id="fn:bignote"><p>Here is one with multiple paragraphs and code.</p>
  140. <p>Indent paragraphs to include them in the footnote.</p>
  141. <p><code>{ my code }</code></p>
  142. <p>Add as many paragraphs as you like.</p></li>
  143. </ol>
  144. </div>
  145. `,
  146. }
  147. }
  148. // Test cases without ambiguous links
  149. var sameCases = []string{
  150. // dear imgui wiki markdown extract: special wiki syntax
  151. `Wiki! Enjoy :)
  152. - [[Links, Language bindings, Engine bindings|Links]]
  153. - [[Tips]]
  154. See commit 65f1bf27bc
  155. Ideas and codes
  156. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  157. - Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786
  158. - Node graph editors https://github.com/ocornut/imgui/issues/306
  159. - [[Memory Editor|memory_editor_example]]
  160. - [[Plot var helper|plot_var_example]]`,
  161. // wine-staging wiki home extract: tables, special wiki syntax, images
  162. `## What is Wine Staging?
  163. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  164. ## Quick Links
  165. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  166. | [[images/icon-install.png]] | [[Installation]] |
  167. |--------------------------------|----------------------------------------------------------|
  168. | [[images/icon-usage.png]] | [[Usage]] |
  169. `,
  170. // libgdx wiki page: inline images with special syntax
  171. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  172. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  173. [[images/1.png]]
  174. 2. Perform a test run by hitting the Run! button.
  175. [[images/2.png]]
  176. ## More tests {#custom-id}
  177. (from https://www.markdownguide.org/extended-syntax/)
  178. ### Definition list
  179. First Term
  180. : This is the definition of the first term.
  181. Second Term
  182. : This is one definition of the second term.
  183. : This is another definition of the second term.
  184. ### Footnotes
  185. Here is a simple footnote,[^1] and here is a longer one.[^bignote]
  186. [^1]: This is the first footnote.
  187. [^bignote]: Here is one with multiple paragraphs and code.
  188. Indent paragraphs to include them in the footnote.
  189. ` + "`{ my code }`" + `
  190. Add as many paragraphs as you like.
  191. `,
  192. }
  193. func TestTotal_RenderWiki(t *testing.T) {
  194. answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
  195. for i := 0; i < len(sameCases); i++ {
  196. line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas)
  197. assert.Equal(t, answers[i], line)
  198. }
  199. testCases := []string{
  200. // Guard wiki sidebar: special syntax
  201. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  202. // rendered
  203. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  204. `,
  205. // special syntax
  206. `[[Name|Link]]`,
  207. // rendered
  208. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  209. `,
  210. }
  211. for i := 0; i < len(testCases); i += 2 {
  212. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  213. assert.Equal(t, testCases[i+1], line)
  214. }
  215. }
  216. func TestTotal_RenderString(t *testing.T) {
  217. answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/"))
  218. for i := 0; i < len(sameCases); i++ {
  219. line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas)
  220. assert.Equal(t, answers[i], line)
  221. }
  222. testCases := []string{}
  223. for i := 0; i < len(testCases); i += 2 {
  224. line := RenderString(testCases[i], AppSubURL, nil)
  225. assert.Equal(t, testCases[i+1], line)
  226. }
  227. }
  228. func TestRender_RenderParagraphs(t *testing.T) {
  229. test := func(t *testing.T, str string, cnt int) {
  230. unix := []byte(str)
  231. res := string(RenderRaw(unix, "", false))
  232. assert.Equal(t, strings.Count(res, "<p"), cnt)
  233. mac := []byte(strings.ReplaceAll(str, "\n", "\r"))
  234. res = string(RenderRaw(mac, "", false))
  235. assert.Equal(t, strings.Count(res, "<p"), cnt)
  236. dos := []byte(strings.ReplaceAll(str, "\n", "\r\n"))
  237. res = string(RenderRaw(dos, "", false))
  238. assert.Equal(t, strings.Count(res, "<p"), cnt)
  239. }
  240. test(t, "\nOne\nTwo\nThree", 1)
  241. test(t, "\n\nOne\nTwo\nThree", 1)
  242. test(t, "\n\nOne\nTwo\nThree\n\n\n", 1)
  243. test(t, "A\n\nB\nC\n", 2)
  244. test(t, "A\n\n\nB\nC\n", 2)
  245. }