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 7.5KB

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