forked from sbfkcel/markdown-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (44 loc) · 1.42 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
const http = require("http"),
url = require('url'),
qs = require('querystring'),
mathjax = require("mathjax-node"),
yuml2svg = require('yuml2svg');
mathjax.start();
const app = http.createServer((req,res)=>{
let queryObj = qs.parse(url.parse(req.url).query),
tex = queryObj.tex,
yuml = queryObj.yuml,
theme = queryObj.theme,
errFn = (msg)=>{
res.writeHead(404,{'Content-type':'text/html;charset=utf-8'});
res.write(msg);
res.end();
},
successFn = (result)=>{
res.writeHead(200,{'Content-type':'image/svg+xml;charset=utf-8'});
res.write(result);
res.end();
};
if(yuml){
yuml2svg(yuml,{isDark:theme === 'dark'}).then(v => {
successFn(v);
}).catch(e => {
errFn('Yuml formula is wrong!');
});
}else if(tex){
mathjax.typeset({
math:tex,
format:'TeX',
svg:true
},data => {
if(theme === 'dark'){
data.svg = data.svg.replace(/fill="currentColor"/g,'fill="#ffffff"');
};
successFn(data.svg);
})
}else{
// 请通过`tex`参数传入LaTeX公式,或使用`yuml`参数传入`yuml`表达式。
errFn('Please pass LaTeX formula via `tex` parameter or `Yuml` expression using `yuml` parameter.');
};
});
app.listen(8001);