forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontmatter.js
More file actions
221 lines (200 loc) · 5.22 KB
/
Copy pathfrontmatter.js
File metadata and controls
221 lines (200 loc) · 5.22 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
// the error handling of this module was taken from `@github-docs/frontmatter`.
// but, it's no longer maintained (in fact, the repo is just gone).
// the code is still available on npm:
// https://www.npmjs.com/package/@github-docs/frontmatter
//
// also, for some reason, it doesn't expose the error location that
// it gets from `gray-matter`, but we could use it for nicer errors.
// using `@gr2m/gray-matter` instead of `gray-matter` because the original
// library hasn't been updated in years and this fork updates the `js-yaml`
// dependency, which is much smaller and more performant.
const grayMatter = require('@gr2m/gray-matter');
const yaml = require('js-yaml');
const DEFAULT_REASON = 'Invalid frontmatter entry';
/**
* @typedef {Object} FrontmatterParseResult
* @property {Record<string, unknown>} attributes
* @property {string} body
* @property {YAMLException | null} error
*/
/**
* Parse the frontmatter and body from the content of a Markdown/MDX string.
*
* @example
* ```js
* import { frontmatter } from './scripts/utils/frontmatter'
*
* const mdxString = `---
* key: value
* list:
* - item 1
* - item 2
* ---
* howdy pardner 🤠`;
*
* const { attributes, body, error } = frontmatter(mdxString)
* console.log(attributes)
* // {
* // key: 'value',
* // list: ['item 1', 'item 2']
* // }
*
* console.log(body)
* // 'howdy pardner 🤠'
*
* console.log(error)
* // null
* ```
*
* @param {string} mdString - Full content of the MD(X) file to parse.
* @return {FrontmatterParseResult} { attributes, body, error }
*/
const frontmatter = (mdString) => {
let result;
try {
const { content, data } = grayMatter(mdString);
result = {
attributes: data,
body: content,
error: null,
};
} catch (error) {
if (error.reason.startsWith('can not read a block mapping entry;')) {
error.reason = DEFAULT_REASON;
}
result = {
attributes: {},
body: null,
error,
};
}
return result;
};
const stringifyFrontmatter = (body, attributes, options) => {
try {
return grayMatter.stringify(body, attributes, options);
} catch (error) {
return error;
}
};
/**
* Parse the frontmatter and check for required freshnessValidatedDate field on most content doc pages.
* Valid options are `never` or `YYYY-MM-DD`
*
* @example
* ```yaml
* ---
* title: This is a doc page
* freshnessValidatedDate: 2021-03-15
* ---
* ```
*
*```js
* // error.message = undefined
* ```
*
* ```yaml
* ---
* title: This is a doc page with an invalid date
* freshnessValidatedDate: 21-03-15
* ---
* ```
*
*```js
* // error.message = freshnessValidatedDate is not a valid value. Must be date format YYYY-MM-DD or `never`
* ```
*
*
*
* @param {string} mdString - Full content of the MD(X) file to parse.
* @returns error | undefined
*/
const validateFreshnessDate = (mdString) => {
let error;
const { data } = grayMatter(mdString, {
engines: {
yaml: {
parse: (string) => yaml.safeLoad(string, { schema: yaml.JSON_SCHEMA }),
},
},
});
const isValidDate = (date) => {
const regex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
return regex.test(date);
};
// freshnessValidatedDate is a required field and must be a date or `never`
if (data.freshnessValidatedDate) {
const stringDate = data.freshnessValidatedDate.toString();
if (!isValidDate(stringDate) && !stringDate.includes('never')) {
error = new Error(
'freshnessValidatedDate is not a valid value. Must be date format YYYY-MM-DD or `never`'
);
}
} else {
error = new Error('freshnessValidatedDate field missing from frontmatter');
}
return error;
};
/**
* Parse the frontmatter and check for required releaseDate field on whats-new, security-bulletins, and release-notes.
* Valid format is `YYYY-MM-DD`
*
* @example
* ```yaml
* ---
* title: This is a security bulletin
* releaseDate: '2021-03-15'
* ---
* ```
*
*```js
* // error.message = undefined
* ```
*
* ```yaml
* ---
* title: This is a security bulletin with an invalid date
* releaseDate: 21-03-15
* ---
* ```
*
*```js
* // error.message = releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes
* ```
*
*
*
* @param {string} mdString - Full content of the MD(X) file to parse.
* @returns error | undefined
*/
const validateReleaseDate = (mdString) => {
let error;
const { data } = grayMatter(mdString, {
engines: {
yaml: {
parse: (string) => yaml.safeLoad(string, { schema: yaml.JSON_SCHEMA }),
},
},
});
const isValidDate = (date) => {
const regex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
return regex.test(date);
};
// releaseDate is a required field and must be a date wrapped in single quotes
if (data.releaseDate) {
if (!isValidDate(data.releaseDate)) {
error = new Error(
"releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes"
);
}
} else {
error = new Error('releaseDate field missing from frontmatter');
}
return error;
};
module.exports = {
frontmatter,
stringifyFrontmatter,
validateFreshnessDate,
validateReleaseDate,
};