-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy patheditor.html
More file actions
310 lines (274 loc) · 8.08 KB
/
editor.html
File metadata and controls
310 lines (274 loc) · 8.08 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<html>
<head>
<title>Live markdown editor based on MathJax and Marked</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- based on MathJax/test/sample-dynamic-2.html -->
<style>
body {
width: 40em;
}
.hint {
text-align: right;
color: #555555;
font-size: small;
}
.hint a {
color: #555555;
}
textarea {
margin-top: 1ex;
width: 100%;
height: 20em;
}
.preview {
border:1px dotted;
padding: 3px;
width: 99%;
margin-top:1ex;
}
blockquote {
color: #6a737d;
padding-left: 3px;
padding-top: 3px;
padding-bottom: 3px;
margin: 0;
border-left: 3px solid #73A0C5;
}
blockquote > blockquote {
border-color: #CF908D;
}
blockquote > blockquote > blockquote {
border-color: #57C59C;
}
blockquote > blockquote > blockquote > blockquote {
border-color: #C957F8;
}
blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #F8B829;
}
blockquote > blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #8DA1C5;
}
blockquote > blockquote > blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #888;
}
blockquote p:first-of-type {
margin-top: 0;
}
blockquote p:last-of-type {
margin-bottom: 0;
}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] },
TeX: { equationNumbers: {autoNumber: "AMS"} }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="lib/marked/lib/marked.js"></script>
<script>
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false, // IMPORTANT, because we do MathJax before markdown,
// however we do escaping in 'CreatePreview'.
smartLists: true,
smartypants: false
});
</script>
<script>
var Preview = {
delay: 50, // delay after keystroke before updating
preview: null, // filled in by Init below
buffer: null, // filled in by Init below
timeout: null, // store setTimout id
mjRunning: false, // true when MathJax is processing
oldText: null, // used to check if an update is needed
//
// Get the preview and buffer DIV's
//
Init: function () {
this.preview = document.getElementById("marked-mathjax-preview");
this.buffer = document.getElementById("marked-mathjax-preview-buffer");
this.textarea = document.getElementById("marked-mathjax-input");
},
//
// Switch the buffer and preview, and display the right one.
// (We use visibility:hidden rather than display:none since
// the results of running MathJax are more accurate that way.)
//
SwapBuffers: function () {
var buffer = this.preview;
var preview = this.buffer;
this.buffer = buffer;
this.preview = preview;
buffer.style.display = "none";
buffer.style.position = "absolute";
preview.style.position = "";
preview.style.display = "";
},
//
// This gets called when a key is pressed in the textarea.
// We check if there is already a pending update and clear it if so.
// Then set up an update to occur after a small delay (so if more keys
// are pressed, the update won't occur until after there has been
// a pause in the typing).
// The callback function is set up below, after the Preview object is set up.
//
Update: function () {
if (this.timeout) {clearTimeout(this.timeout)}
this.timeout = setTimeout(this.callback,this.delay);
},
//
// Creates the preview and runs MathJax on it.
// If MathJax is already trying to render the code, return
// If the text hasn't changed, return
// Otherwise, indicate that MathJax is running, and start the
// typesetting. After it is done, call PreviewDone.
//
CreatePreview: function () {
Preview.timeout = null;
if (this.mjRunning) return;
var text = this.textarea.value;
if (text === this.oldtext) return;
text = this.Escape(text); //Escape tags before doing stuff
this.buffer.innerHTML = this.oldtext = text;
this.mjRunning = true;
MathJax.Hub.Queue(
["Typeset",MathJax.Hub,this.buffer],
["PreviewDone",this],
["resetEquationNumbers", MathJax.InputJax.TeX]
);
},
//
// Indicate that MathJax is no longer running,
// do markdown over MathJax's result,
// and swap the buffers to show the results.
//
PreviewDone: function () {
this.mjRunning = false;
text = this.buffer.innerHTML;
text = this.PartialDescape(text);
this.buffer.innerHTML = marked (text);
this.SwapBuffers();
},
Escape: function (html, encode) {
return html
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
},
PartialDescape: function (html) {
var lines = html.split('\n');
var out = '';
// is true when we are
// ```
// inside a code block
// ```
var inside_code = false;
for (var i = 0; i < lines.length; i++) {
// a hack to properly rendre the blockquotes
if (lines[i].startsWith('>')) {
lines[i] = lines[i].replace(/>/g, '>');
}
// rendrer properly stuff like this
// ```c
// if (a > b)
// ```
if (inside_code) {
// inside the code we descape stuff
lines[i] = lines[i]
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, '\'');
}
if (lines[i].startsWith('```')) {
inside_code = ! inside_code;
}
out += lines[i] + '\n';
}
return out;
},
// The idea here is to perform fast updates.
// See http://stackoverflow.com/questions/11228558/let-pagedown-and-mathjax-work-together/21563171?noredirect=1#comment46869312_21563171
// But our implementation is a bit buggy: flickering, bad rendering when someone types very fast.
//
// If you want to enable such buggy fast updates, you should
// add something like onkeypress="Preview.UpdateKeyPress(event)" to textarea's attributes.
UpdateKeyPress: function (event) {
if (event.keyCode < 16 || event.keyCode > 47) {
this.preview.innerHTML = '<p>' + marked(this.textarea.value) + '</p>';
this.buffer.innerHTML = '<p>' + marked(this.textarea.value) + '</p>';
}
this.Update();
}
};
//
// Cache a callback to the CreatePreview action
//
Preview.callback = MathJax.Callback(["CreatePreview",Preview]);
Preview.callback.autoReset = true; // make sure it can run more than once</script>
</head>
<body>
<div style="float:right">
<a href="https://github.com/kerzol/markdown-mathjax">Get the source of this page</a>
</div>
<h4>Type something:</h4>
<textarea id="marked-mathjax-input"
onkeyup="Preview.Update()"
name="comment"
"autofocus">
```
#include<cmath>
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
```
##### test
$ \Gamma, G$
$A_G A_\Gamma$
> test
>> $\Gamma$ [Example](http://exmaple.com)
<marquee> NOT! </marquee>
$$
\begin{array}{|c|c|c|c|}
\hline
1& 1 > -1 & 1 & -1 \\ \hline
& 3 & 55 & 44\\ \hline
& 4 & 93 & 33\\ \hline
& 5 & 6 & 22\\ \hline
\end{array}$$
```
if (1 <b> a </b> 2)
```
</textarea>
<div class="hint">Use
<a href="http://en.wikibooks.org/wiki/LaTeX/Mathematics">$\LaTeX$</a>
to type formulæ
and <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">markdown</a> to format text.
</div>
<div class="preview" id="marked-mathjax-preview"></div>
<div class="preview" id="marked-mathjax-preview-buffer"
style="display:none;
position:absolute;
top:0; left: 0"></div>
<script>
Preview.Init();
Preview.Update();
</script>
</body>
</html>