Scroll completion menu to top when completion list changes.
[lw2-viewer.git] / js-foreign-lib / mathjax.js
blobf02059e20adc60d4e2a446219858f135a5920aad
1 var mathjax = require('mathjax-node'),
2     chunks = [],
3     size = null;
5 mathjax.config({ MathJax: { loader: {load: ['ui/safe']},
6                             extensions: ["Safe.js"] } });
8 process.stdin.on('data', (chunk) => {
9         if(size === null) {
10                 size = chunk.readUInt32LE();
11                 onDataChunk(chunk.subarray(4));
12         } else {
13                 onDataChunk(chunk);
14         }
15 });
17 function onDataChunk(chunk) {
18         if(chunk.length > size) {
19                 var excessChunk = chunk.subarray(size);
20                 process.stdin.unshift(excessChunk);
21                 chunk = chunk.subarray(0, size);
22         }
24         chunks.push(chunk);
26         if(chunk.length == size) {
27                 onMessage(Buffer.concat(chunks).toString());
28                 size = null;
29                 chunks = [];
30         }
31         else {
32                 size -= remainingChunk.length;
33         }
36 function onMessage(input) {
37         mathjax.typeset(
38                 { math: input,
39                   format: "inline-TeX",
40                   html: true,
41                   css: true
42                 },
43                 (data) => {
44                         var sizeBuf = Buffer.alloc(4);
45                         if(data.errors) {
46                                 sizeBuf.writeUInt32LE(0);
47                                 process.stdout.write(sizeBuf);
48                         }
49                         else {
50                                 var dataBuf = Buffer.from("<style>"+data.css+"</style>"+data.html);
51                                 sizeBuf.writeUInt32LE(dataBuf.length);
52                                 process.stdout.write(sizeBuf);
53                                 process.stdout.write(dataBuf);
54                         }
55                 }
56         );