Skip to content

Commit 0e63566

Browse files
authored
Optional Mathjax rendering using mathjax-node-page package (#53)
* Node upgrade dependencies * Barely working mathjax rendering using node.js * Cache mathjax css and make it a command line option * Allow use of local mathjax fonts * Move mathjax-node-page config out of function scope * No line break (word wrap) and justify
1 parent 6ff99d0 commit 0e63566

File tree

8 files changed

+258
-6
lines changed

8 files changed

+258
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
package-lock.json

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Environment variables
2929
* `INSTANT_MARKDOWN_BLOCK_EXTERNAL=1` - by default, external resources such as
3030
images, stylesheets, frames and plugins are *allowed*. Use this setting to
3131
*block* such external content.
32+
33+
* `INSTANT_MARKDOWN_MATHJAX_FONTS="/usr/share/mathjax/fonts/HTML-CSS/"` - to
34+
serve fonts for Mathjax from a local directory.

css/mjpage-html.css

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/mjpage-svg.css

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
.mjpage .MJX-monospace {
3+
font-family: monospace
4+
}
5+
6+
.mjpage .MJX-sans-serif {
7+
font-family: sans-serif
8+
}
9+
10+
.mjpage {
11+
display: inline;
12+
font-style: normal;
13+
font-weight: normal;
14+
line-height: normal;
15+
font-size: 100%;
16+
font-size-adjust: none;
17+
text-indent: 0;
18+
text-align: left;
19+
text-transform: none;
20+
letter-spacing: normal;
21+
word-spacing: normal;
22+
word-wrap: normal;
23+
white-space: nowrap;
24+
float: none;
25+
direction: ltr;
26+
max-width: none;
27+
max-height: none;
28+
min-width: 0;
29+
min-height: 0;
30+
border: 0;
31+
padding: 0;
32+
margin: 0
33+
}
34+
35+
.mjpage * {
36+
transition: none;
37+
-webkit-transition: none;
38+
-moz-transition: none;
39+
-ms-transition: none;
40+
-o-transition: none
41+
}
42+
43+
.mjx-svg-href {
44+
fill: blue;
45+
stroke: blue
46+
}
47+
48+
.MathJax_SVG_LineBox {
49+
display: table!important
50+
}
51+
52+
.MathJax_SVG_LineBox span {
53+
display: table-cell!important;
54+
width: 10000em!important;
55+
min-width: 0;
56+
max-width: none;
57+
padding: 0;
58+
border: 0;
59+
margin: 0
60+
}
61+
62+
.mjpage__block {
63+
text-align: center;
64+
margin: 1em 0em;
65+
position: relative;
66+
display: block!important;
67+
text-indent: 0;
68+
max-width: none;
69+
max-height: none;
70+
min-width: 0;
71+
min-height: 0;
72+
width: 100%
73+
}

github-markdown.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ td,th {
145145
font-size: 16px;
146146
line-height: 1.6;
147147
word-wrap: break-word;
148+
text-align: justify;
148149
}
149150

150151
.markdown-body a {

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta http-equiv="Content-Language" content="en">
77
<link rel="stylesheet" type="text/css" href="/github-syntax-highlight.css">
88
<link rel="stylesheet" type="text/css" href="/github-markdown.css">
9+
<link rel="stylesheet" type="text/css" href="/css/mjpage-html.css">
910
<style>
1011
.markdown-body {
1112
min-width: 200px;

instant-markdown-d

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var server = require('http').createServer(httpHandler),
88
os = require('os'),
99
send = require('send');
1010

11+
var mjpage = require('mathjax-node-page').mjpage;
12+
// var fs = require('fs'); // for debugging
13+
var enableMathJax = process.argv[2] === "--mathjax";
14+
1115
// WARNING: By setting this environment variable, anyone on your network may
1216
// run arbitrary code in your browser and read arbitrary files in the working
1317
// directory of the open file!
@@ -35,10 +39,47 @@ var md = new MarkdownIt({
3539
}
3640
});
3741

42+
if (enableMathJax) md.use(require('markdown-it-mathjax')());
43+
44+
const mjPageConfig = {
45+
format: ["TeX"],
46+
cssInline: false,
47+
};
48+
49+
if (process.env.INSTANT_MARKDOWN_MATHJAX_FONTS) {
50+
mjPageConfig.fontURL = process.env.INSTANT_MARKDOWN_MATHJAX_FONTS;
51+
}
52+
53+
const mjNodeConfig = {
54+
html: true,
55+
// mml: true,
56+
// svg: true,
57+
equationNumbers: "AMS",
58+
speakText: false
59+
};
60+
61+
function mathJaxRenderEmit(newHtml) {
62+
if(enableMathJax) {
63+
mjpage(
64+
newHtml,
65+
mjPageConfig,
66+
mjNodeConfig,
67+
function(data) {
68+
// console.log(data); // resulting HTML string
69+
// fs.writeFileSync('output.html', data, 'utf-8'); // debug
70+
io.sockets.emit('newContent', data);
71+
}
72+
);
73+
}
74+
else {
75+
io.sockets.emit('newContent', newHtml)
76+
}
77+
}
78+
3879
var lastWrittenMarkdown = '';
3980
function writeMarkdown(body) {
4081
lastWrittenMarkdown = md.render(body);
41-
io.sockets.emit('newContent', lastWrittenMarkdown);
82+
mathJaxRenderEmit(lastWrittenMarkdown);
4283
}
4384

4485
function readAllInput(input, callback) {
@@ -135,7 +176,8 @@ function httpHandler(req, res) {
135176
io.sockets.on('connection', function(sock){
136177
process.stdout.write('connection established!');
137178
if (lastWrittenMarkdown) {
138-
sock.emit('newContent', lastWrittenMarkdown);
179+
sock.emit('newContent', lastWrittenMarkdown); // Quick preview
180+
if (enableMathJax) mathJaxRenderEmit(lastWrittenMarkdown);
139181
}
140182
});
141183

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
"instant-markdown-d": "./instant-markdown-d"
1111
},
1212
"dependencies": {
13-
"highlight.js": "^8.4.0",
14-
"markdown-it": "^3.0.3",
15-
"send": "~0.1.0",
16-
"socket.io": "^1.4.5"
13+
"highlight.js": "^9.13.0",
14+
"markdown-it": "^8.4.2",
15+
"markdown-it-mathjax": "^2.0.0",
16+
"mathjax-node-page": "^3.0.1",
17+
"mathjax": "^2.7.5",
18+
"send": "~0.16.2",
19+
"socket.io": "^2.1.1"
1720
}
1821
}

0 commit comments

Comments
 (0)