forked from walmartlabs/static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·194 lines (176 loc) · 4.97 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var handlebars = require('handlebars'),
async = require('async'),
cheerio = require('cheerio'),
marked = require('marked'),
path = require('path'),
fs = require('fs'),
_ = require('underscore'),
highlight = require('highlight.js');
var config = {
addIdsToHeadings: true,
gfm: true, //github flavored markdown
highlight: function(code, lang) {
return highlight.highlight('javascript', code).value;
}
};
var jqueryPath = './lib/jquery.js',
asyncTolkens = {},
markdownCallbacks = [];
var transforms = {};
function addTransform(name, callback) {
transforms[name] = callback;
}
addTransform('html', function(buffer, complete, context, data) {
return complete(buffer.toString());
});
addTransform('md', function(buffer, complete, context, data) {
var html = marked(buffer.toString(), {
gfm: config.gfm,
highlight: config.highlight
});
async.series(_.map(markdownCallbacks, function(callback) {
return function(next) {
callback(html, function(modifiedHTML) {
html = modifiedHTML;
next();
});
}
}), function() {
complete(html);
});
});
addTransform('hbs', function(buffer, complete, context, data) {
var output = handlebars.compile(buffer.toString(), {
data: true
})(context, {
data: data
});
var filteredAsyncTolkens = {};
_.each(asyncTolkens, function(tolkenData, tolken) {
if (data.file === tolkenData.file) {
filteredAsyncTolkens[tolken] = tolkenData;
}
});
if (!_.keys(filteredAsyncTolkens).length) {
complete(output);
} else {
async.series(_.map(filteredAsyncTolkens, function(tolkenData, tolken) {
return function(next) {
var args = tolkenData.args;
args.push(function(callbackOutput) {
output = output.replace(tolken, callbackOutput.toString());
next();
});
tolkenData.callback.apply(tolkenData.callback, args);
};
}), function() {
complete(output);
});
}
});
function $(html, callback) {
callback(cheerio.load(html));
}
function modifyDocumentFragment(html, callback, next) {
$(html, function($) {
callback($);
next($.html());
});
}
function removeOuterBodyTag(html) {
return html.replace(/^\s*\<body\>/, '').replace(/\<\/body\>\s*$/, '');
}
handlebars.registerAsyncHelper = function(name, callback) {
handlebars.registerHelper(name, function() {
var tolken = String(new Date().getTime() + Math.random());
var args = _.toArray(arguments),
data = args[args.length - 1].data;
asyncTolkens[tolken] = {
file: data.file,
args: args,
callback: callback
};
return tolken;
});
};
handlebars.registerHelper('require', function(file, options) {
var filePath = path.join(path.dirname(options.data.file), file);
require(filePath)(module.exports);
return '';
});
handlebars.registerAsyncHelper('include', function(file, options, callback) {
var filePath = path.join(path.dirname(options.data.file), file);
transform(filePath, function(fileData) {
var selector = options.hash.select;
if (selector) {
$(fileData.toString(), function($) {
var generatedHTML = '';
$(selector).each(function() {
// make more like a regular dom object
this.attributes = this[0].attribs;
this.id = this[0].attribs.id;
this.tagName = this[0].name;
this.innerHTML = this.html();
generatedHTML += options.fn(this);
});
callback(generatedHTML);
});
} else {
callback(fileData.toString());
}
}, options.hash, options.data);
});
function transform(source, callback, options) {
fs.readFile(source, function(err, data) {
if (err) {
console.trace();
throw err;
}
var extensions = source.split('/').pop().split('.');
var callbacks = _.filter(extensions, function(extension) {
return extension in transforms;
}).map(function(extension) {
return function(next) {
transforms[extension](data, next, options || {}, {
file: source
});
};
});
async.series(callbacks, callback);
});
}
function onMarkdown(callback) {
markdownCallbacks.push(callback);
}
onMarkdown(function(html, next) {
if (config.addIdsToHeadings) {
modifyDocumentFragment(html, function($) {
if (config.addIdsToHeadings) {
addIdsToHeadings($);
}
}, next);
} else {
next(html);
}
});
function addIdsToHeadings($) {
$('h1,h2,h3,h4,h5,h6').each(function() {
var text = $(this).html().split('<').shift();
var id = text.replace(/(^\s+|\s+$)/g, '').replace(/[\s]+/g, '-').replace(/([a-z])([A-Z])/g, function() {
return arguments[1] + '-' + arguments[2].toLowerCase();
}).toLowerCase();
if (id.match(/^\s+$/) || !id) {
return;
}
$(this).attr('id', id);
});
}
module.exports = {
config: config,
transform: transform,
handlebars: handlebars,
$: $,
modifyDocumentFragment: modifyDocumentFragment,
onMarkdown: onMarkdown,
addTransform: addTransform
};