Skip to content

Instantly share code, notes, and snippets.

@jcmckeown
Last active April 8, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcmckeown/0d00c005d0b4ff213707 to your computer and use it in GitHub Desktop.
Save jcmckeown/0d00c005d0b4ff213707 to your computer and use it in GitHub Desktop.
If you're using intensedebate comments on a page that already includes mathjax, this plugin script (to enter here: (http://intensedebate.com/pluginEditor/) ) will use the ambient mathjax to typeset maths in the comments. I have this running in my tumblr, http://jessecmckeown.tumblr.com .
var id_math_stuff = {
// id_math_stuff.theMJ
theMJ : window.MathJax,
// id_math_stuff.idMathRun =
idMathRun : function () {
if (id_math_stuff.theMJ) {
id_math_stuff.theMJ.Hub.Queue(["Typeset", id_math_stuff.theMJ.Hub, "idc-cover"]);
} else {
console.log("there is still no mathjax?");
}
},
// id_math_stuff.saveEscapes =
saveEscapes : function (commentext) {
var the_text = commentext.replace(/\?/ig, "?a");
the_text = the_text.replace(/\x5c/ig, "?b");
return the_text;
},
// id_math_stuff.escapeEscapes =
escapeEscapes : function (argRec) {
var the_text = argRec.text;
the_text = the_text.replace(/\?b/ig, "\\");
the_text = the_text.replace(/\?a/ig, "?");
return the_text;
},
// id_math_stuff.switchEdit =
switchEdit : function () {
if (!id_math_stuff.editComment) {
id_math_stuff.editComment = window.IDeditComment;
window.IDeditComment = function (commentid) {
var elt, nowJax, delim, t;
/** because it only seems sensible */
if (!commentObj.curUser.userid || commentObj.curUser.userid <= 0) {
return;
}
/** There shouldn't be scripts that aren't mathjax's! */
elt = document.getElementById("IDComment-CommentText" + commentid).getElementsByTagName("script");
while ( elt[0] ) {
t = elt [0] ;
nowJax = id_math_stuff.theMJ.Hub.getJaxFor(t);
delim = /display/.test(t.type) ? "$$" : "$";
t.insertAdjacentHTML('afterend', delim + nowJax.originalText + delim);
nowJax.Remove();
t.parentNode.removeChild(t);
}
id_math_stuff.editComment(commentid);
};
}
if (!id_math_stuff.saveComment) {
id_math_stuff.saveComment = window.IDsaveComment;
window.IDsaveComment = function (commentid) {
var textare = document.getElementById( "IDEditCommentTextArea"+commentid);
var theString = id_math_stuff.saveEscapes(textare.value);
textare.value = theString;
id_math_stuff.saveComment(commentid);
var ctext = document.getElementById( "IDComment-CommentText" + commentid);
ctext.innerHTML = id_math_stuff.escapeEscapes( ctext.innerHTML );
id_math_stuff.theMJ.Hub.Queue(["Typeset", id_math_stuff.theMJ.Hub, "idc-cover"]);
}
}
}
};
id_add_action('comment_post', id_math_stuff.idMathRun, 45);
id_add_action('idcomments_func_load', id_math_stuff.switchEdit, 99);
id_add_filter('pre_comment_text', id_math_stuff.saveEscapes, 2);
id_add_filter('comment_text_load', id_math_stuff.escapeEscapes, 40);
@jcmckeown
Copy link
Author

In 5th revision, playing with js/DOM idioms; particularly, from developer.mozilla.org,

Returns a list of elements with the given tag name. The subtree underneath the specified element is searched, excluding the element itself. The returned list is live, meaning that it updates itself with the DOM tree automatically. Consequently, there is no need to call several times element.getElementsByTagName with the same element and arguments.

This was throwing me for a bad loop (!) previously.

In the meantime, still don't know how to queue mathjax re-typesets, but never mind that for now.

@jcmckeown
Copy link
Author

In Revision 6, improving approximate idempotency in do-nothing edits, and correctness of editing generally. It seems I _still_ don't grok MathJax.Hub.Queue calls, to Typeset or otherwise. I'm sure that I'm doing everything MathJax.com says I should, but maybe... maybe I'm not?

@jcmckeown
Copy link
Author

Upon reconsideration, the timing-like-issue in the previous observation might be due to "id_math_stuff.saveComment" (the saved IDsaveComment) doing asynchronous things and on the whole returning too quickly. Or maybe not. I don't want to dig into the original IDsaveComment code trying to see what it does because they seem not to want me "reverse-engineering" things.

@jcmckeown
Copy link
Author

v. 7

  • ... firefox(?) changed(? fixed?) the semantics of javascript object member names(?) ... anyways, something broke the fetching the window.MathJax object, from id_math_stuff.editComment's perspective;
  • for independent reasons, discovered that "nowjax.HTMLCSS" is the wrong route to the correct delim, but on the other hand I haven't found the correct general schema yet. We'll call the fixed version v.7.1 .

@jcmckeown
Copy link
Author

now v 7.1 ! MathJax kindly leaves "display" in the script "type" HTML attribute, whatever OutputJax one is using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment