-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtypeset.js
More file actions
67 lines (60 loc) · 1.44 KB
/
typeset.js
File metadata and controls
67 lines (60 loc) · 1.44 KB
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
'use strict';
let mj = require("mathjax-node");
mj.config({
extensions: "TeX/color",
MathJax: {
extensions: ["Safe.js"]
}
});
mj.start();
let typesetConfig = function(tex) {
return {
math: cleanTex(tex),
format: "inline-TeX",
svg: true,
svgNode: true,
mml: true,
speakText: false,
ex: 6,
width: 100,
linebreaks: true
}
};
let cleanTex = function(tex) {
return tex.replace(/\\slash/, '/')
};
function ensureTextFill(svg) {
for (let text of svg.getElementsByTagName('text')) {
if (!text.hasAttribute('fill')) {
text.setAttribute('fill', 'currentColor');
}
}
}
let mjCallback = function(scale, cb) {
return function(data) {
if (!data.errors) {
let svg;
if (data.svgNode) {
ensureTextFill(data.svgNode);
if (scale !== 1) {
const w = data.svgNode.getAttribute("width").match(/([\d.]+)(.*)/)
data.svgNode.setAttribute("width", `${w[1] * scale}${w[2]}`)
const h = data.svgNode.getAttribute("height").match(/([\d.]+)(.*)/)
data.svgNode.setAttribute("height", `${h[1] * scale}${h[2]}`)
}
svg = data.svgNode.outerHTML;
}
cb(null, {svg, mml: data.mml});
} else {
cb(data.errors);
}
}
};
// Public
let typeset = function(opts, cb) {
opts = opts || {}
const tex = opts.tex
const scale = opts.scale || 1
mj.typeset(typesetConfig(tex), mjCallback(scale, cb));
};
module.exports = typeset;