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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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>Ideas and codes</p>
  87. <ul>
  88. <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>
  89. <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>
  90. <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
  91. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  92. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  93. </ul>
  94. `,
  95. `<h2 id="what-is-wine-staging">What is Wine Staging?</h2>
  96. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  97. <h2 id="quick-links">Quick Links</h2>
  98. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  99. <table>
  100. <thead>
  101. <tr>
  102. <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>
  103. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  104. </tr>
  105. </thead>
  106. <tbody>
  107. <tr>
  108. <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>
  109. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  110. </tr>
  111. </tbody>
  112. </table>
  113. `,
  114. `<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>
  115. <ol>
  116. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  117. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
  118. <li>Perform a test run by hitting the Run! button.
  119. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
  120. </ol>
  121. <h2 id="custom-id">More tests</h2>
  122. <p>(from <a href="https://www.markdownguide.org/extended-syntax/" rel="nofollow">https://www.markdownguide.org/extended-syntax/</a>)</p>
  123. <h3 id="definition-list">Definition list</h3>
  124. <dl>
  125. <dt>First Term</dt>
  126. <dd>This is the definition of the first term.</dd>
  127. <dt>Second Term</dt>
  128. <dd>This is one definition of the second term.</dd>
  129. <dd>This is another definition of the second term.</dd>
  130. </dl>
  131. <h3 id="footnotes">Footnotes</h3>
  132. <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>
  133. <div>
  134. <hr/>
  135. <ol>
  136. <li id="fn:1">This is the first footnote.
  137. </li>
  138. <li id="fn:bignote"><p>Here is one with multiple paragraphs and code.</p>
  139. <p>Indent paragraphs to include them in the footnote.</p>
  140. <p><code>{ my code }</code></p>
  141. <p>Add as many paragraphs as you like.</p>
  142. </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. Ideas and codes
  155. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  156. - Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786
  157. - Node graph editors https://github.com/ocornut/imgui/issues/306
  158. - [[Memory Editor|memory_editor_example]]
  159. - [[Plot var helper|plot_var_example]]`,
  160. // wine-staging wiki home extract: tables, special wiki syntax, images
  161. `## What is Wine Staging?
  162. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  163. ## Quick Links
  164. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  165. | [[images/icon-install.png]] | [[Installation]] |
  166. |--------------------------------|----------------------------------------------------------|
  167. | [[images/icon-usage.png]] | [[Usage]] |
  168. `,
  169. // libgdx wiki page: inline images with special syntax
  170. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  171. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  172. [[images/1.png]]
  173. 2. Perform a test run by hitting the Run! button.
  174. [[images/2.png]]
  175. ## More tests {#custom-id}
  176. (from https://www.markdownguide.org/extended-syntax/)
  177. ### Definition list
  178. First Term
  179. : This is the definition of the first term.
  180. Second Term
  181. : This is one definition of the second term.
  182. : This is another definition of the second term.
  183. ### Footnotes
  184. Here is a simple footnote,[^1] and here is a longer one.[^bignote]
  185. [^1]: This is the first footnote.
  186. [^bignote]: Here is one with multiple paragraphs and code.
  187. Indent paragraphs to include them in the footnote.
  188. ` + "`{ my code }`" + `
  189. Add as many paragraphs as you like.
  190. `,
  191. }
  192. func TestTotal_RenderWiki(t *testing.T) {
  193. answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
  194. for i := 0; i < len(sameCases); i++ {
  195. line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas)
  196. assert.Equal(t, answers[i], line)
  197. }
  198. testCases := []string{
  199. // Guard wiki sidebar: special syntax
  200. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  201. // rendered
  202. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  203. `,
  204. // special syntax
  205. `[[Name|Link]]`,
  206. // rendered
  207. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  208. `,
  209. }
  210. for i := 0; i < len(testCases); i += 2 {
  211. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  212. assert.Equal(t, testCases[i+1], line)
  213. }
  214. }
  215. func TestTotal_RenderString(t *testing.T) {
  216. answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/"))
  217. for i := 0; i < len(sameCases); i++ {
  218. line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas)
  219. assert.Equal(t, answers[i], line)
  220. }
  221. testCases := []string{}
  222. for i := 0; i < len(testCases); i += 2 {
  223. line := RenderString(testCases[i], AppSubURL, nil)
  224. assert.Equal(t, testCases[i+1], line)
  225. }
  226. }