In this version, the rendered structure includes the predefined <math></math> tags.
private async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
return new Promise<void>((resolve, reject) => {
const script: HTMLScriptElement = document.createElement("script");
script.id = config.id;
script.type = "text/javascript";
script.src = config.source;
// script.integrity = config.integrity;
script.crossOrigin = "anonymous";
script.async = true;
script.onload = () => resolve();
script.onerror = error => reject(error);
document.head.appendChild(script);
});
}
private async registerMathJaxAsync(config: MathJaxConfig): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (document.getElementById(config.id)) {
console.warn('MathJax script already loaded');
resolve();
return;
}
// ✅ Enhanced MathJax v4 configuration to preserve MathML
(window as any).MathJax = {
startup: {
elements: null, // Process all math elements automatically
// pageReady() {
// // Preserve original math elements before processing
// const mathElements = document.querySelectorAll('math');
// mathElements.forEach((math: Element) => {
// // Store original MathML as data attribute
// const mathml = math.outerHTML;
// math.setAttribute('data-original-mathml', mathml);
// // Create a hidden copy for restoration
// if (!math.nextElementSibling || !math.nextElementSibling.classList.contains('original-math')) {
// const clone = math.cloneNode(true) as HTMLElement;
// clone.classList.add('original-math');
// clone.style.display = 'none';
// clone.style.position = 'absolute';
// clone.style.left = '-9999px';
// math.parentNode?.insertBefore(clone, math.nextSibling);
// }
// });
// return (window as any).MathJax.startup.defaultPageReady();
// }
},
options: {
enableAssistiveMml: true,
renderActions: {
// This helps preserve accessibility MathML
addMenu: [0, '', '']
}
},
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
},
svg: {
// Better preservation of source
fontCache: 'global'
}
};
const script = document.createElement('script');
script.id = config.id;
script.type = 'text/javascript';
script.src = config.source;
script.crossOrigin = 'anonymous';
script.defer = true;
script.onload = () => {
if (window.MathJax && window.MathJax.startup?.promise) {
window.MathJax.startup.promise.then(() => resolve());
} else {
resolve();
}
};
script.onerror = (error) => {
console.error('Failed to load MathJax script', error);
reject(error);
};
document.head.appendChild(script);
});
}