UNPKG

1.31 kBJavaScriptView Raw
1'use strict';
2
3var typeOf = require('kind-of');
4var stringify = require('./stringify');
5var utils = require('./utils');
6
7/**
8 * Normalize the given value to ensure an object is returned
9 * with the expected properties.
10 */
11
12module.exports = function(file) {
13 if (typeOf(file) !== 'object') {
14 file = { content: file };
15 }
16
17 if (typeOf(file.data) !== 'object') {
18 file.data = {};
19 }
20
21 if (file.content == null) {
22 file.content = file.contents;
23 }
24
25 var orig = utils.toBuffer(file.content);
26 Object.defineProperty(file, 'orig', {
27 configurable: true,
28 enumerable: false,
29 writable: true,
30 value: orig
31 });
32
33 Object.defineProperty(file, 'matter', {
34 configurable: true,
35 enumerable: false,
36 writable: true,
37 value: file.matter || ''
38 });
39
40 Object.defineProperty(file, 'language', {
41 configurable: true,
42 enumerable: false,
43 writable: true,
44 value: file.language || ''
45 });
46
47 Object.defineProperty(file, 'stringify', {
48 configurable: true,
49 enumerable: false,
50 writable: true,
51 value: function(data, options) {
52 if (options && options.language) {
53 file.language = options.language;
54 }
55 return stringify(file, data, options);
56 }
57 });
58
59 file.content = utils.toString(file.content);
60 file.excerpt = '';
61 return file;
62};