JJ Allaire | efa6ad4 | 2016-01-30 13:12:05 -0500 | [diff] [blame] | 1 | /** |
| 2 | * A plugin which enables rendering of math equations inside |
| 3 | * of reveal.js slides. Essentially a thin wrapper for MathJax. |
| 4 | * |
| 5 | * @author Hakim El Hattab |
| 6 | */ |
| 7 | var RevealMath = window.RevealMath || (function(){ |
| 8 | |
| 9 | var options = Reveal.getConfig().math || {}; |
| 10 | options.mathjax = options.mathjax || 'https://cdn.mathjax.org/mathjax/latest/MathJax.js'; |
| 11 | options.config = options.config || 'TeX-AMS_HTML-full'; |
| 12 | |
| 13 | loadScript( options.mathjax + '?config=' + options.config, function() { |
| 14 | |
| 15 | MathJax.Hub.Config({ |
| 16 | messageStyle: 'none', |
| 17 | tex2jax: { |
| 18 | inlineMath: [['$','$'],['\\(','\\)']] , |
| 19 | skipTags: ['script','noscript','style','textarea','pre'] |
| 20 | }, |
| 21 | skipStartupTypeset: true |
| 22 | }); |
| 23 | |
| 24 | // Typeset followed by an immediate reveal.js layout since |
| 25 | // the typesetting process could affect slide height |
| 26 | MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] ); |
| 27 | MathJax.Hub.Queue( Reveal.layout ); |
| 28 | |
| 29 | // Reprocess equations in slides when they turn visible |
| 30 | Reveal.addEventListener( 'slidechanged', function( event ) { |
| 31 | |
| 32 | MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] ); |
| 33 | |
| 34 | } ); |
| 35 | |
| 36 | } ); |
| 37 | |
| 38 | function loadScript( url, callback ) { |
| 39 | |
| 40 | var head = document.querySelector( 'head' ); |
| 41 | var script = document.createElement( 'script' ); |
| 42 | script.type = 'text/javascript'; |
| 43 | script.src = url; |
| 44 | |
| 45 | // Wrapper for callback to make sure it only fires once |
| 46 | var finish = function() { |
| 47 | if( typeof callback === 'function' ) { |
| 48 | callback.call(); |
| 49 | callback = null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | script.onload = finish; |
| 54 | |
| 55 | // IE |
| 56 | script.onreadystatechange = function() { |
| 57 | if ( this.readyState === 'loaded' ) { |
| 58 | finish(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Normal browsers |
| 63 | head.appendChild( script ); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | })(); |