-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_test.go
229 lines (198 loc) · 5.82 KB
/
parse_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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var rawContent = `public:: true
title:: Blog article: hello & world
- First paragraph
- Second paragraph
- bullet point
`
var textPart = `- First paragraph
- Second paragraph
- bullet point
`
func TestParseAttributes(t *testing.T) {
attributes := parseAttributes(rawContent)
assert.Equal(t, map[string]string{
"public": "true",
"title": "Blog article: hello & world",
}, attributes)
}
func TestStripAttributes(t *testing.T) {
textWithoutAttributes := stripAttributes(rawContent)
require.Equal(t, textPart, textWithoutAttributes)
}
func TestParseAssets(t *testing.T) {
t.Run("extracts relative images", func(t *testing.T) {
content := "- ![hello world](../assets/image.png)"
result := parseAssets(content)
require.Equal(t, []string{"../assets/image.png"}, result)
})
t.Run("ignores absolute images", func(t *testing.T) {
content := "- ![hello world](http://example.com/assets/image.png)"
result := parseAssets(content)
require.Equal(t, 0, len(result))
})
// TODO if first content line contains only image, move it to an image attribute (based on some config)
}
func TestParsePage(t *testing.T) {
t.Run("adds filename as title if it is missing", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "",
}
result := parsePage(testPage)
require.Equal(t, "name with space", result.pc.attributes["title"])
})
t.Run("uses title page property if present", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "title:: title from page prop\n",
}
result := parsePage(testPage)
require.Equal(t, "title from page prop", result.pc.attributes["title"])
})
t.Run("uses unescaped filename title", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/Blog idea%3A All good laws that EU brought.md",
content: "",
}
result := parsePage(testPage)
require.Equal(t, "Blog idea: All good laws that EU brought", result.pc.attributes["title"])
})
t.Run("uses sanitized filename as the exportFileName", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/Blog idea%3A All good laws that EU brought.md",
content: "",
}
result := parsePage(testPage)
require.Equal(t, "blog-idea-all-good-laws-that-eu-brought.md", result.exportFilename)
})
t.Run("uses slug as the exportFileName", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "slug:: slug-name\n",
}
result := parsePage(testPage)
require.Equal(t, "slug-name.md", result.exportFilename)
})
t.Run("uses date and slug as the exportFileName", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "slug:: slug-name\ndate:: 2023-07-29\n",
}
result := parsePage(testPage)
require.Equal(t, "2023-07-29-slug-name.md", result.exportFilename)
})
t.Run("keeps slug if present", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "slug:: slug-name\n",
}
result := parsePage(testPage)
require.Equal(t, "slug-name", result.pc.attributes["slug"])
})
t.Run("uses exportFilename as slug", func(t *testing.T) {
testPage := textFile{
absoluteFSPath: "/name with space.md",
content: "",
}
result := parsePage(testPage)
require.Equal(t, "name-with-space", result.pc.attributes["slug"])
})
}
func TestParseContent(t *testing.T) {
t.Run("removes square brackets from date", func(t *testing.T) {
result := parseContent("date:: [[2023-07-30]]\n")
require.Equal(t, "", result.content)
require.Equal(t, "2023-07-30", result.attributes["date"])
})
t.Run("parses page with only one attribute", func(t *testing.T) {
result := parseContent("public:: true\n")
require.Equal(t, "", result.content)
require.Equal(t, "true", result.attributes["public"])
})
t.Run("trims attribute names and values", func(t *testing.T) {
result := parseContent(" public:: true\n")
require.Equal(t, "", result.content)
require.Equal(t, "true", result.attributes["public"])
})
t.Run("parses page with one line", func(t *testing.T) {
result := parseContent("- a\n")
require.Equal(t, "\na\n", result.content)
require.Empty(t, result.attributes)
})
t.Run("removes dashes with no text after them", func(t *testing.T) {
result := parseContent("-\n\t- \n\t\t-")
require.Equal(t, "\n\n", result.content)
})
t.Run("removes dashes from the text", func(t *testing.T) {
result := parseContent("-\n- hello")
require.Equal(t, "\n\nhello", result.content)
})
t.Run("turns second level bullet points into first level", func(t *testing.T) {
result := parseContent("\t- hello\n\t- world")
require.Equal(t, "- hello\n- world", result.content)
})
t.Run("removes one tab from multi-level bullet points", func(t *testing.T) {
result := parseContent("\t\t- hello\n\t\t\t- world")
require.Equal(t, "\t- hello\n\t\t- world", result.content)
})
t.Run("handles fenced blocks in second-level bullet points", func(t *testing.T) {
result := parseContent(`
- ## If statement
- ~~~bash
if [-a file];then
...
else
...
fi
~~~
`)
require.Equal(t, `
## If statement
- ~~~bash
if [-a file];then
...
else
...
fi
~~~
`, result.content)
})
t.Run("removes tabs from all subsequent lines of a bullet point", func(t *testing.T) {
result := parseContent(`
- ~~~ts
const hello = "world";
~~~
- single line
- multiple
lines
in
one`)
require.Equal(t, `
~~~ts
const hello = "world";
~~~
single line
multiple
lines
in
one`, result.content)
})
t.Run("removes all content after - private", func(t *testing.T) {
result := parseContent(`
- single line
- private
- multiple
lines
in
one`)
require.Equal(t, `
single line
`, result.content)
})
}