Skip to content

Commit befe1f1

Browse files
committed
fix
1 parent 936665b commit befe1f1

File tree

6 files changed

+268
-166
lines changed

6 files changed

+268
-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,163 @@
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+
testcases := []struct {
98+
name string
99+
testcase string
100+
expected string
101+
}{
102+
{
103+
"indent-0",
104+
`
105+
\[
106+
\alpha
107+
\]
108+
`,
109+
`<pre class="code-block is-loading"><code class="chroma language-math display">
110+
\alpha
111+
</code></pre>
112+
`,
113+
},
114+
{
115+
"indent-1",
116+
`
117+
\[
118+
\alpha
119+
\]
120+
`,
121+
`<pre class="code-block is-loading"><code class="chroma language-math display">
122+
\alpha
123+
</code></pre>
124+
`,
125+
},
126+
{
127+
"indent-2",
128+
`
129+
\[
130+
\alpha
131+
\]
132+
`,
133+
`<pre class="code-block is-loading"><code class="chroma language-math display">
134+
\alpha
135+
</code></pre>
136+
`,
137+
},
138+
{
139+
"indent-0-oneline",
140+
`$$ x $$
141+
foo`,
142+
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
143+
<p>foo</p>
144+
`,
145+
},
146+
{
147+
"indent-3-oneline",
148+
` $$ x $$<SPACE>
149+
foo`,
150+
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
151+
<p>foo</p>
152+
`,
153+
},
154+
}
155+
156+
for _, test := range testcases {
157+
t.Run(test.name, func(t *testing.T) {
158+
res, err := RenderString(markup.NewTestRenderContext(), strings.ReplaceAll(test.testcase, "<SPACE>", " "))
159+
assert.NoError(t, err)
160+
assert.Equal(t, test.expected, string(res), "unexpected result for test case:\n%s", test.testcase)
161+
})
162+
}
163+
}

modules/markup/markdown/markdown_test.go

-131
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ import (
1313
"code.gitea.io/gitea/modules/markup"
1414
"code.gitea.io/gitea/modules/markup/markdown"
1515
"code.gitea.io/gitea/modules/setting"
16-
"code.gitea.io/gitea/modules/svg"
1716
"code.gitea.io/gitea/modules/test"
1817
"code.gitea.io/gitea/modules/util"
1918

2019
"github.com/stretchr/testify/assert"
21-
"golang.org/x/text/cases"
22-
"golang.org/x/text/language"
2320
)
2421

2522
const (
@@ -386,81 +383,6 @@ func TestColorPreview(t *testing.T) {
386383
}
387384
}
388385

389-
func TestMathBlock(t *testing.T) {
390-
const nl = "\n"
391-
testcases := []struct {
392-
testcase string
393-
expected string
394-
}{
395-
{
396-
"$a$",
397-
`<p><code class="language-math is-loading">a</code></p>` + nl,
398-
},
399-
{
400-
"$ a $",
401-
`<p><code class="language-math is-loading">a</code></p>` + nl,
402-
},
403-
{
404-
"$a$ $b$",
405-
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
406-
},
407-
{
408-
`\(a\) \(b\)`,
409-
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
410-
},
411-
{
412-
`$a$.`,
413-
`<p><code class="language-math is-loading">a</code>.</p>` + nl,
414-
},
415-
{
416-
`.$a$`,
417-
`<p>.$a$</p>` + nl,
418-
},
419-
{
420-
`$a a$b b$`,
421-
`<p>$a a$b b$</p>` + nl,
422-
},
423-
{
424-
`a a$b b`,
425-
`<p>a a$b b</p>` + nl,
426-
},
427-
{
428-
`a$b $a a$b b$`,
429-
`<p>a$b $a a$b b$</p>` + nl,
430-
},
431-
{
432-
"a$x$",
433-
`<p>a$x$</p>` + nl,
434-
},
435-
{
436-
"$x$a",
437-
`<p>$x$a</p>` + nl,
438-
},
439-
{
440-
"$$a$$",
441-
`<pre class="code-block is-loading"><code class="chroma language-math display">a</code></pre>` + nl,
442-
},
443-
{
444-
"$a$ ($b$) [$c$] {$d$}",
445-
`<p><code class="language-math is-loading">a</code> (<code class="language-math is-loading">b</code>) [$c$] {$d$}</p>` + nl,
446-
},
447-
{
448-
"$$a$$ test",
449-
`<p><code class="language-math display is-loading">a</code> test</p>` + nl,
450-
},
451-
{
452-
"test $$a$$",
453-
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
454-
},
455-
}
456-
457-
for _, test := range testcases {
458-
res, err := markdown.RenderString(markup.NewTestRenderContext(), test.testcase)
459-
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
460-
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
461-
}
462-
}
463-
464386
func TestTaskList(t *testing.T) {
465387
testcases := []struct {
466388
testcase string
@@ -551,56 +473,3 @@ space</p>
551473
assert.NoError(t, err)
552474
assert.Equal(t, expected, string(result))
553475
}
554-
555-
func TestAttention(t *testing.T) {
556-
defer svg.MockIcon("octicon-info")()
557-
defer svg.MockIcon("octicon-light-bulb")()
558-
defer svg.MockIcon("octicon-report")()
559-
defer svg.MockIcon("octicon-alert")()
560-
defer svg.MockIcon("octicon-stop")()
561-
562-
renderAttention := func(attention, icon string) string {
563-
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>`
564-
tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
565-
tmpl = strings.ReplaceAll(tmpl, "{icon}", icon)
566-
tmpl = strings.ReplaceAll(tmpl, "{Attention}", cases.Title(language.English).String(attention))
567-
return tmpl
568-
}
569-
570-
test := func(input, expected string) {
571-
result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
572-
assert.NoError(t, err)
573-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
574-
}
575-
576-
test(`
577-
> [!NOTE]
578-
> text
579-
`, renderAttention("note", "octicon-info")+"\n<p>text</p>\n</blockquote>")
580-
581-
test(`> [!note]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
582-
test(`> [!tip]`, renderAttention("tip", "octicon-light-bulb")+"\n</blockquote>")
583-
test(`> [!important]`, renderAttention("important", "octicon-report")+"\n</blockquote>")
584-
test(`> [!warning]`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
585-
test(`> [!caution]`, renderAttention("caution", "octicon-stop")+"\n</blockquote>")
586-
587-
// escaped by mdformat
588-
test(`> \[!NOTE\]`, renderAttention("note", "octicon-info")+"\n</blockquote>")
589-
590-
// legacy GitHub style
591-
test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
592-
}
593-
594-
func BenchmarkSpecializedMarkdown(b *testing.B) {
595-
// 240856 4719 ns/op
596-
for i := 0; i < b.N; i++ {
597-
markdown.SpecializedMarkdown(&markup.RenderContext{})
598-
}
599-
}
600-
601-
func BenchmarkMarkdownRender(b *testing.B) {
602-
// 23202 50840 ns/op
603-
for i := 0; i < b.N; i++ {
604-
_, _ = markdown.RenderString(markup.NewTestRenderContext(), "https://example.com\n- a\n- b\n")
605-
}
606-
}

0 commit comments

Comments
 (0)