-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.4 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
const Metalsmith = require("metalsmith");
const async = require("async");
const path = require("path");
// template engine
const render = require("consolidate").dot.render;
const doT = require("dot");
doT.templateSettings.strip = false;
const isDev = true
const util = require("./util");
const source = util.path("template");
const dest = util.path( isDev ? './dist' : "/Users/your-project/src");
Metalsmith(source)
.metadata(require("./config")) // data source
.use(renderTemplateFiles())
.source(".") // start from template root instead of `./src` which is Metalsmith's default for `source`
.destination(dest)
.clean(isDev)
.build(function(err, files) {
if (err) {
throw err;
}
});
function renderTemplateFiles(skipInterpolation) {
return (files, metalsmith, done) => {
const keys = Object.keys(files);
const metalsmithMetadata = metalsmith.metadata();
async.each(
keys,
(file, next) => {
const str = files[file].contents.toString();
// do not attempt to render files that do not have mustaches
if (!/{{([^{}]+)}}/g.test(str)) {
return next();
}
render(str, metalsmithMetadata, (err, res) => {
if (err) {
err.message = `[${file}] ${err.message}`;
return next(err);
}
files[file].contents = new Buffer(res);
next();
});
},
done
);
};
}