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 9.9 kB

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