-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (146 loc) · 4.31 KB
/
Copy pathindex.js
File metadata and controls
173 lines (146 loc) · 4.31 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
'use strict';
var utils = require('./utils');
/**
* Front matter parser
*/
var parser = module.exports;
/**
* Parse front matter from the given string or the `contents` in the
* given `file` and callback `next(err, file)`.
*
* If an object is passed, either `file.contents` or `file.content`
* may be used (for gulp and assemble compatibility).
*
* ```js
* // pass a string
* parser.parse('---\ntitle: foo\n---\nbar', function (err, file) {
* //=> {content: 'bar', data: {title: 'foo'}}
* });
*
* // or an object
* var file = {contents: new Buffer('---\ntitle: foo\nbar')};
* parser.parse(file, function(err, res) {
* //=> {content: 'bar', data: {title: 'foo'}}
* });
* ```
* @param {String|Object} `file` The object or string to parse.
* @param {Object|Function} `options` or `next` callback function. Options are passed to [gray-matter][].
* @param {Function} `next` callback function.
* @api public
*/
parser.parse = function matterParse(file, options, next) {
if (typeof options === 'function') {
next = options;
options = {};
}
if (typeof next !== 'function') {
throw new TypeError('expected a callback function');
}
try {
next(null, parser.parseSync(file, options));
} catch (err) {
next(err);
}
};
/**
* Parse front matter from the given string or the `contents` in the
* given `file`. If an object is passed, either `file.contents` or
* `file.content` may be used (for gulp and assemble compatibility).
*
* ```js
* // pass a string
* var res = parser.parseSync('---\ntitle: foo\n---\nbar');
*
* // or an object
* var file = {contents: new Buffer('---\ntitle: foo\nbar')};
* var res = parser.parseSync(file);
* //=> {content: 'bar', data: {title: 'foo'}}
* ```
* @param {String|Object} `file` The object or string to parse.
* @param {Object} `options` passed to [gray-matter][].
* @api public
*/
parser.parseSync = function matterParseSync(file, options) {
if (typeof file === 'string') {
file = { contents: new Buffer(file) };
} else if (!utils.isObject(file)) {
throw new TypeError('expected file to be a string or object');
}
if (file.content && !file.contents) {
file.contents = new Buffer(file.content);
}
file.data = file.data || {};
if (!utils.isValidFile(file)) {
file.content = file.contents.toString();
file.orig = file.content;
return file;
}
try {
var opts = utils.extend({strict: true}, options, file.options);
// allow files to selectively disable parsing
if (opts.frontMatter === false) {
file.content = file.contents.toString();
file.orig = file.content;
return file;
}
var str = file.contents.toString();
var parsed = utils.matter(str, opts);
file.orig = parsed.orig;
file.data = utils.merge({}, file.data, parsed.data);
file.content = utils.trim(parsed.content);
file.contents = new Buffer(file.content);
return file;
} catch (err) {
throw err;
}
};
parser.stringify = function stringify(file, options, next) {
if (typeof options === 'function') {
next = options;
options = {};
}
if (typeof next !== 'function') {
throw new TypeError('expected a callback function');
}
if (!utils.isValidFile(file) || !utils.hasYFM(file)) {
next(null, file);
return;
}
try {
next(null, parser.stringifySync(file, options));
} catch (err) {
next(err);
}
};
parser.stringifySync = function stringifySync(file, options) {
if (typeof file === 'string') {
file = { contents: new Buffer(file) };
} else if (!utils.isObject(file)) {
throw new TypeError('expected file to be a string or object');
}
if (file.content && !file.contents) {
file.contents = new Buffer(file.content);
}
if (!utils.isValidFile(file) || !utils.hasYFM(file)) {
return file;
}
try {
file.content = utils.matter.stringify(file.content, file.data.yfm);
delete file.data.yfm;
return file;
} catch (err) {
throw err;
}
};
/**
* Decorate `.parseMatter` and `.stringifyMatter` functions onto the `file` object,
* so front-matter can be parsed on-demand.
*/
parser.file = function(file, options) {
file.parseMatter = function(opts) {
return parser.parseSync(this, utils.merge({}, options, opts));
};
file.stringifyMatter = function(opts) {
return parser.stringifySync(this, utils.merge({}, options, opts));
};
};