Closed aress31 closed 3 years ago
The error seems to indicate the problem is in the highlighting. What value are you highlighting?
I have just made some test to see whether the highlighting is working as it should or not, this is the content of my Landing.mdx
file:
### A few tests
```javascript
<div
style={{
backgroundColor: "rebeccapurple",
padding: "20px",
marginTop: "20px",
}}
/>
(supposed to be 3 backticks here but because of GitHub MD reader cant put them)
You can use more backticks (say, 5) on the outer code, if you want to include backticks (day, 4) inside the code!
@wooorm thanks for the comment but that's not the core issue here, the main problem is the above post and error. ☺
True! I’m on mobile now and was under the impression there were more positive or negative examples you provided, that were hidden because of that for some reason. If not: could you clarify what works and what doesn’t?
Everything works beside the CodeBlock
- I followed the tutorial on the MDX website. The use of code block in my mdx files ( ```) trigger the error detailed in the opening post.
/cc @johno you wrote the guide originally (and /cc @ChristopherBiscardi, as I believe you’ve written similar articles). Any idea what’s going on here?
Isn't classname undefined if there's no language on the code block?
I'm any case, the error in the issue isn't complete. The react error points to an error preceding the one pasted that isn't included.
Also this CodeBlock looks like it was written to replace the code element not pre
@ChristopherBiscardi if it can help, please see the full stack trace:
Uncaught TypeError: Cannot read property 'length' of undefined
at Object.matchGrammar (index.js:207)
at Object.tokenize (index.js:313)
at Highlight.render (index.js:306)
at finishClassComponent (react-dom.development.js:17160)
at updateClassComponent (react-dom.development.js:17110)
at beginWork (react-dom.development.js:18620)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at react-dom.development.js:11089
at unstable_runWithPriority (scheduler.development.js:653)
at runWithPriority$1 (react-dom.development.js:11039)
at flushSyncCallbackQueueImpl (react-dom.development.js:11084)
at workLoop (scheduler.development.js:597)
at flushWork (scheduler.development.js:552)
at MessagePort.performWorkUntilDeadline (scheduler.development.js:164)
index.js:1 The above error occurred in the <Highlight> component:
in Highlight (at CodeBlock.jsx:8)
in Unknown (created by MDXCreateElement)
in MDXCreateElement (at landing.md:76)
in pre (created by MDXCreateElement)
in MDXCreateElement (at landing.md:76)
in wrapper (created by MDXCreateElement)
in MDXCreateElement (at landing.md:21)
in MDXContent (at App.jsx:85)
in MDXProvider (at App.jsx:84)
in div (created by ForwardRef(Container))
in ForwardRef(Container) (created by WithStyles(ForwardRef(Container)))
in WithStyles(ForwardRef(Container)) (at App.jsx:83)
in Suspense (at App.jsx:82)
in main (created by ProxyComponent)
in ProxyComponent (created by StyledComponent)
in StyledComponent (created by Content)
in Content (at App.jsx:81)
in Layout (created by Root)
in LayoutProvider (created by Root)
in Root (at App.jsx:78)
in Router (created by BrowserRouter)
in BrowserRouter (at App.jsx:77)
in ThemeProvider (at App.jsx:75)
in Unknown (at src/index.js:8)
in StrictMode (at src/index.js:7)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
The above error occurred in the <Highlight> component:
in Highlight (at CodeBlock.jsx:8)
in Unknown (created by MDXCreateElement)
in MDXCreateElement (at landing.md:68)
in pre (created by MDXCreateElement)
in MDXCreateElement (at landing.md:68)
in wrapper (created by MDXCreateElement)
in MDXCreateElement (at landing.md:21)
in MDXContent (at App.jsx:85)
in MDXProvider (at App.jsx:84)
in div (created by ForwardRef(Container))
in ForwardRef(Container) (created by WithStyles(ForwardRef(Container)))
in WithStyles(ForwardRef(Container)) (at App.jsx:83)
in Suspense (at App.jsx:82)
in main (created by ProxyComponent)
in ProxyComponent (created by StyledComponent)
in StyledComponent (created by Content)
in Content (at App.jsx:81)
in Layout (created by Root)
in LayoutProvider (created by Root)
in Root (at App.jsx:78)
in Router (created by BrowserRouter)
in BrowserRouter (at App.jsx:77)
in ThemeProvider (at App.jsx:75)
in Unknown (at src/index.js:8)
in StrictMode (at src/index.js:7)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
index.js:207 Uncaught TypeError: Cannot read property 'length' of undefined
at Object.matchGrammar (index.js:207)
at Object.tokenize (index.js:313)
at Highlight.render (index.js:306)
at finishClassComponent (react-dom.development.js:17160)
at updateClassComponent (react-dom.development.js:17110)
at beginWork (react-dom.development.js:18620)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at react-dom.development.js:11089
at unstable_runWithPriority (scheduler.development.js:653)
at runWithPriority$1 (react-dom.development.js:11039)
at flushSyncCallbackQueueImpl (react-dom.development.js:11084)
at workLoop (scheduler.development.js:597)
at flushWork (scheduler.development.js:552)
at MessagePort.performWorkUntilDeadline (scheduler.development.js:164)
Any update on this issue?
It is unclear. You never provided the code in full, for example, or responded to Chris' suggestions
@wooorm what do you mean I replied to @ChristopherBiscardi by providing the full stack trace and all the relevant parts of the code are provided in the previous posts. Please let me know what more you need to investigate the issue and I'll make sure to provide it. 😇
Really want to under what's going on and why it is not working although I diligently followed the tutorial.
@aress31 if you could provide a codesandbox example that reproduces the issue that would be superb and I'd be happy to dig in and debug it for you
a git repo would also work if you don't want to use codesandbox
I'm also having an issue with this. @aress31 here's where the problem might be:
export default ({ children, className }) => {
If you debug you'll see children
isn't passed to the Code element. Still digging to figure out why that might be.
I'll also add that (somewhat unexpectedly) simply importing a css file from https://prismjs.com/ was enough to get syntax highlighting working in the context of MDX. Not sure why it wasn't necessary to run the corresponding JS as you'd need to do otherwise (e.g. https://betterstack.dev/blog/code-highlighting-in-react-using-prismjs/).
Thanks for your patience, y’all!
@rothbart I think that’s exactly what prism-react-renderer
does: it means you don‘t have to add the Prism JS. But, you still need to style it.
If this is the exact same issue as the original one, I’d appreciate it if any of you can figure out why children isn’t passed in your case. Because, uhm, I think we’re doing that everywhere 🤷♂️ And: did y’all update to use pre
instead of code
, as @ChristopherBiscardi pointed out?
I believe the original issue is addressed by the suggestions in this thread. If someone is still stumbling on this exact issue, please comment here. But I’m assuming it’ll be something else, so if that’s the case please open a new issue!
Subject of the issue
I followed the tutorial to turn on syntax highlighting on my application but I am getting the following error:
This is my
CodeBlock.jsx
component:And this is my
MDXProvider
configuration:I have bootstrapped my application using CRA.
Your environment
Steps to reproduce
Tell us how to reproduce this issue. Please provide a working and simplified example.
🎉 BONUS POINTS for creating a minimal reproduction and uploading it to GitHub. This will get you the fastest support. 🎉
Expected behaviour
Should be working. 😵