Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when using react-syntax-highlighter #570

Closed
lonkaars opened this issue Apr 14, 2021 · 14 comments
Closed

TypeError when using react-syntax-highlighter #570

lonkaars opened this issue Apr 14, 2021 · 14 comments
Labels
💪 phase/solved Post is done 🐛 type/bug This is a problem

Comments

@lonkaars
Copy link

Subject of the issue

I get this error when I try to use react-syntax-highlighter with react-markdown:

TypeError: Cannot read property 'children' of undefined
    at mergeNodeChildren (.../node_modules/react-markdown/lib/ast-to-react.js:246:16)
    at getNodeProps (.../node_modules/react-markdown/lib/ast-to-react.js:206:40)
    at astToReact (.../node_modules/react-markdown/lib/ast-to-react.js:37:19)
    at .../node_modules/react-markdown/lib/ast-to-react.js:42:14
    at Array.map (<anonymous>)
    at resolveChildren (.../node_modules/react-markdown/lib/ast-to-react.js:41:43)
    at astToReact (.../node_modules/react-markdown/lib/ast-to-react.js:38:73)
    at .../node_modules/react-markdown/lib/react-markdown.js:48:12
    at Array.forEach (<anonymous>)
    at ReactMarkdown (.../node_modules/react-markdown/lib/react-markdown.js:47:40)
    at ReactMarkdownWithHtml (.../node_modules/react-markdown/lib/with-html.js:13:10)
    at processChild (.../node_modules/react-dom/cjs/react-dom-server.node.development.js:3353:14)
    at resolve (.../node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5)
    at ReactDOMServerRenderer.render (.../node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22)
    at ReactDOMServerRenderer.read (.../node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)
    at renderToString (.../node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)

It looks like an internal react-markdown error so I figured this is the right place to submit this issue

Your environment

  • OS: Arch Linux (kernel 5.11.13)
  • Packages:
    typescript@4.2.3
    react-markdown@5.0.3
    react-syntax-highlighter@15.4.3
    next@10.0.9
    react@17.0.2
  • Env: Node 15.14.0, yarn 1.22.10, Brave (chromium) 89.0.4389.114

Steps to reproduce

import gfm from 'remark-gfm';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';

export function RenderedArticle(props: { content: string; }) {
	return <ReactMarkdownWithHTML
		plugins={[gfm]}
		allowDangerousHtml
		children={props.content}
		renderers={{
			code: (props: {
				language: string;
				value: ReactNode;
			}) {
				return <SyntaxHighlighter language={props.language} children={props.value}/>;
			}
		}}
	/>;
}

Expected behavior

The code should compile fine and display beautiful highlighted code ✨

Actual behavior

TypeError: Cannot read property 'children' of undefined :(

@lonkaars lonkaars added 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Apr 14, 2021
@wooorm
Copy link
Member

wooorm commented Apr 14, 2021

What text are you passing?

@lonkaars
Copy link
Author

I tried console.log()ing the props, but they're both undefined inside the custom code renderer. The input should be:

```py
# api/tests/example.py
from flask import Blueprint

example = Blueprint('example', __name__)

@example.route('/example')
def index():
	# python dictionaries are automatically converted to JSON by flask
    return {"hello": "world"}, 200 # flask returns http code 200 by default if no code is explicitly defined

# define a `dynamic_route` variable at the end of your endpoint definition file
# dynamic_route[0] is the namespace
# dynamic_route[1] is your flask.Blueprint
dynamic_route = ["/tests", status]

# this endpoint will be routed to /tests/example
#                                  \___/ \_____/
#                                    |      |
#                                    |      endpoint (defined by the @Blueprint.route() decorator)
#                                    |
#                                    namespace (defined in dynamic_route variable)
\```

I don't think this has anything to do with props being redefined inside the code renderer, because it's also undefined when the code renderer is moved to a seperate function. I've also tried calling the <SyntaxRenderer> component directly with the same results.

@wooorm
Copy link
Member

wooorm commented Apr 14, 2021

Probably the old broken HTML parser. So this should be fixed by #563, which’ll land soon, as a major new version

@lonkaars
Copy link
Author

lonkaars commented Apr 14, 2021

Thanks! Is there any way I could update to the new html parser? I read about a new upcoming release in the readme, but can I just yarn upgrade react-markdown --latest, or are there more steps involved?

@wooorm
Copy link
Member

wooorm commented Apr 14, 2021

Going to release it today/tomorrow or so. First have to think if there should be some more things landing with it

@wooorm
Copy link
Member

wooorm commented Apr 15, 2021

Released!

@wooorm wooorm closed this as completed Apr 15, 2021
@wooorm wooorm added ⛵️ status/released 🐛 type/bug This is a problem and removed 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Apr 15, 2021
@jahirfiquitiva
Copy link

jahirfiquitiva commented Apr 15, 2021

@lonkaars @wooorm

Would you mind helping me to fix this? I started using React Syntax Highlighter and now the inline code looks like this:

Shot 2021-04-15 at 03-28-41@2x

But I was expecting it to look like this
Shot 2021-04-15 at 03-29-02@2x

Here's the code snippets:

const CodeRenderer = (props: any) => {
  return <SyntaxHighlighter language={props.language} style={dracula}>
    {props.children}
  </SyntaxHighlighter>;
};
<ReactMarkdown
  plugins={[gfm]}
  components={{
    // @ts-ignore
    // eslint-disable-next-line react/display-name
    code: ({ node, ...props }) => <CodeRenderer {...props} />,
  }}>
  {mdBody}
</ReactMarkdown>

@wooorm
Copy link
Member

wooorm commented Apr 15, 2021

Please see the readme, it has examples: https://github.com/remarkjs/react-markdown#use-custom-components-syntax-highlight

@jahirfiquitiva

This comment has been minimized.

@wooorm

This comment has been minimized.

@lonkaars

This comment has been minimized.

@shuent

This comment has been minimized.

@SalahAdDin

This comment has been minimized.

@ChristianMurphy

This comment has been minimized.

@wooorm wooorm added the 💪 phase/solved Post is done label Aug 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 phase/solved Post is done 🐛 type/bug This is a problem
Development

No branches or pull requests

6 participants