When I try to add certain snippets of code which work well in regular markdown I get "undefined" errors
See original GitHub issueThe latest is specifically this snippet of code:
secondHand.style.mozTransform = `rotate(${secondsDegrees}deg)`;
secondHand.style.webkitTransform = `rotate(${secondsDegrees}deg)`;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
I am being told in console that secondsDegrees is not defined:
Uncaught ReferenceError: secondsDegrees is not defined
This is supposed to be a snippet of code in markdown, so it should not matter whether or not something has been “defined”. I have been using Chrome, and I also use Atom and eslint. This kind of thing has happened several times already. How can I avoid it? Or can I? Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
"Text is Undefined" error using Markdown? - Stack Overflow
I downloaded markdown from http://code.google.com/p/wmd/ (well, in fact I cloned the hg repository), in order to have a client-side highlighter ...
Read more >eslint/eslint-plugin-markdown: Lint JavaScript code ... - GitHub
Lint JavaScript code blocks in Markdown documents. ... Some rules that catch mistakes in regular code are less helpful in documentation.
Read more >15 Common Problems with rmarkdown (and some solutions)
15.1 Avoiding problems. To avoid problems in the first place, I try and do the following: Develop code in chunks and execute the...
Read more >Code Snippets shows fatal error when trying to insert first ...
HI guys, I just installed code snippets to adjust woocommerce to a “from” price ... Fatal error: Call to undefined function wp_add_inline_script() in ......
Read more >A Comprehensive Guide To Error Handling In Node.js
Errors happen in every application. Devs have to decide: do you write code to handle the error? Suppress it? Notify the user?
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This isn’t a problem with react-markdown, but rather with how your code defines the markdown.
Let’s image you want the following markdown to be rendered:
If you put this into a Javascript file like this:
It will give an error like the one you are encountering:
This is because the backticks in this markdown will be interpreted as closing/starting ES6 template literals.
So in order to make this work, you need to escape the backticks by prefixing them with a
\
character. Having done that, you will also have to escape variable interpolation in the same way, escaping the$
character. Altogether:Note that this is only necessary because of the way the markdown is loaded/defined. If you were to define the same markdown with plain old ES5 string semantics, it would work just fine:
Or if you were fetching the markdown from a remote source, it would also just work:
The main takeaway from this is that the error you are seeing is a result of the input in and of itself and is not really related to react-markdown. If you take the same code that defines the markdown source and remove any reference to react-markdown, it’ll give you the same error.
Hope this was helpful 😃
@rexxars I decided it was worth the couple of extra seconds to escape the template literals so I could stay true to the code I used in the project(s). It works very nicely! Thanks so much.