This repository was archived by the owner on Sep 9, 2022. It is now read-only.
forked from github/docs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-frontmatter.js
More file actions
223 lines (204 loc) · 5.97 KB
/
Copy pathread-frontmatter.js
File metadata and controls
223 lines (204 loc) · 5.97 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
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
import parse from '../../lib/read-frontmatter.js'
const filepath = 'path/to/file.md'
const fixture1 = `---
title: Hello, World
meaning_of_life: 42
---
I am content.
`
describe('frontmatter', () => {
it('parses frontmatter and content in a given string (no options required)', () => {
const { data, content, errors } = parse(fixture1)
expect(data.title).toBe('Hello, World')
expect(data.meaning_of_life).toBe(42)
expect(content.trim()).toBe('I am content.')
expect(errors.length).toBe(0)
})
describe('frontmatter.stringify', () => {
it('is exported', () => {
expect(typeof parse.stringify).toBe('function')
})
})
describe('YML parsing errors', () => {
it('creates errors if YML has an unescaped quote', () => {
const fixture = `---
intro: 'I've got an unescaped quote'
---
I am content.
`
const { errors } = parse(fixture, { filepath })
expect(errors.length).toBe(1)
const expectedError = {
filepath: 'path/to/file.md',
message: 'YML parsing error!',
reason: 'invalid frontmatter entry',
}
expect(errors[0]).toEqual(expectedError)
})
it('creates errors if YML has incorrect indentation', () => {
const fixture = `---
title: Hello, World
intro: 'I have a bad leading space'
---
I am content.
`
const { errors } = parse(fixture, { filepath })
expect(errors.length).toBe(1)
const expectedError = {
filepath: 'path/to/file.md',
message: 'YML parsing error!',
reason: 'bad indentation of a mapping entry',
}
expect(errors[0]).toEqual(expectedError)
})
})
describe('schema', () => {
it('is optional', () => {
const schema = {
properties: {
title: {
type: 'string',
},
meaning_of_life: {
type: 'number',
},
},
}
const { data, content, errors } = parse(fixture1, { schema })
expect(data.title).toBe('Hello, World')
expect(data.meaning_of_life).toBe(42)
expect(content.trim()).toBe('I am content.')
expect(errors.length).toBe(0)
})
it('creates errors if frontmatter does not conform to schema', () => {
const schema = {
properties: {
meaning_of_life: {
type: 'number',
minimum: 50,
},
},
}
const { data, content, errors } = parse(fixture1, { schema })
expect(data.title).toBe('Hello, World')
expect(data.meaning_of_life).toBe(42)
expect(content.trim()).toBe('I am content.')
expect(errors.length).toBe(1)
const expectedError = {
attribute: 'minimum',
property: 'meaning_of_life',
expected: 50,
actual: 42,
message: 'must be greater than or equal to 50',
}
expect(errors[0]).toEqual(expectedError)
})
it('creates errors if required frontmatter is not present', () => {
const schema = {
properties: {
yet_another_key: {
type: 'string',
required: true,
},
},
}
const { errors } = parse(fixture1, { schema })
expect(errors.length).toBe(1)
const expectedError = {
attribute: 'required',
property: 'yet_another_key',
expected: true,
actual: undefined,
message: 'is required',
}
expect(errors[0]).toEqual(expectedError)
})
})
describe('validateKeyNames', () => {
const schema = {
properties: {
age: {
type: 'number',
},
},
}
it('creates errors for undocumented keys if `validateKeyNames` is true', () => {
const { errors } = parse(fixture1, { schema, validateKeyNames: true, filepath })
expect(errors.length).toBe(2)
const expectedErrors = [
{
property: 'title',
message: 'not allowed. Allowed properties are: age',
filepath: 'path/to/file.md',
},
{
property: 'meaning_of_life',
message: 'not allowed. Allowed properties are: age',
filepath: 'path/to/file.md',
},
]
expect(errors).toEqual(expectedErrors)
})
it('does not create errors for undocumented keys if `validateKeyNames` is false', () => {
const { errors } = parse(fixture1, { schema, validateKeyNames: false })
expect(errors.length).toBe(0)
})
})
describe('validateKeyOrder', () => {
it('creates errors if `validateKeyOrder` is true and keys are not in order', () => {
const schema = {
properties: {
meaning_of_life: {
type: 'number',
},
title: {
type: 'string',
},
},
}
const { errors } = parse(fixture1, { schema, validateKeyOrder: true, filepath })
const expectedErrors = [
{
property: 'keys',
message:
'keys must be in order. Current: title,meaning_of_life; Expected: meaning_of_life,title',
filepath: 'path/to/file.md',
},
]
expect(errors).toEqual(expectedErrors)
})
it('does not create errors if `validateKeyOrder` is true and keys are in order', () => {
const schema = {
properties: {
title: {
type: 'string',
},
meaning_of_life: {
type: 'number',
},
},
}
const { errors } = parse(fixture1, { schema, validateKeyOrder: true })
expect(errors.length).toBe(0)
})
it('does not create errors if `validateKeyOrder` is true and expected keys are in order', () => {
const schema = {
properties: {
title: {
type: 'string',
required: true,
},
yet_another_key: {
type: 'string',
},
meaning_of_life: {
type: 'number',
required: true,
},
},
}
const { errors } = parse(fixture1, { schema, validateKeyOrder: true })
expect(errors.length).toBe(0)
})
})
})