You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mdstripper_test.go 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2019 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 mdstripper
  5. import (
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestMarkdownStripper(t *testing.T) {
  11. type testItem struct {
  12. markdown string
  13. expectedText []string
  14. expectedLinks []string
  15. }
  16. list := []testItem{
  17. {
  18. `
  19. ## This is a title
  20. This is [one](link) to paradise.
  21. This **is emphasized**.
  22. This: should coallesce.
  23. ` + "```" + `
  24. This is a code block.
  25. This should not appear in the output at all.
  26. ` + "```" + `
  27. * Bullet 1
  28. * Bullet 2
  29. A HIDDEN ` + "`" + `GHOST` + "`" + ` IN THIS LINE.
  30. `,
  31. []string{
  32. "This is a title",
  33. "This is",
  34. "to paradise.",
  35. "This",
  36. "is emphasized",
  37. ".",
  38. "This: should coallesce.",
  39. "Bullet 1",
  40. "Bullet 2",
  41. "A HIDDEN",
  42. "IN THIS LINE.",
  43. },
  44. []string{
  45. "link",
  46. }},
  47. {
  48. "Simply closes: #29 yes",
  49. []string{
  50. "Simply closes: #29 yes",
  51. },
  52. []string{},
  53. },
  54. {
  55. "Simply closes: !29 yes",
  56. []string{
  57. "Simply closes: !29 yes",
  58. },
  59. []string{},
  60. },
  61. }
  62. for _, test := range list {
  63. text, links := StripMarkdown([]byte(test.markdown))
  64. rawlines := strings.Split(text, "\n")
  65. lines := make([]string, 0, len(rawlines))
  66. for _, line := range rawlines {
  67. line := strings.TrimSpace(line)
  68. if line != "" {
  69. lines = append(lines, line)
  70. }
  71. }
  72. assert.EqualValues(t, test.expectedText, lines)
  73. assert.EqualValues(t, test.expectedLinks, links)
  74. }
  75. }