zestedesavoir / zmarkdown

Live demo: https://zestedesavoir.github.io/zmarkdown/
MIT License
224 stars 52 forks source link

KaTeX: TypeError: Cannot read properties of undefined (reading 'type') #476

Open philippemilink opened 1 year ago

philippemilink commented 1 year ago

Sentry reported the following issue:

TypeError: Cannot read properties of undefined (reading 'type')
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 9987, col 17, in Object.mathmlBuilder [as array]
    if (cols[0].type === "separator") {
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 7178, col 50, in buildGroup
    var result = _mathmlGroupBuilders[group.type](group, options);
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 7095, col 17, in buildExpression
    var group = buildMathML_buildGroup(expression[0], options);
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 7194, col 20, in buildMathML
    var expression = buildMathML_buildExpression(tree, options); // Wrap up the expression in an mrow so it is presented in the semantics
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 7263, col 22, in buildTree
    var mathMLNode = buildMathML(tree, expression, options, false);
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 17303, col 12, in renderToDomTree
    return buildTree_buildTree(tree, expression, settings);
  File "/opt/zds/app/zmd/node_modules/katex/dist/katex.js", line 17263, col 16, in renderToString
    var markup = katex_renderToDomTree(expression, options).toMarkup();
  File "/opt/zds/app/zmd/node_modules/rehype-katex/index.js", line 48, col 18, in onelement
    result = katex(
  File "/opt/zds/app/zmd/node_modules/unist-util-visit/index.js", line 27, col 12, in overload
    return visitor(node, index, parent)
  File "/opt/zds/app/zmd/node_modules/unist-util-visit-parents/index.js", line 56, col 27, in visit
    result = toResult(visitor(node, parents))

I have the full content that triggered this error, but I managed to make https://zestedesavoir.github.io/zmarkdown/ stop render the Markdown with only this content (probably the above error is occuring):

zz

$$
\begin{array}{}
t\\
t
\end{array}
$$

The LaTeX equation does not seem to be valid (I get an error when trying to build a LaTeX file containing this equation), but I guess zmd should rather just issue a warning, not an error.

StaloneLab commented 1 year ago

I planned to fix this issue in ZMd 11.2, but unfortunately, it seems to be an upstream problem: KaTeX is throwing an error because of incorrect behavior instead of returning it properly. I need to check if it still fails with newer versions of KaTeX before opening an issue on their bug tracker.