/
index.js
246 lines (203 loc) · 6.39 KB
/
index.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var fm = require('../')
var fs = require('fs')
var path = require('path')
var test = require('tape')
test('var fm = require("front-matter")', function (t) {
t.equal(typeof fm, 'function')
t.end()
})
test('fm(string) - parse yaml delinetead by `---`', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/dashes-seperator.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
t.ok(content.attributes, 'should have `attributes` key')
t.equal(content.attributes.title, 'Three dashes marks the spot')
t.equal(content.attributes.tags.length, 3)
t.ok(content.body, 'should have a `body` key')
t.ok(content.body.match("don't break"), 'should match body')
t.ok(content.body.match('---'), 'should match body')
t.ok(content.body.match("Also this shouldn't be a problem"),
'should match body')
t.equal(content.bodyBegin, 10)
t.ok(content.frontmatter, 'should have a `frontmatter` key')
t.ok(content.frontmatter.match('title: Three dashes marks the spot'), 'should match frontmatter')
t.ok(content.frontmatter.match('expaned-description: with some --- crazy stuff in it'), 'should match frontmatter')
t.end()
})
})
test('fm(string) - parse yaml delinetead by `= yaml =`', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/yaml-seperator.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
var meta = content.attributes
var body = content.body
t.equal(meta.title, "I couldn't think of a better name")
t.equal(meta.description, 'Just an example of using `= yaml =`')
t.ok(body.match('Plays nice with markdown syntax highlighting'),
'should match body')
t.end()
})
})
test('fm(string) - parse yaml ended by `...`', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/dots-ending.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
var meta = content.attributes
var body = content.body
t.equal(meta.title, 'Example with dots document ending')
t.equal(meta.description, 'Just an example of using `...`')
t.ok(body.match("It shouldn't break with ..."),
'should match body')
t.end()
})
})
test('fm(string) - string missing front-matter', function (t) {
var content = fm('No front matter here')
t.equal(content.body, 'No front matter here')
t.end()
})
test('fm(string) - string missing body', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/missing-body.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
t.equal(content.attributes.title, 'Three dashes marks the spot')
t.equal(content.attributes.tags.length, 3)
t.equal(content.body, '')
t.equal(content.bodyBegin, 9)
t.end()
})
})
test('fm(string) - insecure yaml', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/unsafe.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
t.throws(() => {
fm(data)
}, /YAMLException/)
t.end()
})
})
test('fm(string) - wrapped test in yaml', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/wrapped-text.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
var folded = [
'There once was a man from Darjeeling',
'Who got on a bus bound for Ealing',
' It said on the door',
' "Please don\'t spit on the floor"',
'So he carefully spat on the ceiling\n'
].join('\n')
t.equal(content.attributes['folded-text'], folded)
t.ok(content.body.match('Some crazy stuff going on up there'),
'should match body')
t.end()
})
})
test('fm(string) - strings with byte order mark', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/bom.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data)
t.equal(content.attributes.title, "Relax guy, I'm not hiding any BOMs")
t.end()
})
})
test('fm(string) - no front matter, markdown with hr', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/no-front-matter.md'),
'utf8',
function (err, data) {
t.error(err, 'read should not error')
var content = fm(data)
t.equal(content.body, data)
t.equal(content.bodyBegin, 1)
t.end()
})
})
test('fm(string, true) - complex yaml', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/complex-yaml.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
var content = fm(data, true)
t.ok(content.attributes, 'should have `attributes` key')
t.equal(content.attributes.title, 'This is a title!')
t.equal(content.attributes.contact, null)
t.equal(content.attributes.match.toString(), '/pattern/gim')
t.end()
})
})
test('fm.test(string) - yaml seperator', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/yaml-seperator.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
t.equal(fm.test(data), true)
t.end()
})
})
test('fm.test(string) - dashes seperator', function (t) {
fs.readFile(
path.resolve(__dirname, '../examples/dashes-seperator.md'),
'utf8',
function (err, data) {
t.error(err, 'read(...) should not error')
t.equal(fm.test(data), true)
t.end()
})
})
test('fm.test(string) - no front-matter', function (t) {
t.equal(fm.test('no front matter here'), false)
t.end()
})
test('Supports live updating', function (t) {
var seperator = '---'
var string = ''
for (var i = 0; i < seperator.length; i++) {
string += seperator[i]
try {
fm(string)
} catch (e) {
t.error(e)
}
}
string += '\n'
string += 'foo: bar'
var content = fm(string)
t.same(content, {
attributes: {},
body: string,
bodyBegin: 1
})
string += '\n---\n'
content = fm(string)
t.same(content, {
attributes: { foo: 'bar' },
body: '',
bodyBegin: 4,
frontmatter: 'foo: bar'
})
t.end()
})