Repository navigation
Expand file tree
/
Copy pathfrontmatter_test.go
More file actions
131 lines (121 loc) · 3.35 KB
/
Copy pathfrontmatter_test.go
File metadata and controls
131 lines (121 loc) · 3.35 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bytes"
"strings"
"testing"
)
func TestMarkdownRendersRawHTML(t *testing.T) {
var buf bytes.Buffer
if err := md.Convert([]byte(`<a id="toc_70"></a>`), &buf); err != nil {
t.Fatalf("render markdown: %v", err)
}
if got := buf.String(); !strings.Contains(got, `<a id="toc_70"></a>`) {
t.Fatalf("raw HTML anchor was not preserved: %s", got)
}
}
func TestSplitFrontMatter(t *testing.T) {
cases := []struct {
name string
src string
wantYAML string
wantBody string
}{
{
name: "plain",
src: "name: web-design\ndescription: a skill\n---\n# Title\n\nbody\n",
wantYAML: "",
wantBody: "name: web-design\ndescription: a skill\n---\n# Title\n\nbody\n",
},
{
name: "yaml block",
src: "---\nname: web-design\ndescription: a skill\n---\n# Title\n\nbody\n",
wantYAML: "name: web-design\ndescription: a skill\n",
wantBody: "# Title\n\nbody\n",
},
{
name: "ellipsis terminator",
src: "---\ntitle: Hi\n...\nbody\n",
wantYAML: "title: Hi\n",
wantBody: "body\n",
},
{
name: "unterminated is untouched",
src: "---\nname: web-design\n# Title\n",
wantYAML: "",
wantBody: "---\nname: web-design\n# Title\n",
},
{
name: "horizontal rules are not front matter",
src: "---\n\n---\n\ntext\n",
wantYAML: "",
wantBody: "---\n\n---\n\ntext\n",
},
{
name: "bom prefix",
src: "\ufeff---\ntitle: Hi\n---\nbody\n",
wantYAML: "title: Hi\n",
wantBody: "body\n",
},
{
name: "crlf",
src: "---\r\ntitle: Hi\r\n---\r\nbody\r\n",
wantYAML: "title: Hi\r\n",
wantBody: "body\r\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
yaml, body := splitFrontMatter([]byte(tc.src))
if yaml != tc.wantYAML {
t.Errorf("yaml = %q, want %q", yaml, tc.wantYAML)
}
if string(body) != tc.wantBody {
t.Errorf("body = %q, want %q", body, tc.wantBody)
}
})
}
}
func TestFrontMatterValue(t *testing.T) {
yaml := "name: web-design\n\ntitle: \"Web 设计\"\nquoted: 'x'\n"
cases := map[string]string{
"name": "web-design",
"title": "Web 设计",
"quoted": "x",
"absent": "",
}
for key, want := range cases {
if got := frontMatterValue(yaml, key); got != want {
t.Errorf("frontMatterValue(%q) = %q, want %q", key, got, want)
}
}
}
func TestFrontMatterTable(t *testing.T) {
if got := frontMatterTable(""); got != "" {
t.Errorf("empty front matter should render nothing, got %q", got)
}
table := frontMatterTable("name: web-design\ndescription: a skill\n")
for _, want := range []string{
"<table>", "<tbody>",
"<th>name</th>", "<td>web-design</td>",
"<th>description</th>", "<td>a skill</td>",
} {
if !strings.Contains(table, want) {
t.Errorf("table missing %q\n got: %s", want, table)
}
}
if strings.Contains(table, "<thead>") {
t.Error("table should have no header row (matching GitHub)")
}
}
func TestFrontMatterTableEscapesAndFolds(t *testing.T) {
table := frontMatterTable("title: \"<b>hi</b>\"\ntags:\n - a\n - b\n")
if strings.Contains(table, "<b>hi</b>") {
t.Error("value HTML should be escaped")
}
if !strings.Contains(table, "<b>hi</b>") {
t.Errorf("expected escaped value, got: %s", table)
}
if !strings.Contains(table, "<td>a<br>b</td>") {
t.Errorf("list items should fold into one <br>-joined cell, got: %s", table)
}
}