forked from hackadashery/hackadashery.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (121 loc) · 3.22 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
/**
* Dependencies
*/
var consolidate = require('consolidate');
var debug = require('debug')('metalsmith-in-place');
var each = require('async').each;
var extend = require('extend');
var omit = require('lodash.omit');
var path = require('path');
/**
* Helpers
*/
var readPartials = require('./helpers/read-partials');
var check = require('./helpers/check');
/**
* Expose `plugin`
*/
module.exports = plugin;
/**
* Settings
*
* Options supported by metalsmith-in-place
*/
var settings = ['engine', 'partials', 'pattern', 'rename'];
/**
* Metalsmith plugin for in-place templating.
*
* @param {String or Object} options
* @property {String} engine
* @property {String} partials (optional)
* @property {String} pattern (optional)
* @property {Boolean} rename (optional)
* @return {Function}
*/
function plugin(opts){
/**
* Init
*/
opts = opts || {};
// Accept string option to specify the engine
if (typeof opts === 'string') {
opts = {engine: opts};
}
// An engine should be specified
if (!opts.engine) {
throw new Error('"engine" option required');
}
// Throw an error for unsupported engines or typos
if (!consolidate[opts.engine]) {
throw new Error('Unknown template engine: "' + opts.engine + '"');
}
// Map options to local variables
var engine = opts.engine;
var partials = opts.partials;
var pattern = opts.pattern;
var rename = opts.rename;
// Move all unrecognised options to params
var params = omit(opts, settings);
/**
* Main plugin function
*/
return function(files, metalsmith, done){
var metadata = metalsmith.metadata();
var matches = {};
/**
* Process any partials and pass them to consolidate as a partials object
*/
if (partials) {
if (typeof partials === 'string') {
params.partials = readPartials(partials, pattern, metalsmith);
} else {
params.partials = partials;
}
}
/**
* Stringify files that pass the check, and store in matches
*/
Object.keys(files).forEach(function(file){
if (!check(files, file, pattern)) {
return;
}
debug('stringifying file: %s', file);
var data = files[file];
data.contents = data.contents.toString();
matches[file] = data;
});
/**
* Render files
*/
function convert(file, done){
debug('converting file: %s', file);
var data = files[file];
// Deep clone params (by passing 'true')
var clonedParams = extend(true, {}, params);
var clone = extend({}, clonedParams, metadata, data);
var str = clone.contents;
var render = consolidate[engine].render;
// Rename file if necessary
var fileInfo;
if (rename) {
delete files[file];
fileInfo = path.parse(file);
file = path.join(fileInfo.dir, fileInfo.name + '.html');
debug('renamed file to: %s', file);
}
render(str, clone, function(err, str){
if (err) {
return done(err);
}
data.contents = new Buffer(str);
debug('converted file: %s', file);
files[file] = data;
done();
});
}
/**
* Render all matched files
*/
each(Object.keys(matches), convert, done);
};
}