-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
TemplateUtils.js
68 lines (57 loc) · 1.99 KB
/
TemplateUtils.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
const shim = require('lib/shim').default;
const { time } = require('lib/time-utils.js');
const Mustache = require('mustache');
const TemplateUtils = {};
// Mustache escapes strings (including /) with the html code by default
// This isn't useful for markdown so it's disabled
Mustache.escape = text => {
return text;
};
TemplateUtils.render = function(input) {
// new template variables can be added here
// If there are too many, this should be moved to a new file
// view needs to be set in this function so that the formats reflect settings
const view = {
date: time.formatMsToLocal(new Date().getTime(), time.dateFormat()),
time: time.formatMsToLocal(new Date().getTime(), time.timeFormat()),
datetime: time.formatMsToLocal(new Date().getTime()),
custom_datetime: () => {
return (text, render) => {
return render(time.formatMsToLocal(new Date().getTime(), text));
};
},
};
return Mustache.render(input, view);
};
TemplateUtils.loadTemplates = async function(filePath) {
const templates = [];
let files = [];
if (await shim.fsDriver().exists(filePath)) {
try {
files = await shim.fsDriver().readDirStats(filePath);
} catch (error) {
let msg = error.message ? error.message : '';
msg = `Could not read template names from ${filePath}\n${msg}`;
error.message = msg;
throw error;
}
// Make sure templates are always in the same order
// sensitivity ensures that the sort will ignore case
files.sort((a, b) => { return a.path.localeCompare(b.path, undefined, { sensitivity: 'accent' }); });
for (const file of files) {
if (file.path.endsWith('.md')) {
try {
const fileString = await shim.fsDriver().readFile(`${filePath}/${file.path}`, 'utf-8');
templates.push({ label: file.path, value: fileString });
} catch (error) {
let msg = error.message ? error.message : '';
msg = `Could not load template ${file.path}\n${msg}`;
error.message = msg;
throw error;
}
}
}
}
return templates;
};
module.exports = TemplateUtils;