load-mathjax.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import getMeta from '../../utils/meta'
  2. let mathJaxPromise: Promise<typeof window.MathJax>
  3. export const loadMathJax = async (options?: {
  4. enableMenu?: boolean
  5. numbering?: string
  6. singleDollar?: boolean
  7. useLabelIds?: boolean
  8. }) => {
  9. if (!mathJaxPromise) {
  10. mathJaxPromise = new Promise((resolve, reject) => {
  11. options = {
  12. enableMenu: false,
  13. singleDollar: true,
  14. useLabelIds: false,
  15. ...options,
  16. }
  17. const inlineMath = [['\\(', '\\)']]
  18. if (options.singleDollar) {
  19. inlineMath.push(['$', '$'])
  20. }
  21. // https://docs.mathjax.org/en/v3.2-latest/upgrading/v2.html
  22. window.MathJax = {
  23. // https://docs.mathjax.org/en/latest/options/input/tex.html#the-configuration-block
  24. tex: {
  25. macros: {
  26. // Implements support for the \bm command from the bm package. It bolds the argument in math mode.
  27. // https://github.com/mathjax/MathJax/issues/1219#issuecomment-341059843
  28. bm: ['\\boldsymbol{#1}', 1],
  29. },
  30. inlineMath,
  31. displayMath: [
  32. ['\\[', '\\]'],
  33. ['$$', '$$'],
  34. ],
  35. packages: {
  36. '[-]': [
  37. 'html', // avoid creating HTML elements/attributes
  38. 'require', // prevent loading disabled packages
  39. ],
  40. },
  41. processEscapes: true,
  42. processEnvironments: true,
  43. useLabelIds: options.useLabelIds,
  44. tags: options.numbering,
  45. },
  46. loader: {
  47. load: [
  48. 'ui/safe', // https://docs.mathjax.org/en/latest/options/safe.html
  49. ],
  50. },
  51. options: {
  52. enableMenu: options.enableMenu, // https://docs.mathjax.org/en/latest/options/menu.html
  53. },
  54. startup: {
  55. typeset: false,
  56. pageReady() {
  57. // disable the "Math Renderer" option in the context menu,
  58. // as only SVG is available
  59. window.MathJax.startup.document.menu.menu
  60. .findID('Renderer')
  61. .disable()
  62. },
  63. ready() {
  64. window.MathJax.startup.defaultReady()
  65. // remove anything from the "font-family" attribute after a semicolon
  66. // so it's not added to the style attribute
  67. // https://github.com/mathjax/MathJax/issues/3129#issuecomment-1807225345
  68. const { safe } = window.MathJax.startup.document
  69. safe.filterAttributes.set('fontfamily', 'filterFontFamily')
  70. safe.filterMethods.filterFontFamily = (
  71. _safe: any,
  72. family: string
  73. ) => family.split(/;/)[0]
  74. },
  75. },
  76. }
  77. const script = document.createElement('script')
  78. const path = getMeta('ol-mathJaxPath')
  79. if (!path) {
  80. reject(new Error('No MathJax path found'))
  81. return
  82. }
  83. script.src = path
  84. script.addEventListener('load', async () => {
  85. await window.MathJax.startup.promise
  86. document.head.appendChild(window.MathJax.svgStylesheet())
  87. resolve(window.MathJax)
  88. })
  89. script.addEventListener('error', reject)
  90. document.head.append(script)
  91. })
  92. }
  93. return mathJaxPromise
  94. }