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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. 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 := util.URLJoin(AppSubURL, "WikiPage")
  28. lnkWiki := util.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 TestMisc_IsMarkdownFile(t *testing.T) {
  34. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  35. trueTestCases := []string{
  36. "test.md",
  37. "wow.MARKDOWN",
  38. "LOL.mDoWn",
  39. }
  40. falseTestCases := []string{
  41. "test",
  42. "abcdefg",
  43. "abcdefghijklmnopqrstuvwxyz",
  44. "test.md.test",
  45. }
  46. for _, testCase := range trueTestCases {
  47. assert.True(t, IsMarkdownFile(testCase))
  48. }
  49. for _, testCase := range falseTestCases {
  50. assert.False(t, IsMarkdownFile(testCase))
  51. }
  52. }
  53. func TestRender_Images(t *testing.T) {
  54. setting.AppURL = AppURL
  55. setting.AppSubURL = AppSubURL
  56. test := func(input, expected string) {
  57. buffer := RenderString(input, setting.AppSubURL, nil)
  58. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  59. }
  60. url := "../../.images/src/02/train.jpg"
  61. title := "Train"
  62. href := "https://gitea.io"
  63. result := util.URLJoin(AppSubURL, url)
  64. test(
  65. "!["+title+"]("+url+")",
  66. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
  67. test(
  68. "[["+title+"|"+url+"]]",
  69. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
  70. test(
  71. "[!["+title+"]("+url+")]("+href+")",
  72. `<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
  73. }
  74. func testAnswers(baseURLContent, baseURLImages string) []string {
  75. return []string{
  76. `<p>Wiki! Enjoy :)</p>
  77. <ul>
  78. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  79. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  80. </ul>
  81. <p>Ideas and codes</p>
  82. <ul>
  83. <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>
  84. <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
  85. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  86. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  87. </ul>
  88. `,
  89. `<h2>What is Wine Staging?</h2>
  90. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  91. <h2>Quick Links</h2>
  92. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  93. <table>
  94. <thead>
  95. <tr>
  96. <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>
  97. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  98. </tr>
  99. </thead>
  100. <tbody>
  101. <tr>
  102. <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>
  103. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. `,
  108. `<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>
  109. <ol>
  110. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  111. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
  112. <li>Perform a test run by hitting the Run! button.
  113. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
  114. </ol>
  115. `,
  116. }
  117. }
  118. // Test cases without ambiguous links
  119. var sameCases = []string{
  120. // dear imgui wiki markdown extract: special wiki syntax
  121. `Wiki! Enjoy :)
  122. - [[Links, Language bindings, Engine bindings|Links]]
  123. - [[Tips]]
  124. Ideas and codes
  125. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  126. - Node graph editors https://github.com/ocornut/imgui/issues/306
  127. - [[Memory Editor|memory_editor_example]]
  128. - [[Plot var helper|plot_var_example]]`,
  129. // wine-staging wiki home extract: tables, special wiki syntax, images
  130. `## What is Wine Staging?
  131. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  132. ## Quick Links
  133. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  134. | [[images/icon-install.png]] | [[Installation]] |
  135. |--------------------------------|----------------------------------------------------------|
  136. | [[images/icon-usage.png]] | [[Usage]] |
  137. `,
  138. // libgdx wiki page: inline images with special syntax
  139. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  140. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  141. [[images/1.png]]
  142. 2. Perform a test run by hitting the Run! button.
  143. [[images/2.png]]`,
  144. }
  145. func TestTotal_RenderWiki(t *testing.T) {
  146. answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
  147. for i := 0; i < len(sameCases); i++ {
  148. line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
  149. assert.Equal(t, answers[i], line)
  150. }
  151. testCases := []string{
  152. // Guard wiki sidebar: special syntax
  153. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  154. // rendered
  155. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  156. `,
  157. // special syntax
  158. `[[Name|Link]]`,
  159. // rendered
  160. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  161. `,
  162. }
  163. for i := 0; i < len(testCases); i += 2 {
  164. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  165. assert.Equal(t, testCases[i+1], line)
  166. }
  167. }
  168. func TestTotal_RenderString(t *testing.T) {
  169. answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/"))
  170. for i := 0; i < len(sameCases); i++ {
  171. line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), nil)
  172. assert.Equal(t, answers[i], line)
  173. }
  174. testCases := []string{}
  175. for i := 0; i < len(testCases); i += 2 {
  176. line := RenderString(testCases[i], AppSubURL, nil)
  177. assert.Equal(t, testCases[i+1], line)
  178. }
  179. }