-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
43 lines (34 loc) · 1.08 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
var path = require('path');
var fs = require('fs');
var handlebars = require('handlebars');
require('./helpers')(hexo);
function handlebarsRenderer(data, locals) {
var template = handlebars.compile(data.text),
helperDir = path.join(hexo.theme_dir, 'helper');
var partialDir = path.join(hexo.theme_dir, 'layout', 'partials');
var partials;
try {
partials = fs.readdirSync(partialDir);
} catch (e) {
// if this fails, there just aren't any partials. No problem.
}
if (partials) {
partials.forEach(function (fname) {
if (fname.split('.').slice(-1)[0] !== 'hbs') return;
handlebars.registerPartial(fname.split('.')[0], fs.readFileSync(path.join(partialDir, fname)).toString());
});
}
var helpers = {};
try {
helpers = require(helperDir)(hexo);
} catch(e) {
// no additional helpers available. No problem.
}
return template(locals, {
helpers: helpers
});
}
['hbs', 'handlebars'].forEach(function(extension) {
hexo.extend.renderer.register(extension, 'html', handlebarsRenderer, true);
});
exports.handlebars = handlebars;