Skip to content

Instantly share code, notes, and snippets.

@reitzig
Created November 17, 2015 10:27
Show Gist options
  • Save reitzig/bc87e2dd506bcb9f7328 to your computer and use it in GitHub Desktop.
Save reitzig/bc87e2dd506bcb9f7328 to your computer and use it in GitHub Desktop.
MathJax for Slack -- Attempt 1
( function () { // start of anonymous wrapper function (needed to restrict variable scope on Opera)
// Opera does not support @match, so re-check that we're on SE chat before doing anything
// if ( location.hostname != 'slack.com' ) return;
// Baseline MathJax URL and config, copied from SE:
var mathJaxURL = "//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full";
var config = {
"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: 50 },
tex2jax: { inlineMath: [ ["$", "$"], ["\\\\(","\\\\)"] ], displayMath: [ ["$$","$$"], ["\\[", "\\]"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" },
TeX: { noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
messageStyle: "none"
};
// Inject MathJax config and chat polling code to page:
var configScript = document.createElement( 'script' );
configScript.type = 'text/x-mathjax-config';
configScript.textContent = "MathJax.Hub.Config(" + JSON.stringify(config) + ");";
document.head.appendChild( configScript );
// Load MathJax itself:
var mathJaxScript = document.createElement( 'script' );
mathJaxScript.type = "text/javascript";
mathJaxScript.src = mathJaxURL;
document.head.appendChild( mathJaxScript );
// The function that makes sure the given element gets compiled by MathJax.
function mathjaxify(elem) {
console.log('ping', elem);
MathJax.Hub.Queue(["Typeset",MathJax.Hub,elem]);
elem.addClass('mathjaxed');
}
$(document).ready(function() {
// Install hook
$("#msgs_div").on('DOMSubtreeModified propertychange', function() {
//$("span.message_body:not(.mathjaxed)").each(function() {
// mathjaxify($(this));
//});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
});
});
} )(); // end of anonymous wrapper function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment