1
0

markdown_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package markup_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "gogs.io/gogs/internal/conf"
  8. . "gogs.io/gogs/internal/markup"
  9. )
  10. func Test_IsMarkdownFile(t *testing.T) {
  11. oldExts := conf.Markdown.FileExtensions
  12. defer func() { conf.Markdown.FileExtensions = oldExts }()
  13. conf.Markdown.FileExtensions = strings.Split(".md,.markdown,.mdown,.mkd", ",")
  14. tests := []struct {
  15. ext string
  16. expVal bool
  17. }{
  18. {ext: ".md", expVal: true},
  19. {ext: ".markdown", expVal: true},
  20. {ext: ".mdown", expVal: true},
  21. {ext: ".mkd", expVal: true},
  22. {ext: ".org", expVal: false},
  23. {ext: ".rst", expVal: false},
  24. {ext: ".asciidoc", expVal: false},
  25. }
  26. for _, test := range tests {
  27. assert.Equal(t, test.expVal, IsMarkdownFile(test.ext))
  28. }
  29. }
  30. func Test_RawMarkdown_AutoLink(t *testing.T) {
  31. oldURL := conf.Server.ExternalURL
  32. defer func() { conf.Server.ExternalURL = oldURL }()
  33. conf.Server.ExternalURL = "http://localhost:3000/"
  34. tests := []struct {
  35. name string
  36. input string
  37. want string
  38. }{
  39. {
  40. name: "issue URL from same instance",
  41. input: "http://localhost:3000/user/repo/issues/3333",
  42. want: `<a href="http://localhost:3000/user/repo/issues/3333">#3333</a>`,
  43. },
  44. {
  45. name: "non-matching issue-like URL",
  46. input: "http://1111/2222/ssss-issues/3333?param=blah&blahh=333",
  47. want: `<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>`,
  48. },
  49. {
  50. name: "external issue URL",
  51. input: "http://test.com/issues/33333",
  52. want: `http://test.com/issues/33333`,
  53. },
  54. {
  55. name: "commit URL from same instance",
  56. input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae",
  57. want: `<code><a href="http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae">d8a994ef24</a></code>`,
  58. },
  59. {
  60. name: "commit URL with fragment from same instance",
  61. input: "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  62. want: `<code><a href="http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2">d8a994ef24</a></code>`,
  63. },
  64. {
  65. name: "external commit URL",
  66. input: "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  67. want: `https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2`,
  68. },
  69. }
  70. for _, test := range tests {
  71. t.Run(test.name, func(t *testing.T) {
  72. result := string(RawMarkdown([]byte(test.input), ""))
  73. require.NotEmpty(t, result)
  74. assert.Contains(t, result, test.want)
  75. })
  76. }
  77. }