load-mathjax.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import getMeta from '../../utils/meta'
  2. let mathJaxPromise: Promise<typeof window.MathJax>
  3. export const loadMathJax = async () => {
  4. if (!mathJaxPromise) {
  5. mathJaxPromise = new Promise((resolve, reject) => {
  6. // https://docs.mathjax.org/en/v3.2-latest/upgrading/v2.html
  7. window.MathJax = {
  8. // https://docs.mathjax.org/en/latest/options/input/tex.html#the-configuration-block
  9. tex: {
  10. inlineMath: [
  11. ['\\(', '\\)'],
  12. ['$', '$'],
  13. ],
  14. displayMath: [
  15. ['\\[', '\\]'],
  16. ['$$', '$$'],
  17. ],
  18. packages: {
  19. '[-]': [
  20. 'html', // avoid creating HTML elements/attributes
  21. 'require', // prevent loading disabled packages
  22. ],
  23. },
  24. processEscapes: true,
  25. processEnvironments: true,
  26. },
  27. loader: {
  28. load: [
  29. 'ui/safe', // https://docs.mathjax.org/en/latest/options/safe.html
  30. ],
  31. },
  32. options: {
  33. enableMenu: false, // https://docs.mathjax.org/en/latest/options/menu.html
  34. },
  35. startup: {
  36. typeset: false,
  37. },
  38. }
  39. const script = document.createElement('script')
  40. script.src = getMeta('ol-mathJax3Path')
  41. script.addEventListener('load', async () => {
  42. await window.MathJax.startup.promise
  43. document.head.appendChild(window.MathJax.svgStylesheet())
  44. resolve(window.MathJax)
  45. })
  46. script.addEventListener('error', reject)
  47. document.head.append(script)
  48. })
  49. }
  50. return mathJaxPromise
  51. }