question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When I try to add certain snippets of code which work well in regular markdown I get "undefined" errors

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
rexxarscommented, Oct 18, 2017

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:

## Template literals

```js
const color = "blue"
const str = `The car is ${color}`
console.log(str)
```

Variable interpolation is **neat**, right?

If you put this into a Javascript file like this:

const markdown = `
## Template literals

```js
const color = "blue"
const str = `The car is ${color}`
console.log(str)
```

Variable interpolation is **neat**, right?`

It will give an error like the one you are encountering:

ReferenceError: color is not defined

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:

const markdown = `
## Template literals

\`\`\`js
const color = "blue"
const str = \`The car is \${color}\`
console.log(str)
\`\`\`

Variable interpolation is **neat**, right?`

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:

const markdown = [
  '## Template literals',
  '',
  '```js',
  'const color = "blue"',
  'const str = `The car is ${color}`',
  'console.log(str)',
  '```',
  '',
  'Variable interpolation is **neat**, right?`',
].join('\n')

console.log(markdown)

Or if you were fetching the markdown from a remote source, it would also just work:

fetch('some-markdown-file.md')
  .then(res => res.text())
  .then(markdown => console.log(ReactDOM.renderToString(<Markdown source={markdown} />)))

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 😃

0reactions
interglobalmediacommented, Oct 18, 2017

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found