This repository was archived by the owner on Jun 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnunjucks_config.js
53 lines (44 loc) · 1.66 KB
/
nunjucks_config.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
'use strict';
const fs = require('fs');
const path = require('path');
const _nunjucks = require('nunjucks');
const yaml = require('js-yaml');
module.exports = {
init: ()=>{
const ROOT_DIR = process.env.ROOT_DIR;
const bc = yaml.safeLoad(fs.readFileSync(path.join(ROOT_DIR,'_config.yml'), 'utf8'));
let nunjucks_opts = { autoescape: true, trimBlocks: true, lstripBlocks: true };
if( process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test" ){
nunjucks_opts = Object.assign(nunjucks_opts,{watch: false});
}
else{
nunjucks_opts = Object.assign(nunjucks_opts,{watch: true});
}
const nunjucks = _nunjucks.configure(path.join(ROOT_DIR,'themes',bc.meta.blog_theme), nunjucks_opts);
// filters
nunjucks.addFilter('date', function(str, count) {
// date filter
let dateObj = new Date(str);
return `${dateObj.getDate()}/${+dateObj.getMonth()+1}/${dateObj.getFullYear()}`;
});
nunjucks.addFilter('stringify', function(obj, count) {
// stringify filter
return JSON.stringify(obj);
});
nunjucks.addFilter('addbaseurl', function(str, count) {
// baseurl filter
if( process.env.NODE_ENV === "development"){
return str;
}
else{
if(!bc.meta.userpage){
return `/${bc.meta.baseurl}${str}`;
}
else{
return str;
}
}
});
return nunjucks;
}
};