mathjax.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* global MathJax */
  2. define(['base'], function(App) {
  3. return App.directive('mathjax', function($compile, $parse) {
  4. return {
  5. link(scope, element, attrs) {
  6. if (!(MathJax && MathJax.Hub)) return
  7. // Allowing HTML can be unsafe unless using something like
  8. // `ng-bind-html` because of potential Angular XSS via {{/}}
  9. if (!$parse(attrs.mathjaxAllowHtml)(scope)) {
  10. const mathJaxContents = element.html()
  11. const nonBindableEl = $compile('<span ng-non-bindable></span>')({})
  12. element.html('').append(nonBindableEl)
  13. nonBindableEl.html(mathJaxContents)
  14. }
  15. if (attrs.delimiter !== 'no-single-dollar') {
  16. const inlineMathConfig =
  17. MathJax.Hub.config && MathJax.Hub.config.tex2jax.inlineMath
  18. const alreadyConfigured = _.find(
  19. inlineMathConfig,
  20. c => c[0] === '$' && c[1] === '$'
  21. )
  22. if (!alreadyConfigured) {
  23. MathJax.Hub.Config({
  24. tex2jax: {
  25. inlineMath: inlineMathConfig.concat([['$', '$']])
  26. }
  27. })
  28. }
  29. }
  30. setTimeout(() => {
  31. MathJax.Hub.Queue(['Typeset', MathJax.Hub, element.get(0)])
  32. }, 0)
  33. }
  34. }
  35. })
  36. })