math.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. var options = Reveal.getConfig().math || {};
  9. var mathjax = options.mathjax || 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js';
  10. var config = options.config || 'TeX-AMS_HTML-full';
  11. var url = mathjax + '?config=' + config;
  12. var defaultOptions = {
  13. messageStyle: 'none',
  14. tex2jax: {
  15. inlineMath: [ [ '$', '$' ], [ '\\(', '\\)' ] ],
  16. skipTags: [ 'script', 'noscript', 'style', 'textarea', 'pre' ]
  17. },
  18. skipStartupTypeset: true
  19. };
  20. function defaults( options, defaultOptions ) {
  21. for ( var i in defaultOptions ) {
  22. if ( !options.hasOwnProperty( i ) ) {
  23. options[i] = defaultOptions[i];
  24. }
  25. }
  26. }
  27. function loadScript( url, callback ) {
  28. var head = document.querySelector( 'head' );
  29. var script = document.createElement( 'script' );
  30. script.type = 'text/javascript';
  31. script.src = url;
  32. // Wrapper for callback to make sure it only fires once
  33. var finish = function() {
  34. if( typeof callback === 'function' ) {
  35. callback.call();
  36. callback = null;
  37. }
  38. }
  39. script.onload = finish;
  40. // IE
  41. script.onreadystatechange = function() {
  42. if ( this.readyState === 'loaded' ) {
  43. finish();
  44. }
  45. }
  46. // Normal browsers
  47. head.appendChild( script );
  48. }
  49. return {
  50. init: function() {
  51. defaults( options, defaultOptions );
  52. defaults( options.tex2jax, defaultOptions.tex2jax );
  53. options.mathjax = options.config = null;
  54. loadScript( url, function() {
  55. MathJax.Hub.Config( options );
  56. // Typeset followed by an immediate reveal.js layout since
  57. // the typesetting process could affect slide height
  58. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
  59. MathJax.Hub.Queue( Reveal.layout );
  60. // Reprocess equations in slides when they turn visible
  61. Reveal.addEventListener( 'slidechanged', function( event ) {
  62. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] );
  63. } );
  64. } );
  65. // lofi xbrowser Promise.resolve()
  66. return { then: function( resolve ) { resolve(); }};
  67. }
  68. }
  69. })();
  70. Reveal.registerPlugin( 'math', RevealMath );