-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmd_toc_test.go
63 lines (56 loc) · 1.17 KB
/
md_toc_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
package website
import (
"bytes"
"testing"
"github.com/verifa/website/testutil"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
)
func TestTableOfContents(t *testing.T) {
t.Parallel()
md := goldmark.New(
goldmark.WithParserOptions(
// TOC requres headings to have IDs.
parser.WithAutoHeadingID(),
),
goldmark.WithExtensions(
&tableOfContentsExt{},
),
)
type test struct {
name string
source func() []byte
exp tableOfContents
}
tests := []test{
{
name: "short",
source: func() []byte { return []byte("## Subtitle\n### Subsubtitle") },
exp: tableOfContents{
{
level: 2,
text: "Subtitle",
url: "#subtitle",
children: []*heading{
{
level: 3,
text: "Subsubtitle",
url: "#subsubtitle",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
context := parser.NewContext()
buf := bytes.Buffer{}
if err := md.Convert(tt.source(), &buf, parser.WithContext(context)); err != nil {
t.Fatalf("converting: %s", err)
}
toc := tableOfContentsFromContext(context)
testutil.AssertNoDiff(t, tt.exp, toc)
})
}
}