mathjax.js 1.2 KB

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