forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (84 loc) · 2.71 KB
/
server.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
/* eslint no-console:0 */
var fs = require("fs");
var path = require("path");
var browserify = require("browserify");
var express = require("express");
var glob = require("glob");
var less = require("less");
var app = express();
if (require.main === module) {
app.use(express.logger());
}
var serveBrowserified = function(file, standaloneName) {
return function(req, res, next) {
var files;
if (Array.isArray(file)) {
files = file;
} else if (file.indexOf("*") !== -1) {
files = glob.sync(file);
} else {
files = [file];
}
var options = {};
if (standaloneName) {
options.standalone = standaloneName;
}
var b = browserify(files, options);
var stream = b.bundle();
var body = "";
stream.on("data", function(s) { body += s; });
stream.on("error", function(e) { next(e); });
stream.on("end", function() {
res.setHeader("Content-Type", "text/javascript");
res.send(body);
});
};
};
app.get("/katex.js", serveBrowserified("./katex", "katex"));
app.use("/test/jasmine",
express["static"](
path.dirname(
require.resolve("jasmine-core/lib/jasmine-core/jasmine.js")
)
)
);
app.get("/test/katex-spec.js", serveBrowserified("./test/*[Ss]pec.js"));
app.get("/contrib/auto-render/auto-render.js",
serveBrowserified("./contrib/auto-render/auto-render",
"renderMathInElement"));
app.get("/katex.css", function(req, res, next) {
fs.readFile("static/katex.less", {encoding: "utf8"}, function(err, data) {
if (err) {
next(err);
return;
}
less.render(data, {
paths: ["./static"],
filename: "katex.less",
}, function(err, output) {
if (err) {
console.error(String(err));
next(err);
return;
}
res.setHeader("Content-Type", "text/css");
res.send(output.css);
});
});
});
app.use(express["static"](path.join(__dirname, "static")));
app.use(express["static"](path.join(__dirname, "build")));
app.use("/test", express["static"](path.join(__dirname, "test")));
app.use("/contrib", express["static"](path.join(__dirname, "contrib")));
// app.use("/unicode-fonts",
// express["static"](path.join(__dirname, "static", "unicode-fonts")));
app.use(function(err, req, res, next) {
console.error(err.stack);
res.setHeader("Content-Type", "text/plain");
res.send(500, err.stack);
});
if (require.main === module) {
app.listen(7936);
console.log("Serving on http://0.0.0.0:7936/ ...");
}
module.exports = app;