Skip to content

Instantly share code, notes, and snippets.

@npisenti
Created July 1, 2012 17:05
Show Gist options
  • Save npisenti/3028983 to your computer and use it in GitHub Desktop.
Save npisenti/3028983 to your computer and use it in GitHub Desktop.
Mathjax + Showdown (markdown)
/* 'showjax' script from ls_atomic.
* used to render markdown (via showdown.js)
* and then render mathjax.
*/
$(document).ready(function() {
$(".markdown").each(function() { $(this).markdown(); }); // when document loads, render markdown + mathjax
$(".jaxonly").each(function() {$(this).renderJax(); }); // render sections that are mathjax-only
});
// jQuery 'markdown' plugin
//
$.fn.markdown = function(typeset) {
// It appears to typeset the whole page if it doesn't find the ID. Which
// works, but isn't all that great.
mathDiv = ($(this).attr("id"));
converter = new Showdown.converter();
$(this).html(converter.makeHtml($(this).text())); // renders markdown for given DOM element
if (typeof(typeset) == "undefined" || typeset == true) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, mathDiv]); // renders mathjax if 'typeset' is true (or undefined)
typesetStubbornMath();
}
}
// Render the bits of math that have inexplicably still failed to render, while
// leaving the rest alone. (If you try to typeset the whole page, it will break
// other things).
function typesetStubbornMath() {
$(".MathJax_Preview").each( function() {
if($(this).text() != "") {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, $(this).attr("id")]);
}
});
}
// Not sure where this came into play...
// But, it renders whole page in markdown alone, then comes through again to render mathjax.
$.fn.markdownInner = function() {
$(this).find(".markdown").each(function() {
$(this).markdown(false); });
MathJax.Hub.Queue(["Typeset", MathJax.Hub, mathDiv]);
typesetStubbornMath();
}
// simple jQuery plugin to render mathjax independently of markdown
$.fn.renderJax = function() {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, $(this).attr("id")]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment