remarkjs / react-markdown

Markdown component for React
https://remarkjs.github.io/react-markdown/
MIT License
13.26k stars 876 forks source link

An error occurs with certain text when using `remarkMath` and `rehypeKatex`. #853

Closed devleejb closed 3 months ago

devleejb commented 3 months ago

Initial checklist

Affected packages and versions

react-markdown:9.0.1

Link to runnable example

No response

Steps to reproduce

  1. Implement the following code. (Node 18, npm 10)
    <Markdown
        remarkPlugins={[remarkMath]}
        rehypePlugins={[rehypeKatex]}
    >
      {`$\\begin\\ca$`}
    </Markdown>
  2. Run the code.
  3. Check for any errors.

Expected behavior

It should render the markdown text correctly.

Actual behavior

Following error occurs.

image

Runtime

Other (please specify in steps to reproduce)

Package manager

Other (please specify in steps to reproduce)

OS

macOS

Build and bundle tools

Vite

wooorm commented 3 months ago

This looks like a KaTeX problem, why not raise it with KaTeX? Why raise it here? Do you think there is something to change here?

devleejb commented 3 months ago

@wooorm At first, I thought that it is a KaTex problem. But, when I run the following code, it runs correctly.

const file = await unified()
  .use(remarkParse)
  .use(remarkMath)
  .use(remarkRehype)
  .use(rehypeKatex)
  .use(rehypeStringify)
  .process(`$\\begin\\ca$`)
wooorm commented 3 months ago

Cannot reproduce:

import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkMath from 'remark-math'
import remarkRehype from 'remark-rehype'
import rehypeKatex from 'rehype-katex'
import rehypeStringify from 'rehype-stringify'

const file = await unified()
  .use(remarkParse)
  .use(remarkMath)
  .use(remarkRehype)
  .use(rehypeKatex)
  .use(rehypeStringify)
  .process(`$\\begin\\ca$`)

console.log(String(file))
file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:7482
    throw new Error("Expected node of type " + type + ", but got " + (node ? "node of type " + node.type : String(node)));
          ^

Error: Expected node of type textord, but got node of type color
    at assertNodeType (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:7482:11)
    at Object.handler (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:11491:18)
    at Parser.callFunction (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:17637:19)
    at Parser.parseFunction (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:17620:17)
    at Parser.parseGroup (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:18012:21)
    at Parser.parseAtom (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:17450:21)
    at Parser.parseExpression (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:17312:23)
    at Parser.parse (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:17244:24)
    at parseTree (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:18247:21)
    at renderToDomTree (file:///Users/tilde/Projects/oss/react-markdown/node_modules/katex/dist/katex.mjs:18337:16)

As in: there’s definitely a KaTeX error, when using the latest dependencies otherwise.

Now, rehype-katex also has a problem. It should catch that error.

devleejb commented 3 months ago

@wooorm Okay. Sorry, I'll check it again and raise it in KaTeX.

wooorm commented 3 months ago

OK, if you believe that math input to be valid for KaTeX.

I’ll also work on a fix in rehype-katex to swallow that error and display the raw input instead of crashing.

wooorm commented 3 months ago

In rehype-katex I am getting this error btw:

ParseError: KaTeX parse error: Undefined control sequence: \ca at position 7: \begin\̲c̲a̲
    at new ParseError (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:147:16)
    at Parser.parseGroup (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:18016:17)
    at Parser.parseAtom (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17450:21)
    at Parser.parseExpression (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17312:23)
    at Parser.parseArgumentGroup (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17955:27)
    at Parser.parseGroupOfType (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17705:21)
    at Parser.parseArguments (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17670:22)
    at Parser.parseFunction (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17619:14)
    at Parser.parseGroup (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:18012:21)
    at Parser.parseAtom (file:///Users/tilde/Projects/oss/remark-math/node_modules/katex/dist/katex.mjs:17450:21) {
  position: 6,
  length: 3,
  rawMessage: 'Undefined control sequence: \\ca'
}

So could be that that is the root problem

github-actions[bot] commented 3 months ago

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

github-actions[bot] commented 3 months ago

Hi team! Could you describe why this has been marked as external?

Thanks, — bb