本站源代码
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.

297 lines
9.8KB

  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.
  139. </li>
  140. <li id="fn:bignote"><p>Here is one with multiple paragraphs and code.</p>
  141. <p>Indent paragraphs to include them in the footnote.</p>
  142. <p><code>{ my code }</code></p>
  143. <p>Add as many paragraphs as you like.</p>
  144. </li>
  145. </ol>
  146. </div>
  147. `,
  148. }
  149. }
  150. // Test cases without ambiguous links
  151. var sameCases = []string{
  152. // dear imgui wiki markdown extract: special wiki syntax
  153. `Wiki! Enjoy :)
  154. - [[Links, Language bindings, Engine bindings|Links]]
  155. - [[Tips]]
  156. See commit 65f1bf27bc
  157. Ideas and codes
  158. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  159. - Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786
  160. - Node graph editors https://github.com/ocornut/imgui/issues/306
  161. - [[Memory Editor|memory_editor_example]]
  162. - [[Plot var helper|plot_var_example]]`,
  163. // wine-staging wiki home extract: tables, special wiki syntax, images
  164. `## What is Wine Staging?
  165. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  166. ## Quick Links
  167. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  168. | [[images/icon-install.png]] | [[Installation]] |
  169. |--------------------------------|----------------------------------------------------------|
  170. | [[images/icon-usage.png]] | [[Usage]] |
  171. `,
  172. // libgdx wiki page: inline images with special syntax
  173. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  174. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  175. [[images/1.png]]
  176. 2. Perform a test run by hitting the Run! button.
  177. [[images/2.png]]
  178. ## More tests {#custom-id}
  179. (from https://www.markdownguide.org/extended-syntax/)
  180. ### Definition list
  181. First Term
  182. : This is the definition of the first term.
  183. Second Term
  184. : This is one definition of the second term.
  185. : This is another definition of the second term.
  186. ### Footnotes
  187. Here is a simple footnote,[^1] and here is a longer one.[^bignote]
  188. [^1]: This is the first footnote.
  189. [^bignote]: Here is one with multiple paragraphs and code.
  190. Indent paragraphs to include them in the footnote.
  191. ` + "`{ my code }`" + `
  192. Add as many paragraphs as you like.
  193. `,
  194. }
  195. func TestTotal_RenderWiki(t *testing.T) {
  196. answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
  197. for i := 0; i < len(sameCases); i++ {
  198. line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas)
  199. assert.Equal(t, answers[i], line)
  200. }
  201. testCases := []string{
  202. // Guard wiki sidebar: special syntax
  203. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  204. // rendered
  205. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  206. `,
  207. // special syntax
  208. `[[Name|Link]]`,
  209. // rendered
  210. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  211. `,
  212. }
  213. for i := 0; i < len(testCases); i += 2 {
  214. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  215. assert.Equal(t, testCases[i+1], line)
  216. }
  217. }
  218. func TestTotal_RenderString(t *testing.T) {
  219. answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/"))
  220. for i := 0; i < len(sameCases); i++ {
  221. line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas)
  222. assert.Equal(t, answers[i], line)
  223. }
  224. testCases := []string{}
  225. for i := 0; i < len(testCases); i += 2 {
  226. line := RenderString(testCases[i], AppSubURL, nil)
  227. assert.Equal(t, testCases[i+1], line)
  228. }
  229. }
上海开阖软件有限公司 沪ICP备12045867号-1