mathjax2.js (2094B)
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 export const MathJax2 = () => { 8 9 // The reveal.js instance this plugin is attached to 10 let deck; 11 12 let defaultOptions = { 13 messageStyle: 'none', 14 tex2jax: { 15 inlineMath: [ [ '$', '$' ], [ '\\(', '\\)' ] ], 16 skipTags: [ 'script', 'noscript', 'style', 'textarea', 'pre' ] 17 }, 18 skipStartupTypeset: true 19 }; 20 21 function loadScript( url, callback ) { 22 23 let head = document.querySelector( 'head' ); 24 let script = document.createElement( 'script' ); 25 script.type = 'text/javascript'; 26 script.src = url; 27 28 // Wrapper for callback to make sure it only fires once 29 let finish = () => { 30 if( typeof callback === 'function' ) { 31 callback.call(); 32 callback = null; 33 } 34 } 35 36 script.onload = finish; 37 38 // IE 39 script.onreadystatechange = () => { 40 if ( this.readyState === 'loaded' ) { 41 finish(); 42 } 43 } 44 45 // Normal browsers 46 head.appendChild( script ); 47 48 } 49 50 return { 51 id: 'mathjax2', 52 53 init: function( reveal ) { 54 55 deck = reveal; 56 57 let revealOptions = deck.getConfig().mathjax2 || deck.getConfig().math || {}; 58 59 let options = { ...defaultOptions, ...revealOptions }; 60 let mathjax = options.mathjax || 'https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js'; 61 let config = options.config || 'TeX-AMS_HTML-full'; 62 let url = mathjax + '?config=' + config; 63 64 options.tex2jax = { ...defaultOptions.tex2jax, ...revealOptions.tex2jax }; 65 66 options.mathjax = options.config = null; 67 68 loadScript( url, function() { 69 70 MathJax.Hub.Config( options ); 71 72 // Typeset followed by an immediate reveal.js layout since 73 // the typesetting process could affect slide height 74 MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, deck.getRevealElement() ] ); 75 MathJax.Hub.Queue( deck.layout ); 76 77 // Reprocess equations in slides when they turn visible 78 deck.on( 'slidechanged', function( event ) { 79 80 MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] ); 81 82 } ); 83 84 } ); 85 86 } 87 } 88 89 };