GitHub code search
Searched for 'MathJax' in discourse/discourse-math
GitHub file content
Retrieved content of assets/javascripts/initializers/discourse-math-mathjax.js from discourse/discourse-math@main
In the file assets/javascripts/initializers/discourse-math-mathjax.js
, the initMathJax
, decorate
, and mathjax
functions should be checked and potentially modified to ensure that Chinese punctuation is properly supported.
Identifying the Issue:
The main issue lies in the function decorate
, where it’s handling the creation of mathScript
and mathWrapper
elements that render the MathJax content. If Chinese punctuation is not being processed correctly, it’s likely due to how the mathScript.innerText
is being populated and subsequently rendered by MathJax.
Potential Fix:
To support Chinese punctuation, we should ensure that the content being passed to MathJax can handle multiple punctuation types, including those used in Chinese. MathJax’s configuration should allow for this.
Here’s a potential fix:
- Improving Regex (if needed): Modify the text processing part to ensure punctuations are correctly interpreted by MathJax.
- Additional MathJax Configuration: Customize MathJax configuration to include additional character sets or update the current input logic.
Proposed Update to decorate
function:
function decorate(elem, isPreview) {
if (elem.dataset.appliedMathjax) {
return;
}
elem.dataset.appliedMathjax = true;
let tag, classList, type;
if (elem.classList.contains("math")) {
tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : "";
const displayClass = tag === "div" ? "block-math" : "inline-math";
type = `math/tex${display}`;
classList = `math-container ${displayClass} mathjax-math`;
} else if (elem.classList.contains("asciimath")) {
tag = "span";
classList = "math-container inline-math ascii-math";
type = "math/asciimath";
}
const mathScript = document.createElement("script");
mathScript.type = type;
// Fix: Ensure proper handling of multi-language punctuation
mathScript.innerHTML = elem.innerText.replace(/,/g, ",").replace(/。/g, ".");
const mathWrapper = document.createElement(tag);
mathWrapper.classList.add(...classList.split(" "));
mathWrapper.style.display = "none";
mathWrapper.appendChild(mathScript);
elem.after(mathWrapper);
later(
this,
() => {
window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM
if (elem?.parentElement?.offsetParent !== null) {
window.MathJax.Hub.Typeset(mathScript, () => {
elem.style.display = "none";
mathWrapper.style.display = null;
});
}
});
},
isPreview ? 200 : 0
);
}
This modification ensures that any Chinese punctuations like ,
and 。
are replaced with their English counterparts before MathJax processes the content. This might solve the rendering issue. If further customization is needed, you might need to delve deeper into MathJax configurations and extensions.