forked from litao91/goldmark-mathjax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathjax_test.go
75 lines (64 loc) · 1.29 KB
/
mathjax_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mathjax
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/yuin/goldmark"
"github.com/stretchr/testify/assert"
)
type mathJaxTestCase struct {
d string // test description
in string // input markdown source
out string // expected output html
}
func TestMathJax(t *testing.T) {
tests := []mathJaxTestCase{
{
d: "plain text",
in: "foo",
out: `<p>foo</p>`,
},
{
d: "bold",
in: "**foo**",
out: `<p><strong>foo</strong></p>`,
},
{
d: "math inline",
in: "$1+2$",
out: `<p><span class="math inline">\(1+2\)</span></p>`,
},
{
d: "math display",
in: "$$\n1+2\n$$",
out: `<p><span class="math display">\[1+2
\]</span></p>`,
},
{
// this input previously triggered a panic in block.go
d: "list-begin",
in: "*foo\n ",
out: "<p>*foo</p>",
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("%d: %s", i, tc.d), func(t *testing.T) {
out, err := renderMarkdown([]byte(tc.in))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.out, strings.TrimSpace(string(out)))
})
}
}
func renderMarkdown(src []byte) ([]byte, error) {
md := goldmark.New(
goldmark.WithExtensions(MathJax),
)
var buf bytes.Buffer
if err := md.Convert(src, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}