-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandoc-mathjax-svg-filter.js
executable file
·96 lines (87 loc) · 2.35 KB
/
pandoc-mathjax-svg-filter.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
#!/usr/bin/env node
const RawInline = function(format, string) {
return {
t: 'RawInline',
c: [format, string],
};
};
const PandocMath = function(value) {
return {
t: 'Math',
c: value,
};
};
var mjAPI = require("mathjax-node");
mjAPI.config({
MathJax: {
// traditional MathJax configuration
}
});
mjAPI.start();
async function typesetAction(type, value, format, meta) {
if (type == 'Math') {
var isInline = (value[0].t == 'InlineMath');
var data = await mjAPI.typeset({
math: value[1],
format: (isInline ? 'inline-TeX' : 'TeX'),
svg: true,
linebreaks: true,
});
if (!data.errors && data.svg != '') {
if (isInline) {
return RawInline('html', '<span class="math inline">' + data.svg + '</span>');
} else {
return RawInline('html', '<p align="center"><span class="math display">' + data.svg + '</span></p>');
}
} else {
return PandocMath(value);
}
}
return null;
}
// Cannot use this: See https://github.com/mvhenderson/pandoc-filter-node/issues/7 (and 1)
// pandoc.stdio(action);
async function walk(x, action, format, meta) {
if (Array.isArray(x)) {
const array = [];
for (const item of x) {
if (item === Object(item) && item.t) {
var res = await action(item.t, item.c || [], format, meta);
if (!res) {
array.push(await walk(item, action, format, meta));
}
else if (Array.isArray(res)) {
for (const z of res) {
array.push(await walk(z, action, format, meta));
};
}
else {
array.push(await walk(res, action, format, meta));
}
}
else {
array.push(await walk(item, action, format, meta));
}
}
return array;
}
else if (x === Object(x)) {
var obj = {};
for (const k of Object.keys(x)) {
obj[k] = await walk(x[k], action, format, meta);
}
return obj;
}
return x;
}
async function filter(data, action, format) {
return await walk(data, action, format, data.meta || data[0].unMeta);
}
function toJSONFilter(action) {
require('get-stdin')(function (json) {
var data = JSON.parse(json);
var format = (process.argv.length > 2 ? process.argv[2] : '');
filter(data, action, format).then(output => process.stdout.write(JSON.stringify(output)));
});
}
toJSONFilter(typesetAction);