Skip to content

Commit 6f973d4

Browse files
committed
chore: fix test
1 parent 809d22b commit 6f973d4

File tree

5 files changed

+70
-75
lines changed

5 files changed

+70
-75
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,5 @@ typings/
7878

7979

8080
# End of https://www.gitignore.io/api/node
81+
82+
.now

index.html

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ <h1>Math API</h1>
6464
</p>
6565
<h2>Usage</h2>
6666
<p style="font-size: 20px">
67-
https://math.now.sh?from=<span class="equation"
68-
>[LaTeX Math Equation]</span
69-
>
67+
https://math.now.sh?from=<span class="equation">[LaTeX Math Equation]</span>
7068
</p>
7169

7270
<h3>Block notation</h3>
7371
<p>
74-
https://math.now.sh?<span class="notation">from</span>=<span
75-
class="equation"
72+
https://math.now.sh?<span class="notation">from</span>=<span class="equation"
7673
>\frac{1}{\Gamma(s)}\int_{0}^{\infty}\frac{u^{s-1}}{e^{u}-1}\mathrm{d}u</span
7774
>
7875
</p>
@@ -82,8 +79,7 @@ <h3>Block notation</h3>
8279

8380
<h3>Inline notation</h3>
8481
<p>
85-
https://math.now.sh?<span class="notation">inline</span>=<span
86-
class="equation"
82+
https://math.now.sh?<span class="notation">inline</span>=<span class="equation"
8783
>\frac{1}{\Gamma(s)}\int_{0}^{\infty}\frac{u^{s-1}}{e^{u}-1}\mathrm{d}u</span
8884
>
8985
</p>
@@ -104,37 +100,35 @@ <h3>PNG support</h3>
104100

105101
<h3>Color</h3>
106102
<p>
107-
https://math.now.sh?from=<span class="equation">\sum</span>&<span
108-
class="notation"
103+
https://math.now.sh?from=<span class="equation">\sum</span>&<span class="notation"
109104
>color=red</span
110105
>
111106
</p>
112107
<img src="/?from=\sum&color=red" />
113108

114109
<h2>HTML Example</h2>
115110
<a href="https://codesandbox.io/s/2w223oq5lr">
116-
<img
117-
alt="Edit 2w223oq5lr"
118-
src="https://codesandbox.io/static/img/play-codesandbox.svg"
119-
/>
111+
<img alt="Edit 2w223oq5lr" src="https://codesandbox.io/static/img/play-codesandbox.svg" />
120112
</a>
121113
<p>
122-
&lt;img src=&quot;https://math.now.sh?<span class="notation">from</span
123-
>=<span class="equation">\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}</span
114+
&lt;img src=&quot;https://math.now.sh?<span class="notation">from</span>=<span
115+
class="equation"
116+
>\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}</span
124117
>&quot; /&gt;
125118
</p>
126119
<img src="/?from=\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}" />
127120
<p>
128-
&lt;img src=&quot;https://math.now.sh?<span class="notation"
129-
>inline</span
130-
>=<span class="equation">\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}</span
121+
&lt;img src=&quot;https://math.now.sh?<span class="notation">inline</span>=<span
122+
class="equation"
123+
>\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}</span
131124
>&quot; /&gt;
132125
</p>
133126
<img src="/?inline=\log\prod^N_{i}x_{i}=\sum^N_i\log{x_i}" />
134127
<h2>Markdown Example</h2>
135128
<p>
136-
![Equation 1](https://math.now.sh?<span class="notation">from</span
137-
>=<span class="equation">\text{softplus}(x)=\log(1+e^x)</span>)
129+
![Equation 1](https://math.now.sh?<span class="notation">from</span>=<span class="equation"
130+
>\text{softplus}(x)=\log(1+e^x)</span
131+
>)
138132
</p>
139133
<img src="/?from=%5Ctext%7Bsoftplus%7D(x)%3D%5Clog(1%2Be%5Ex)" />
140134

@@ -172,23 +166,22 @@ <h3>URL</h3>
172166
<script>
173167
function onTextChanged(e) {
174168
const value = e.value;
175-
const responseHTML = document.querySelector("#responseHTML");
176-
const responseMarkdown = document.querySelector("#responseMarkdown");
177-
const responseImage = document.querySelector("#responseImage");
169+
const responseHTML = document.querySelector('#responseHTML');
170+
const responseMarkdown = document.querySelector('#responseMarkdown');
171+
const responseImage = document.querySelector('#responseImage');
178172
const purifiedURL =
179-
"https://math.now.sh?from=" +
173+
'https://math.now.sh?from=' +
180174
encodeURIComponent(value)
181-
.replace("(", "%28")
182-
.replace(")", "%29");
175+
.replace('(', '%28')
176+
.replace(')', '%29');
183177
responseHTML.value = '<img src="' + purifiedURL + '" />';
184-
responseMarkdown.value = "![Equation](" + purifiedURL + ")";
178+
responseMarkdown.value = '![Equation](' + purifiedURL + ')';
185179
responseURL.value = purifiedURL;
186180
responseImage.src = purifiedURL;
187181
}
188182
</script>
189183
<h2>Resources</h2>
190-
<a href="https://github.com/uetchy/math-api">Source Code (GitHub)</a
191-
><br />
184+
<a href="https://github.com/uetchy/math-api">Source Code (GitHub)</a><br />
192185
<a href="https://twitter.com/uetschy">Author (Twitter)</a><br />
193186
<a href="https://dev.to/uetchy/math-api-latex-math-as-svg-image-m4p"
194187
>Math API: LaTeX Math as SVG image - DEV Community 👩‍💻👨‍💻</a

index.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import helmet from "helmet";
2-
import express from "express";
3-
import svg2img from "svg2img";
4-
import bodyParser from "body-parser";
5-
import { mathjax } from "mathjax-full/js/mathjax";
6-
import { TeX } from "mathjax-full/js/input/tex";
7-
import { SVG } from "mathjax-full/js/output/svg";
8-
import { LiteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor";
9-
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html";
10-
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages";
1+
import helmet from 'helmet';
2+
import express from 'express';
3+
import svg2img from 'svg2img';
4+
import bodyParser from 'body-parser';
5+
import { mathjax } from 'mathjax-full/js/mathjax';
6+
import { TeX } from 'mathjax-full/js/input/tex';
7+
import { SVG } from 'mathjax-full/js/output/svg';
8+
import { LiteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor';
9+
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html';
10+
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';
1111

1212
const app = express();
1313

@@ -18,17 +18,17 @@ app.use(bodyParser.urlencoded({ extended: true }));
1818
// MathJax bootstrap
1919
const adaptor = new LiteAdaptor();
2020
RegisterHTMLHandler(adaptor);
21-
const html = mathjax.document("", {
21+
const html = mathjax.document('', {
2222
InputJax: new TeX({ packages: AllPackages }),
23-
OutputJax: new SVG({ fontCache: "none" })
23+
OutputJax: new SVG({ fontCache: 'none' }),
2424
});
2525

2626
function tex2svg(equation: string, isInline: boolean, color: string): string {
2727
const svg = adaptor
2828
.innerHTML(html.convert(equation, { display: !isInline }))
2929
.replace(/fill="currentColor"/, `fill="${color}"`);
30-
if (svg.includes("merror")) {
31-
return svg.replace(/<rect.+?><\/rect>/, "");
30+
if (svg.includes('merror')) {
31+
return svg.replace(/<rect.+?><\/rect>/, '');
3232
}
3333
return svg;
3434
}
@@ -42,7 +42,7 @@ function svg2png(svgString: string): Promise<Buffer> {
4242

4343
const args = {
4444
width: `${width * 3}ex`,
45-
height: `${height * 3}ex`
45+
height: `${height * 3}ex`,
4646
};
4747

4848
svg2img(svgString, args, function(error: Error, buffer: Buffer) {
@@ -55,58 +55,56 @@ function svg2png(svgString: string): Promise<Buffer> {
5555
}
5656

5757
// math parser
58-
app.get("/", async function(req, res, next) {
59-
const mode = Object.keys(req.query).includes("from")
60-
? "block"
61-
: Object.keys(req.query).includes("inline")
62-
? "inline"
58+
app.get('/', async function(req, res, next) {
59+
const mode = Object.keys(req.query).includes('from')
60+
? 'block'
61+
: Object.keys(req.query).includes('inline')
62+
? 'inline'
6363
: null;
6464
if (!mode) {
6565
return next();
6666
}
67-
const isInline = mode === "inline";
67+
const isInline = mode === 'inline';
6868
const equation = isInline ? req.query.inline : req.query.from;
6969
if (!equation || equation.match(/\.ico$/)) {
7070
return next();
7171
}
7272

73-
const color = req.query.color || "black";
73+
const color = req.query.color || 'black';
7474
if (/[^a-zA-Z0-9#]/.test(color)) {
7575
return next();
7676
}
7777

7878
const isPNG = /\.png$/.test(equation);
79-
const normalizedEquation = equation.replace(/\.(svg|png)$/, "");
79+
const normalizedEquation = equation.replace(/\.(svg|png)$/, '');
8080

8181
try {
8282
const svgString = tex2svg(normalizedEquation, isInline, color);
8383
const imageData = isPNG ? await svg2png(svgString) : svgString;
8484

85-
res.setHeader("cache-control", "s-maxage=604800, maxage=604800");
85+
res.setHeader('cache-control', 's-maxage=604800, maxage=604800');
8686

8787
// render equation
8888
if (isPNG) {
89-
res.contentType("image/png");
89+
res.contentType('image/png');
9090
} else {
91-
res.contentType("image/svg+xml");
91+
res.contentType('image/svg+xml');
9292
res.write(`<?xml version="1.0" standalone="no" ?>
9393
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
9494
`);
9595
}
9696

9797
res.end(imageData);
9898
} catch (err) {
99-
res.write(
100-
'<svg xmlns="http://www.w3.org/2000/svg"><text x="0" y="15" font-size="15">'
101-
);
99+
res.write('<svg xmlns="http://www.w3.org/2000/svg"><text x="0" y="15" font-size="15">');
102100
res.write(err);
103-
res.end("</text></svg>");
101+
res.end('</text></svg>');
104102
}
105103
});
106104

107105
// welcome page
108-
app.get("/", function(req, res) {
109-
res.redirect(301, "/home");
106+
app.get('/', function(req, res) {
107+
res.redirect(301, '/home');
110108
});
111109

112110
// app.listen();

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
preset: "ts-jest",
3-
testEnvironment: "node",
4-
testRunner: "jest-circus/runner"
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testRunner: 'jest-circus/runner',
55
};

util/now.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
import waitOn from "wait-on";
2-
import getPort from "get-port";
3-
import spawn from "cross-spawn";
4-
import request from "supertest";
1+
import waitOn from 'wait-on';
2+
import getPort from 'get-port';
3+
import spawn from 'cross-spawn';
4+
import request from 'supertest';
5+
6+
const TIMEOUT = 60 * 1000;
57

68
export async function launchNow(
7-
hostname = "127.0.0.1"
9+
hostname = '127.0.0.1',
810
): Promise<[request.SuperTest<request.Test>, () => void]> {
911
const port = await getPort();
1012
const host = `${hostname}:${port}`;
11-
const nowDev = spawn("now", ["dev", "--listen", host], {
13+
const nowDev = spawn('now', ['dev', '--listen', host], {
1214
shell: true,
1315
detached: true,
14-
stdio: "ignore"
16+
stdio: 'ignore',
1517
});
1618
nowDev.unref();
1719
try {
18-
await waitOn({ resources: [`http://${host}`], timeout: 40000 });
20+
await waitOn({ resources: [`http://${host}`], timeout: TIMEOUT });
1921
} catch (err) {
20-
throw new Error("Timeout: launching now dev takes too much time");
22+
throw new Error('Timeout: launching now dev takes too much time');
2123
}
2224
return [
2325
request(`http://${host}`),
2426
() => {
25-
nowDev.kill("SIGINT");
26-
}
27+
nowDev.kill('SIGINT');
28+
},
2729
];
2830
}

0 commit comments

Comments
 (0)