-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (51 loc) · 1.47 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
59
60
61
62
63
64
65
var fs = require('fs'),
path = require('path'),
ul = require('ul'),
md = require('marked');
/**
* Creates a new `Templator` instance.
* @name Templator
* @function
* @param {Object} options An object containing the following fields:
*
* - `path` (String): The path to look for markdown partials.
*/
var Templator = function (options) {
if (typeof options === 'string') {
options = {
path: options
};
}
options = ul.merge(options, {
path: './'
});
options.path = path.resolve(options.path);
this.options = options;
this.cwd = options.path;
};
/**
* Run the contents of an HTML file through the `Templator`
* @name processFile
* @function
* @param {String} templateFile Path to HTML file to be processed
* @return {String} The processed HTML
*/
Templator.prototype.processFile = function (templateFile) {
return this.processContent(fs.readFileSync(templateFile).toString());
};
/**
* Inject markdown partials into HTML template
* @name processContent
* @function
* @param {String} templateContent HTML template string
* @return {String} The processed HTML
*/
Templator.prototype.processContent = function (templateContent) {
return templateContent.replace(/{{(.+)}}/g, function (match, name) {
var filepath = path.join(this.cwd, name + (/\.md$/.test(name) ? '' : '.md'));
return fs.existsSync(filepath)
? md(fs.readFileSync(filepath).toString())
: match;
}.bind(this));
};
module.exports = Templator;