Skip to content

Commit ed2add3

Browse files
committed
fix
1 parent 936665b commit ed2add3

File tree

6 files changed

+269
-166
lines changed

6 files changed

+269
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package markdown_test
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/markup"
11+
"code.gitea.io/gitea/modules/markup/markdown"
12+
"code.gitea.io/gitea/modules/svg"
13+
14+
"github.com/stretchr/testify/assert"
15+
"golang.org/x/text/cases"
16+
"golang.org/x/text/language"
17+
)
18+
19+
func TestAttention(t *testing.T) {
20+
defer svg.MockIcon("octicon-info")()
21+
defer svg.MockIcon("octicon-light-bulb")()
22+
defer svg.MockIcon("octicon-report")()
23+
defer svg.MockIcon("octicon-alert")()
24+
defer svg.MockIcon("octicon-stop")()
25+
26+
renderAttention := func(attention, icon string) string {
27+
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>`
28+
tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
29+
tmpl = strings.ReplaceAll(tmpl, "{icon}", icon)
30+
tmpl = strings.ReplaceAll(tmpl, "{Attention}", cases.Title(language.English).String(attention))
31+
return tmpl
32+
}
33+
34+
test := func(input, expected string) {
35+
result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
36+
assert.NoError(t, err)
37+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
38+
}
39+
40+
test(`
41+
> [!NOTE]
42+
> text
43+
`, renderAttention("note", "octicon-info")+"\n<p>text</p>\n</blockquote>")
44+
45+
test(`> [!note]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
46+
test(`> [!tip]`, renderAttention("tip", "octicon-light-bulb")+"\n</blockquote>")
47+
test(`> [!important]`, renderAttention("important", "octicon-report")+"\n</blockquote>")
48+
test(`> [!warning]`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
49+
test(`> [!caution]`, renderAttention("caution", "octicon-stop")+"\n</blockquote>")
50+
51+
// escaped by mdformat
52+
test(`> \[!NOTE\]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
53+
54+
// legacy GitHub style
55+
test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package markdown_test
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/modules/markup"
10+
"code.gitea.io/gitea/modules/markup/markdown"
11+
)
12+
13+
func BenchmarkSpecializedMarkdown(b *testing.B) {
14+
// 240856 4719 ns/op
15+
for i := 0; i < b.N; i++ {
16+
markdown.SpecializedMarkdown(&markup.RenderContext{})
17+
}
18+
}
19+
20+
func BenchmarkMarkdownRender(b *testing.B) {
21+
// 23202 50840 ns/op
22+
for i := 0; i < b.N; i++ {
23+
_, _ = markdown.RenderString(markup.NewTestRenderContext(), "https://example.com\n- a\n- b\n")
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package markdown
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/markup"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestMathRender(t *testing.T) {
16+
const nl = "\n"
17+
testcases := []struct {
18+
testcase string
19+
expected string
20+
}{
21+
{
22+
"$a$",
23+
`<p><code class="language-math is-loading">a</code></p>` + nl,
24+
},
25+
{
26+
"$ a $",
27+
`<p><code class="language-math is-loading">a</code></p>` + nl,
28+
},
29+
{
30+
"$a$ $b$",
31+
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
32+
},
33+
{
34+
`\(a\) \(b\)`,
35+
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
36+
},
37+
{
38+
`$a$.`,
39+
`<p><code class="language-math is-loading">a</code>.</p>` + nl,
40+
},
41+
{
42+
`.$a$`,
43+
`<p>.$a$</p>` + nl,
44+
},
45+
{
46+
`$a a$b b$`,
47+
`<p>$a a$b b$</p>` + nl,
48+
},
49+
{
50+
`a a$b b`,
51+
`<p>a a$b b</p>` + nl,
52+
},
53+
{
54+
`a$b $a a$b b$`,
55+
`<p>a$b $a a$b b$</p>` + nl,
56+
},
57+
{
58+
"a$x$",
59+
`<p>a$x$</p>` + nl,
60+
},
61+
{
62+
"$x$a",
63+
`<p>$x$a</p>` + nl,
64+
},
65+
{
66+
"$a$ ($b$) [$c$] {$d$}",
67+
`<p><code class="language-math is-loading">a</code> (<code class="language-math is-loading">b</code>) [$c$] {$d$}</p>` + nl,
68+
},
69+
{
70+
"$$a$$",
71+
`<pre class="code-block is-loading"><code class="chroma language-math display">a</code></pre>` + nl,
72+
},
73+
{
74+
"$$a$$ test",
75+
`<p><code class="language-math display is-loading">a</code> test</p>` + nl,
76+
},
77+
{
78+
"test $$a$$",
79+
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
80+
},
81+
{
82+
"foo $x=\\$$ bar",
83+
`<p>foo <code class="language-math is-loading">x=\$</code> bar</p>` + nl,
84+
},
85+
}
86+
87+
for _, test := range testcases {
88+
t.Run(test.testcase, func(t *testing.T) {
89+
res, err := RenderString(markup.NewTestRenderContext(), test.testcase)
90+
assert.NoError(t, err)
91+
assert.Equal(t, test.expected, string(res))
92+
})
93+
}
94+
}
95+
96+
func TestMathRenderBlockIndent(t *testing.T) {
97+
const nl = "\n"
98+
testcases := []struct {
99+
name string
100+
testcase string
101+
expected string
102+
}{
103+
{
104+
"indent-0",
105+
`
106+
\[
107+
\alpha
108+
\]
109+
`,
110+
`<pre class="code-block is-loading"><code class="chroma language-math display">
111+
\alpha
112+
</code></pre>
113+
`,
114+
},
115+
{
116+
"indent-1",
117+
`
118+
\[
119+
\alpha
120+
\]
121+
`,
122+
`<pre class="code-block is-loading"><code class="chroma language-math display">
123+
\alpha
124+
</code></pre>
125+
`,
126+
},
127+
{
128+
"indent-2",
129+
`
130+
\[
131+
\alpha
132+
\]
133+
`,
134+
`<pre class="code-block is-loading"><code class="chroma language-math display">
135+
\alpha
136+
</code></pre>
137+
`,
138+
},
139+
{
140+
"indent-0-oneline",
141+
`$$ x $$
142+
foo`,
143+
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
144+
<p>foo</p>
145+
`,
146+
},
147+
{
148+
"indent-3-oneline",
149+
` $$ x $$<SPACE>
150+
foo`,
151+
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
152+
<p>foo</p>
153+
`,
154+
},
155+
}
156+
157+
for _, test := range testcases {
158+
t.Run(test.name, func(t *testing.T) {
159+
res, err := RenderString(markup.NewTestRenderContext(), strings.ReplaceAll(test.testcase, "<SPACE>", " "))
160+
assert.NoError(t, err)
161+
assert.Equal(t, test.expected, string(res), "unexpected result for test case:\n%s", test.testcase)
162+
})
163+
}
164+
}

0 commit comments

Comments
 (0)