forked from hexojs/hexo-renderer-pandoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (83 loc) · 2.44 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
var spawn = require('child_process').spawn;
var pandocRenderer = function(data, options, callback){
var config = hexo.config.pandoc;
var extensions = '', filters = [], extra = [], meta = [], math = '--mathjax';
if(config) {
if(config.extensions) {
config.extensions.forEach(function(extension) {
extensions += extension;
});
}
if(config.filters) {
config.filters.forEach(function(filter) {
filters.push('--filter');
filters.push(filter);
});
}
if(config.extra) {
for(var e in config.extra) {
var eoption = config.extra[e];
for (var key in eoption){
extra.push('--' + key);
extra.push(eoption[key]);
}
}
}
if(config.meta) {
config.meta.forEach(function(m) {
meta.push('-M');
if(m.length) {
meta.push(m);
} else {
for(var m2 in m) {
meta.push(m2 + '=' + m[m2]);
}
}
});
}
if(config.mathEngine) {
if(typeof config.mathEngine === 'string') {
math = '--' + config.mathEngine;
}
}
}
var args = [ '-f', 'markdown'+extensions, '-t', 'html', math]
.concat(filters)
.concat(extra)
.concat(meta);
if(config && config.template) args.push("--template=" + config.template);
var src = data.text.toString();
var pandoc = spawn('pandoc', args);
var result = '';
var error = '';
pandoc.stdout.setEncoding('utf8');
pandoc.stdout.on('data', function (data) {
result += data.toString();
});
pandoc.stderr.on('data', function (data) {
error += data.toString();
});
pandoc.stdin.write(src, 'utf8');
pandoc.on('close', function (code, signal) {
var msg = '';
if (code !== 0)
msg += 'pandoc exited with code '+code+(error ? ': ' : '.');
if (error)
msg += error;
if (msg)
return callback(new Error(msg));
else{
if (result === '') console.log("The next file error: ");
callback(null, result);
}
});
pandoc.stdin.end();
}
pandocRenderer.disableNunjucks = true;
hexo.extend.renderer.register('md', 'html', pandocRenderer);
hexo.extend.renderer.register('markdown', 'html', pandocRenderer);
hexo.extend.renderer.register('mkd', 'html', pandocRenderer);
hexo.extend.renderer.register('mkdn', 'html', pandocRenderer);
hexo.extend.renderer.register('mdwn', 'html', pandocRenderer);
hexo.extend.renderer.register('mdtxt', 'html', pandocRenderer);
hexo.extend.renderer.register('mdtext', 'html', pandocRenderer);