gitea源码

markdown_attention_test.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markdown_test
  4. import (
  5. "strings"
  6. "testing"
  7. "code.gitea.io/gitea/modules/markup"
  8. "code.gitea.io/gitea/modules/markup/markdown"
  9. "code.gitea.io/gitea/modules/svg"
  10. "github.com/stretchr/testify/assert"
  11. "golang.org/x/text/cases"
  12. "golang.org/x/text/language"
  13. )
  14. func TestAttention(t *testing.T) {
  15. defer svg.MockIcon("octicon-info")()
  16. defer svg.MockIcon("octicon-light-bulb")()
  17. defer svg.MockIcon("octicon-report")()
  18. defer svg.MockIcon("octicon-alert")()
  19. defer svg.MockIcon("octicon-stop")()
  20. test := func(input, expected string) {
  21. result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
  22. assert.NoError(t, err)
  23. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
  24. }
  25. renderAttention := func(attention, icon string) string {
  26. tmpl := `<blockquote class="attention-header attention-{attention}"><p><svg class="attention-icon attention-{attention} svg {icon}" width="16" height="16"></svg><strong class="attention-{attention}">{Attention}</strong></p>`
  27. tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
  28. tmpl = strings.ReplaceAll(tmpl, "{icon}", icon)
  29. tmpl = strings.ReplaceAll(tmpl, "{Attention}", cases.Title(language.English).String(attention))
  30. return tmpl
  31. }
  32. test(`
  33. > [!NOTE]
  34. > text
  35. `, renderAttention("note", "octicon-info")+"\n<p>text</p>\n</blockquote>")
  36. test(`> [!note]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
  37. test(`> [!tip]`, renderAttention("tip", "octicon-light-bulb")+"\n</blockquote>")
  38. test(`> [!important]`, renderAttention("important", "octicon-report")+"\n</blockquote>")
  39. test(`> [!warning]`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
  40. test(`> [!caution]`, renderAttention("caution", "octicon-stop")+"\n</blockquote>")
  41. // escaped by mdformat
  42. test(`> \[!NOTE\]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
  43. // legacy GitHub style
  44. test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
  45. // edge case (it used to cause panic)
  46. test(">\ntext", "<blockquote>\n</blockquote>\n<p>text</p>")
  47. }