markdown_test.go 4.2 KB

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