Path: blob/main/src/resources/formats/revealjs/reveal/plugin/math/mathjax2.js
12923 views
/**1* A plugin which enables rendering of math equations inside2* of reveal.js slides. Essentially a thin wrapper for MathJax.3*4* @author Hakim El Hattab5*/6export const MathJax2 = () => {78// The reveal.js instance this plugin is attached to9let deck;1011let defaultOptions = {12messageStyle: 'none',13tex2jax: {14inlineMath: [ [ '$', '$' ], [ '\\(', '\\)' ] ],15skipTags: [ 'script', 'noscript', 'style', 'textarea', 'pre' ]16},17skipStartupTypeset: true18};1920function loadScript( url, callback ) {2122let head = document.querySelector( 'head' );23let script = document.createElement( 'script' );24script.type = 'text/javascript';25script.src = url;2627// Wrapper for callback to make sure it only fires once28let finish = () => {29if( typeof callback === 'function' ) {30callback.call();31callback = null;32}33}3435script.onload = finish;3637// IE38script.onreadystatechange = () => {39if ( this.readyState === 'loaded' ) {40finish();41}42}4344// Normal browsers45head.appendChild( script );4647}4849return {50id: 'mathjax2',5152init: function( reveal ) {5354deck = reveal;5556let revealOptions = deck.getConfig().mathjax2 || deck.getConfig().math || {};5758let options = { ...defaultOptions, ...revealOptions };59let mathjax = options.mathjax || 'https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js';60let config = options.config || 'TeX-AMS_HTML-full';61let url = mathjax + '?config=' + config;6263options.tex2jax = { ...defaultOptions.tex2jax, ...revealOptions.tex2jax };6465options.mathjax = options.config = null;6667loadScript( url, function() {6869MathJax.Hub.Config( options );7071// Typeset followed by an immediate reveal.js layout since72// the typesetting process could affect slide height73MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, deck.getRevealElement() ] );74MathJax.Hub.Queue( deck.layout );7576// Reprocess equations in slides when they turn visible77deck.on( 'slidechanged', function( event ) {7879MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] );8081} );8283} );8485}86}8788};899091