forked from Mermade/shins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
307 lines (283 loc) · 12.1 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var maybe = require('call-me-maybe');
var hljs = require('highlightjs/highlight.pack.js');
var hlpath = require.resolve('highlightjs/highlight.pack.js').replace('highlight.pack.js', '');
var emoji = require('markdown-it-emoji');
var md = require('markdown-it')({
linkify: true, html: true,
highlight: function (str, lang) {
var slang = lang.split('--')[0]; // allows multiple language tabs for the same language
if (slang && hljs.getLanguage(slang)) {
try {
return '<pre class="highlight tab tab-' + lang + '"><code>' +
hljs.highlight(slang, str, true).value +
'</code></pre>';
} catch (__) { }
}
return '<pre class="highlight"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
}).use(require('markdown-it-lazy-headers'));
md.use(emoji);
var yaml = require('js-yaml');
var ejs = require('ejs');
var uglify = require('uglify-js');
var cheerio = require('cheerio');
var sanitizeHtml = require('sanitize-html');
var globalOptions = {};
function javascript_include_tag(include) {
var includeStr = fs.readFileSync(path.join(__dirname, '/source/javascripts/' + include + '.inc'), 'utf8');
if (globalOptions.minify) {
var scripts = [];
var includes = includeStr.split('\r').join().split('\n');
for (var i in includes) {
var inc = includes[i];
var elements = inc.split('"');
if (elements[1]) {
if (elements[1] == 'text/javascript') {
scripts.push(path.join(__dirname, 'source/javascripts/all_nosearch.js'));
break;
}
else {
scripts.push(path.join(__dirname, elements[1]));
}
}
}
var bundle = uglify.minify(scripts);
if (globalOptions.inline) {
includeStr = '<script>'+bundle.code+'</script>';
}
else {
fs.writeFileSync(path.join(__dirname, '/pub/js/shins.js'), bundle.code, 'utf8');
includeStr = fs.readFileSync(path.join(__dirname, '/source/javascripts/' + include + '.bundle.inc'), 'utf8');
}
}
return includeStr;
}
function partial(include) {
var includeStr = fs.readFileSync(path.join(__dirname, '/source/includes/_' + include + '.md'), 'utf8');
return postProcess(md.render(clean(includeStr)));
}
function replaceAll(target, find, replace) {
return target.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
}
function stylesheet_link_tag(stylesheet, media) {
var override = stylesheet;
if ((stylesheet != 'print') && (stylesheet != 'screen')) {
override = 'theme';
}
if (globalOptions.inline) {
var stylePath = path.join(__dirname, '/pub/css/' + stylesheet + '.css');
if (!fs.existsSync(stylePath)) {
stylePath = path.join(hlpath, '/styles/' + stylesheet + '.css');
}
var styleContent = fs.readFileSync(stylePath, "utf8");
styleContent = replaceAll(styleContent, "../../source", "source"); //fix font paths
if (globalOptions.customCss) {
var overrideFilename = path.join(__dirname, '/pub/css/' + override + '_overrides.css');
if (fs.existsSync(overrideFilename)) {
styleContent += '\n' + fs.readFileSync(overrideFilename, 'utf8');
}
}
return '<style media="'+media+'">'+styleContent+'</style>';
}
else {
if (media == 'screen') {
var target = path.join(__dirname, '/pub/css/' + stylesheet + '.css');
if (!fs.existsSync(target)) {
var source = path.join(hlpath, '/styles/' + stylesheet + '.css');
fs.writeFileSync(target, fs.readFileSync(source));
}
}
var include = '<link rel="stylesheet" media="' + media + '" href="pub/css/' + stylesheet + '.css">';
if (globalOptions.customCss) {
include += '\n <link rel="stylesheet" media="' + media + '" href="pub/css/' + override + '_overrides.css">';
}
return include;
}
}
function language_array(language_tabs) {
var result = [];
for (var lang in language_tabs) {
if (typeof language_tabs[lang] === 'object') {
result.push(Object.keys(language_tabs[lang])[0]);
}
else {
result.push(language_tabs[lang]);
}
}
return JSON.stringify(result).split('"').join('"');
}
function preProcess(content,options) {
let lines = content.split('\r').join('').split('\n');
for (let l=0;l<lines.length;l++) {
let line = lines[l];
let filename = '';
if (line.startsWith('include::') && line.endsWith('[]')) { // asciidoc syntax
filename = line.split(':')[2].replace('[]','');
}
else if (line.startsWith('!INCLUDE ')) { // markdown-pp syntax
filename = line.replace('!INCLUDE ','');
}
if (filename) {
if (options.source) filename = path.resolve(path.dirname(options.source),filename);
let s = fs.readFileSync(filename,'utf8');
let include = s.split('\r').join('').split('\n');
lines.splice(l,1,...include);
}
else lines[l] = line;
}
return lines.join('\n');
}
function postProcess(content) {
// adds id a la GitHub autolinks to automatically-generated headers
content = content.replace(/\<(h[123456])\>(.*)\<\/h[123456]\>/g, function (match, group1, group2) {
return '<' + group1 + ' id="' + group2.toLowerCase().split(' ').join('-').split('/').join('-').split('.').join('-').split('(').join('-').split(')').join('-').split('[').join('-').split(']').join('-').split('?').join('-').split('&').join('-').split(';').join('-').split('{').join('-').split('}').join('-').split('=').join('-') + '">' + group2 + '</' + group1 + '>';
});
return content;
}
function clean(s) {
if (!s) return '';
if (globalOptions.unsafe) return s;
let sanitizeOptions = {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'h1', 'h2', 'img', 'aside', 'article', 'details',
'summary', 'abbr', 'meta', 'link' ]),
allowedAttributes: { a: [ 'href', 'id', 'name', 'target', 'class' ], img: [ 'src', 'alt', 'class' ] , aside: [ 'class' ],
abbr: [ 'title', 'class' ], details: [ 'open', 'class' ], div: [ 'class' ], meta: [ 'name', 'content' ],
link: [ 'rel', 'href', 'type', 'sizes' ],
h1: [ 'id' ], h2: [ 'id' ], h3: [ 'id' ], h4: [ 'id' ], h5: [ 'id' ], h6: [ 'id' ]}
};
// replace things which look like tags which sanitizeHtml will eat
s = s.split('\n>').join('\n$1$');
s = s.split('>=').join('$2$');
s = s.split('<=').join('$3$');
let a = s.split('```');
for (let i=0;i<a.length;i++) {
if (!a[i].startsWith('xml')) {
a[i] = sanitizeHtml(a[i],sanitizeOptions);
}
}
s = a.join('```');
// put back things which sanitizeHtml has mangled
s = s.split('"').join('"');
s = s.split('&').join('&');
s = s.split('>').join('>');
s = s.split('<').join('<');
s = s.split('\n$1$').join('\n>');
s = s.split('$2$').join('>=');
s = s.split('$3$').join('<=');
return s;
}
function render(inputStr, options, callback) {
if (typeof callback === 'undefined') { // for pre-v1.4.0 compatibility
callback = options;
options = {};
}
if (options.inline == true) {
options.minify = true;
}
return maybe(callback, new Promise(function (resolve, reject) {
globalOptions = options;
inputStr = inputStr.split('\r\n').join('\n');
var inputArr = ('\n' + inputStr).split('\n---\n');
if (inputArr.length === 1) {
inputArr = ('\n' + inputStr).split('\n--- \n');
}
var headerStr = inputArr[1];
var header = yaml.safeLoad(headerStr);
/* non-matching languages between Ruby Rouge and highlight.js at 2016/07/10 are
['ceylon','common_lisp','conf','cowscript','erb','factor','io','json-doc','liquid','literate_coffeescript','literate_haskell','llvm','make',
'objective_c','plaintext','praat','properties','racket','sass','sed','shell','slim','sml','toml','tulip','viml'];*/
var sh = hljs.getLanguage('bash');
hljs.registerLanguage('shell', function (hljs) { return sh; });
hljs.registerLanguage('sh', function (hljs) { return sh; });
while (inputArr.length<3) inputArr.push('');
var content = preProcess(inputArr[2],options);
content = md.render(clean(content));
content = postProcess(content);
var locals = {};
locals.current_page = {};
locals.current_page.data = header;
locals.page_content = content;
locals.toc_data = function(content) {
var $ = cheerio.load(content);
var result = [];
var h1,h2,h3,h4,h5;
var headingLevel = header.headingLevel || 2;
$(':header').each(function(e){
var tag = $(this).get(0).tagName.toLowerCase();
var entry = {};
if (tag === 'h1') {
entry.id = $(this).attr('id');
entry.content = $(this).text();
entry.children = [];
h1 = entry;
result.push(entry);
}
if (tag === 'h2') {
let child = {};
child.id = $(this).attr('id');
child.content = $(this).text();
child.children = [];
h2 = child;
if (h1) h1.children.push(child);
}
if ((headingLevel >= 3) && (tag === 'h3')) {
let child = {};
child.id = $(this).attr('id');
child.content = $(this).text();
child.children = [];
h3 = child;
if (h2) h2.children.push(child);
}
if ((headingLevel >= 4) && (tag === 'h4')) {
let child = {};
child.id = $(this).attr('id');
child.content = $(this).text();
child.children = [];
h4 = child;
if (h3) h3.children.push(child);
}
if ((headingLevel >= 5) && (tag === 'h5')) {
let child = {};
child.id = $(this).attr('id');
child.content = $(this).text();
child.children = [];
h5 = child;
if (h4) h4.children.push(child);
}
if ((headingLevel >= 6) && (tag === 'h6')) {
let child = {};
child.id = $(this).attr('id');
child.content = $(this).text();
if (h5) h5.children.push(child);
}
});
return result; //[{id:'test',content:'hello',children:[]}];
};
locals.partial = partial;
locals.image_tag = function (image, altText, className) {
var imageSource = "source/images/" + image;
if (globalOptions.inline) {
var imgContent = fs.readFileSync(path.join(__dirname, imageSource));
imageSource = "data:image/png;base64,"+new Buffer(imgContent).toString('base64');
}
return '<img src="'+imageSource+'" class="' + className + '" alt="' + altText + '">';
};
locals.stylesheet_link_tag = stylesheet_link_tag;
locals.javascript_include_tag = javascript_include_tag;
locals.language_array = language_array;
var ejsOptions = {};
ejsOptions.debug = false;
ejs.renderFile(path.join(__dirname, '/source/layouts/layout.ejs'), locals, ejsOptions, function (err, str) {
if (err) reject(err)
else resolve(str);
});
}));
}
module.exports = {
render: render,
srcDir: function () { return __dirname; }
};