2023-01-25 05:13:29 -05:00
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
|
|
|
|
let mathJaxPromise: Promise<typeof window.MathJax>
|
|
|
|
|
2024-02-08 04:34:07 -05:00
|
|
|
export const loadMathJax = async (options?: {
|
|
|
|
enableMenu?: boolean
|
|
|
|
numbering?: string
|
|
|
|
singleDollar?: boolean
|
|
|
|
useLabelIds?: boolean
|
|
|
|
}) => {
|
2023-01-25 05:13:29 -05:00
|
|
|
if (!mathJaxPromise) {
|
|
|
|
mathJaxPromise = new Promise((resolve, reject) => {
|
2024-02-08 04:34:07 -05:00
|
|
|
options = {
|
|
|
|
enableMenu: false,
|
|
|
|
singleDollar: true,
|
|
|
|
useLabelIds: false,
|
|
|
|
...options,
|
|
|
|
}
|
|
|
|
|
|
|
|
const inlineMath = [['\\(', '\\)']]
|
|
|
|
if (options.singleDollar) {
|
|
|
|
inlineMath.push(['$', '$'])
|
|
|
|
}
|
|
|
|
|
2023-01-25 05:13:29 -05:00
|
|
|
// https://docs.mathjax.org/en/v3.2-latest/upgrading/v2.html
|
|
|
|
window.MathJax = {
|
|
|
|
// https://docs.mathjax.org/en/latest/options/input/tex.html#the-configuration-block
|
|
|
|
tex: {
|
2023-11-23 04:47:50 -05:00
|
|
|
macros: {
|
|
|
|
// Implements support for the \bm command from the bm package. It bolds the argument in math mode.
|
|
|
|
// https://github.com/mathjax/MathJax/issues/1219#issuecomment-341059843
|
|
|
|
bm: ['\\boldsymbol{#1}', 1],
|
|
|
|
},
|
2024-02-08 04:34:07 -05:00
|
|
|
inlineMath,
|
2023-01-25 05:13:29 -05:00
|
|
|
displayMath: [
|
|
|
|
['\\[', '\\]'],
|
|
|
|
['$$', '$$'],
|
|
|
|
],
|
|
|
|
packages: {
|
|
|
|
'[-]': [
|
|
|
|
'html', // avoid creating HTML elements/attributes
|
|
|
|
'require', // prevent loading disabled packages
|
|
|
|
],
|
|
|
|
},
|
|
|
|
processEscapes: true,
|
|
|
|
processEnvironments: true,
|
2024-02-08 04:34:07 -05:00
|
|
|
useLabelIds: options.useLabelIds,
|
|
|
|
tags: options.numbering,
|
2023-01-25 05:13:29 -05:00
|
|
|
},
|
|
|
|
loader: {
|
|
|
|
load: [
|
|
|
|
'ui/safe', // https://docs.mathjax.org/en/latest/options/safe.html
|
|
|
|
],
|
|
|
|
},
|
|
|
|
options: {
|
2024-02-08 04:34:07 -05:00
|
|
|
enableMenu: options.enableMenu, // https://docs.mathjax.org/en/latest/options/menu.html
|
2023-01-25 05:13:29 -05:00
|
|
|
},
|
|
|
|
startup: {
|
|
|
|
typeset: false,
|
2024-02-08 04:34:07 -05:00
|
|
|
pageReady() {
|
|
|
|
// disable the "Math Renderer" option in the context menu,
|
|
|
|
// as only SVG is available
|
|
|
|
window.MathJax.startup.document.menu.menu
|
|
|
|
.findID('Renderer')
|
|
|
|
.disable()
|
|
|
|
},
|
2023-01-25 05:13:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const script = document.createElement('script')
|
2024-02-08 04:34:07 -05:00
|
|
|
const path = getMeta('ol-mathJaxPath')
|
2023-08-29 06:14:04 -04:00
|
|
|
if (!path) {
|
|
|
|
reject(new Error('No MathJax path found'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
script.src = path
|
2023-01-25 05:13:29 -05:00
|
|
|
script.addEventListener('load', async () => {
|
|
|
|
await window.MathJax.startup.promise
|
|
|
|
document.head.appendChild(window.MathJax.svgStylesheet())
|
|
|
|
resolve(window.MathJax)
|
|
|
|
})
|
|
|
|
script.addEventListener('error', reject)
|
|
|
|
document.head.append(script)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return mathJaxPromise
|
|
|
|
}
|