markdown_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package markup_test
  7. import (
  8. "bytes"
  9. . "gitote/gitote/pkg/markup"
  10. "gitote/gitote/pkg/setting"
  11. "strings"
  12. "testing"
  13. "github.com/russross/blackfriday"
  14. . "github.com/smartystreets/goconvey/convey"
  15. )
  16. func Test_IsMarkdownFile(t *testing.T) {
  17. setting.Markdown.FileExtensions = strings.Split(".md,.markdown,.mdown,.mkd", ",")
  18. Convey("Detect Markdown file extension", t, func() {
  19. testCases := []struct {
  20. ext string
  21. match bool
  22. }{
  23. {".md", true},
  24. {".markdown", true},
  25. {".mdown", true},
  26. {".mkd", true},
  27. {".org", false},
  28. {".rst", false},
  29. {".asciidoc", false},
  30. }
  31. for _, tc := range testCases {
  32. So(IsMarkdownFile(tc.ext), ShouldEqual, tc.match)
  33. }
  34. })
  35. }
  36. func Test_Markdown(t *testing.T) {
  37. Convey("Rendering an issue URL", t, func() {
  38. setting.AppURL = "http://localhost:3000/"
  39. htmlFlags := 0
  40. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  41. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  42. renderer := &MarkdownRenderer{
  43. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  44. }
  45. buffer := new(bytes.Buffer)
  46. Convey("To the internal issue tracker", func() {
  47. Convey("It should render valid issue URLs", func() {
  48. testCases := []string{
  49. "http://localhost:3000/user/repo/issues/3333", "<a href=\"http://localhost:3000/user/repo/issues/3333\">#3333</a>",
  50. }
  51. for i := 0; i < len(testCases); i += 2 {
  52. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  53. line, _ := buffer.ReadString(0)
  54. So(line, ShouldEqual, testCases[i+1])
  55. }
  56. })
  57. Convey("It should render but not change non-issue URLs", func() {
  58. testCases := []string{
  59. "http://1111/2222/ssss-issues/3333?param=blah&blahh=333", "<a href=\"http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333\">http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333</a>",
  60. "http://test.com/issues/33333", "<a href=\"http://test.com/issues/33333\">http://test.com/issues/33333</a>",
  61. "http://test.com/issues/3", "<a href=\"http://test.com/issues/3\">http://test.com/issues/3</a>",
  62. "http://issues/333", "<a href=\"http://issues/333\">http://issues/333</a>",
  63. "https://issues/333", "<a href=\"https://issues/333\">https://issues/333</a>",
  64. "http://tissues/0", "<a href=\"http://tissues/0\">http://tissues/0</a>",
  65. }
  66. for i := 0; i < len(testCases); i += 2 {
  67. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  68. line, _ := buffer.ReadString(0)
  69. So(line, ShouldEqual, testCases[i+1])
  70. }
  71. })
  72. })
  73. })
  74. Convey("Rendering a commit URL", t, func() {
  75. setting.AppURL = "http://localhost:3000/"
  76. htmlFlags := 0
  77. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  78. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  79. renderer := &MarkdownRenderer{
  80. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  81. }
  82. buffer := new(bytes.Buffer)
  83. Convey("To the internal issue tracker", func() {
  84. Convey("It should correctly convert URLs", func() {
  85. testCases := []string{
  86. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994e</a></code>",
  87. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994e</a></code>",
  88. "https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", "<a href=\"https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2</a>",
  89. "https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae", "<a href=\"https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae</a>",
  90. }
  91. for i := 0; i < len(testCases); i += 2 {
  92. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  93. line, _ := buffer.ReadString(0)
  94. So(line, ShouldEqual, testCases[i+1])
  95. }
  96. })
  97. })
  98. })
  99. }