-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.35 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
/* global hexo */
'use strict';
var Twig = require('twig');
function twigCompile(data) {
var template = Twig.twig({
data: data.text,
path: data.path
});
return function(locals) {
var context = {};
Object.keys(locals).forEach(function(key) {
var value = locals[key];
if ('function' === typeof value) {
Twig.extendFunction(key, value);
} else {
context[key] = value;
}
});
return template.render(context);
}
}
function twigRenderer(data) {
return twigCompile(data)(data);
}
twigRenderer.compile = twigCompile;
// Configure twig
(function(config) {
if (config.filters) {
config.filters.forEach(function(name) {
var fn = global[name];
Twig.extendFilter(name, fn);
});
}
if (config.functions) {
config.functions.forEach(function(name) {
var fn = global[name];
Twig.extendFunction(name, function() {
if (global[name]) {
return global[name].apply(this, arguments);
} else {
throw new Error("Missing function " + name);
}
});
});
}
})(hexo.config.twig || {});
// Register the renderer
hexo.extend.renderer.register('twig', 'html', twigRenderer);