2
0

markdown_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/stretchr/testify/assert"
  11. "gogs.io/gogs/internal/conf"
  12. . "gogs.io/gogs/internal/markup"
  13. )
  14. func Test_IsMarkdownFile(t *testing.T) {
  15. // TODO: Refactor to accept a list of extensions
  16. conf.Markdown.FileExtensions = strings.Split(".md,.markdown,.mdown,.mkd", ",")
  17. tests := []struct {
  18. ext string
  19. expVal bool
  20. }{
  21. {ext: ".md", expVal: true},
  22. {ext: ".markdown", expVal: true},
  23. {ext: ".mdown", expVal: true},
  24. {ext: ".mkd", expVal: true},
  25. {ext: ".org", expVal: false},
  26. {ext: ".rst", expVal: false},
  27. {ext: ".asciidoc", expVal: false},
  28. }
  29. for _, test := range tests {
  30. assert.Equal(t, test.expVal, IsMarkdownFile(test.ext))
  31. }
  32. }
  33. func Test_Markdown(t *testing.T) {
  34. // TODO: Refactor to accept URL
  35. conf.Server.ExternalURL = "http://localhost:3000/"
  36. htmlFlags := 0
  37. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  38. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  39. renderer := &MarkdownRenderer{
  40. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  41. }
  42. tests := []struct {
  43. input string
  44. expVal string
  45. }{
  46. // Issue URL
  47. {input: "http://localhost:3000/user/repo/issues/3333", expVal: "<a href=\"http://localhost:3000/user/repo/issues/3333\">#3333</a>"},
  48. {input: "http://1111/2222/ssss-issues/3333?param=blah&blahh=333", expVal: "<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>"},
  49. {input: "http://test.com/issues/33333", expVal: "<a href=\"http://test.com/issues/33333\">http://test.com/issues/33333</a>"},
  50. {input: "http://test.com/issues/3", expVal: "<a href=\"http://test.com/issues/3\">http://test.com/issues/3</a>"},
  51. {input: "http://issues/333", expVal: "<a href=\"http://issues/333\">http://issues/333</a>"},
  52. {input: "https://issues/333", expVal: "<a href=\"https://issues/333\">https://issues/333</a>"},
  53. {input: "http://tissues/0", expVal: "<a href=\"http://tissues/0\">http://tissues/0</a>"},
  54. // Commit URL
  55. {input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae", expVal: " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994ef24</a></code>"},
  56. {input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", expVal: " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994ef24</a></code>"},
  57. {input: "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", expVal: "<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>"},
  58. {input: "https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae", expVal: "<a href=\"https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae</a>"},
  59. }
  60. for _, test := range tests {
  61. t.Run("", func(t *testing.T) {
  62. buf := new(bytes.Buffer)
  63. renderer.AutoLink(buf, []byte(test.input), blackfriday.LINK_TYPE_NORMAL)
  64. assert.Equal(t, test.expVal, buf.String())
  65. })
  66. }
  67. }