Mathpix / mathpix-markdown-it

Markdown rendering + Latex extras (equations, tables, ...), with conversion features, for the scientific community
MIT License
539 stars 45 forks source link

Loading fails if body does not contain div #329

Open voegtlel opened 5 months ago

voegtlel commented 5 months ago

Hello,

it may seem strange, but this fails:

<html><head><script>
document.addEventListener("DOMContentLoaded", function() {
  // Uncommenting this makes the loading succeed. It must be a div, e.g. a table does not help.
  //document.body.appendChild(document.createElement('div'));
  let script = document.createElement('script');
  script.src = "https://cdn.jsdelivr.net/npm/mathpix-markdown-it@1.3.8/es5/bundle.js";
  script.onload = function() { window.loadMathJax(); };
  document.head.append(script);
});
</script></head><body></body></html>

with the error:

Error load MathJax => Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

it fails in this place in the bundle.js:

                            if (!document.getElementById("SVG-styles")) {
                                var s = u.MathJax.Stylesheet();
                                document.head.appendChild(s)
                            }

apparently, in the error case, s is not an html node, after adding a div node to the body (the commented line), it becomes a proper html node.

I'm not blocked by this (workaround is commented out), but found it a strange situation and hard to find the error, so I believe this should be fixed.