tillahoffmann / obsidian-jupyter

MIT License
437 stars 23 forks source link

Viewer doesn't properly work for MathJax parts in Markdown cells #36

Closed henkelma closed 2 years ago

henkelma commented 2 years ago

MathJax parts are only properly rendered if the window is in focus while Obsidian is started. In all other cases the MathJax source is visible instead to the rendered equations.

Seems to be caused by:

// https://stackoverflow.com/a/47614491/1150961.
function setInnerHTML(elm, html) {
    elm.innerHTML = html;
    Array.from(elm.querySelectorAll("script")).forEach(oldScript => {
        const newScript = document.createElement("script");
        Array.from(oldScript.attributes)
            .forEach(attr => newScript.setAttribute(attr.name, attr.value));
        newScript.appendChild(document.createTextNode(oldScript.innerHTML));
        oldScript.parentNode.replaceChild(newScript, oldScript);
    });
}

I replaced this with

function setInnerHTML(elm, text) {
    elm.innerHTML = text;
    var scripts = Array.prototype.slice.call(elm.getElementsByTagName("script"));
    for (var i = 0; i < scripts.length; i++) {
        if (scripts[i].src != "") {
            var tag = document.createElement("script");
            tag.src = scripts[i].src;
            document.getElementsByTagName("head")[0].appendChild(tag);
        }
        else {
            eval(scripts[i].innerHTML);
        }
    }
}

This leads to correctly rendered output in all cases. But during the first rendering the following exception is being raised:


Uncaught ReferenceError: init_mathjax is not defined
    at eval (eval at insertAndExecute (eval at <anonymous>```
henkelma commented 2 years ago

Forget the alternative code, it creates more problems than it actually solves. Being new to the js/html world it is tough to judge everything in detail. But from what I guess it would probably be much cleaner to embed the whole Jupyter html into an iframe. Need to experiment further an think about it...

henkelma commented 2 years ago

There is an incompatibility with the used MathJax version between Obsidian and Jupyter. Obsidian is using v3 and Jupyter is using v2. For now that means, that the generated html from an ipynb file can only "live" in an iFrame. Otherwise there will be problems with MathJax.

aidan-gibson commented 2 years ago

37

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

tillahoffmann commented 2 years ago

This is hopefully fixed by #40.