storybook: addon-docs: ${...} in code block is executed

Describe the bug In a code block in MDX, inserting ${var} is executed(-ish) and replaced by something that’s not useful. There doesn’t appear to be a way to escape it, and it shouldn’t be neccesary anyway. This worked fine in Storybook 5.2.x but this bug may have creeped in at a later time.

This example MDX snippet:

```jsx
const CustomLabel = styled(Label)`
  ${display}
`;
```

Will be displayed as:

const CustomLabel = styled(Label)`
  ___CSS_0___
`;

I’ve tried to escape it with \${display}, $\{display}, ${\display}. I’ve tried escaping the backticks with `` and \`. None of these escapings fix the problem. Either the escapes are displayed literally (and the bogus ___CSS_0___ remains), or I get an error that says display is not defined. Strangely, even without this error, it still manages to replace ${display} with something.

To Reproduce Steps to reproduce the behavior:

  1. Have an MDX with the above code
  2. Run it in your Storybook instance

Expected behavior It shouldn’t try to replace anything. It’s not code, it’s documentation.

System: Results of npx -p @storybook/cli@next sb info:

  System:
    OS: Windows 10 10.0.18363
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 10.16.3 - C:\Program Files\nodejs\node.EXE
    npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: 44.18362.449.0

Too bad this command doesn’t output my Storybook version, which is 5.3.14, and for addon-docs as well.

Additional context One workaround is to replace ```jsx with ```, which changes the syntax highlighting. It also stops Storybook from replacing ${display} with a bogus string. Really, the tag after ``` should only specify syntax highlighting and shouldn’t apply any arbitrary parsing or additional code analysis. It should be treated as plain text, and never anything more.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 19 (11 by maintainers)

Most upvoted comments

Thanks @thany! I’ll try my best to track it down when I get a few mins. If anybody else wants to take it, have at it!

@thany plan on replacing it with react-refractor, but whatever works and is maintained.

A hacky workaround (PLEASE DO NOT DO THIS IF YOU DO NOT HAVE TO)

You can paste a zero-width space after dollar sign and it works but please note that this may revenge in the future when you will play with version system

@shilman Just a quick followup, do you (or anyone else) know if this is solved in Storybook 6.0 by any chance?

This would give me and my team another handle to justify spending time to do the upgrade 😎

I’m using version 6 and I confirm issue is there.