Files
2014-08-06 14:36:14 -04:00

129 lines
4.1 KiB
JavaScript

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
});
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.timeout = null;
this.mjRunning = false;
this.oldText = null;
this.preview = document.getElementById("marked-mathjax-preview");
this.buffer = document.getElementById("marked-mathjax-preview-buffer");
this.preview.classList.add('Current'); //bad bugs happen if this isn't here.
},
//
// 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, preview = this.buffer;
this.buffer = buffer; this.preview = preview;
buffer.style.display = "none";
buffer.style.position = "absolute";
buffer.style.height = "auto"; //dunno
buffer.style.top = "0px";
buffer.style.left = "0px";
preview.style.display = "inline-block";
preview.style.position = ""; //dunno
preview.style.width = "90%";
preview.style.height = "auto";
preview.style.overflow = "auto";
buffer.classList.remove('Current');
preview.classList.add('Current');
// 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 = document.getElementById("marked-mathjax-input").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;
// replace occurrences of > at the beginning of a new line
// with > again, so Markdown blockquotes are handled correctly
text = text.replace(/^>/mg, '>');
this.buffer.innerHTML = marked (text);
this.SwapBuffers();
},
Escape: function (html, encode) {
return html
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
};
//
// 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