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

Addon-docs: Fix MDX source string escaping #7497

Merged
merged 2 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ MDXContent.isMDXComponent = true;

export const one = () => <Button>One</Button>;
one.story = {};
one.story.parameters = { mdxSource: \`<Button>One</Button>\` };
one.story.parameters = { mdxSource: '<Button>One</Button>' };
one.story.decorators = [storyFn => <div className=\\"local\\">{storyFn()}</div>];

const componentMeta = {
Expand Down Expand Up @@ -166,12 +166,12 @@ MDXContent.isMDXComponent = true;

export const one = () => <Button>One</Button>;
one.story = {};
one.story.parameters = { mdxSource: \`<Button>One</Button>\` };
one.story.parameters = { mdxSource: '<Button>One</Button>' };

export const helloStory = () => <Button>Hello button</Button>;
helloStory.story = {};
helloStory.story.name = 'hello story';
helloStory.story.parameters = { mdxSource: \`<Button>Hello button</Button>\` };
helloStory.story.parameters = { mdxSource: '<Button>Hello button</Button>' };

const componentMeta = { title: 'Button', includeStories: ['one', 'helloStory'] };

Expand Down Expand Up @@ -240,15 +240,8 @@ export const toStorybook = () => ({
toStorybook.story = {};
toStorybook.story.name = 'to storybook';
toStorybook.story.parameters = {
mdxSource: \`{
template: \\\\\`<storybook-welcome-component (showApp)=\\"showApp()\\"></storybook-welcome-component>\\\\\`,
props: {
showApp: linkTo('Button')
},
moduleMetadata: {
declarations: [Welcome]
}
}\`,
mdxSource:
'{\\\\n template: \`<storybook-welcome-component (showApp)=\\"showApp()\\"></storybook-welcome-component>\`,\\\\n props: {\\\\n showApp: linkTo(\\\\'Button\\\\')\\\\n },\\\\n moduleMetadata: {\\\\n declarations: [Welcome]\\\\n }\\\\n}',
};

const componentMeta = { title: 'MDX|Welcome', includeStories: ['toStorybook'] };
Expand Down Expand Up @@ -315,13 +308,13 @@ MDXContent.isMDXComponent = true;
export const componentNotes = () => <Button>Component notes</Button>;
componentNotes.story = {};
componentNotes.story.name = 'component notes';
componentNotes.story.parameters = { mdxSource: \`<Button>Component notes</Button>\` };
componentNotes.story.parameters = { mdxSource: '<Button>Component notes</Button>' };

export const storyNotes = () => <Button>Story notes</Button>;
storyNotes.story = {};
storyNotes.story.name = 'story notes';
storyNotes.story.parameters = {
mdxSource: \`<Button>Story notes</Button>\`,
mdxSource: '<Button>Story notes</Button>',
...{
notes: 'story notes',
},
Expand Down Expand Up @@ -398,11 +391,11 @@ MDXContent.isMDXComponent = true;
export const helloButton = () => <Button>Hello button</Button>;
helloButton.story = {};
helloButton.story.name = 'hello button';
helloButton.story.parameters = { mdxSource: \`<Button>Hello button</Button>\` };
helloButton.story.parameters = { mdxSource: '<Button>Hello button</Button>' };

export const two = () => <Button>Two</Button>;
two.story = {};
two.story.parameters = { mdxSource: \`<Button>Two</Button>\` };
two.story.parameters = { mdxSource: '<Button>Two</Button>' };

const componentMeta = {
title: 'Button',
Expand Down Expand Up @@ -462,12 +455,12 @@ MDXContent.isMDXComponent = true;

export const one = () => <Button>One</Button>;
one.story = {};
one.story.parameters = { mdxSource: \`<Button>One</Button>\` };
one.story.parameters = { mdxSource: '<Button>One</Button>' };

export const helloStory = () => <Button>Hello button</Button>;
helloStory.story = {};
helloStory.story.name = 'hello story';
helloStory.story.parameters = { mdxSource: \`<Button>Hello button</Button>\` };
helloStory.story.parameters = { mdxSource: '<Button>Hello button</Button>' };

const componentMeta = { title: 'Button', includeStories: ['one', 'helloStory'] };

Expand Down Expand Up @@ -558,7 +551,7 @@ MDXContent.isMDXComponent = true;

export const text = () => 'Plain text';
text.story = {};
text.story.parameters = { mdxSource: \`'Plain text'\` };
text.story.parameters = { mdxSource: \\"'Plain text'\\" };

const componentMeta = { title: 'Text', includeStories: ['text'] };

Expand Down
7 changes: 4 additions & 3 deletions addons/docs/mdx-compiler-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mdxToJsx = require('@mdx-js/mdx/mdx-hast-to-jsx');
const parser = require('@babel/parser');
const generate = require('@babel/generator').default;
const camelCase = require('lodash/camelCase');
const jsStringEscape = require('js-string-escape');

// Generate the MDX as is, but append named exports for every
// story in the contents
Expand Down Expand Up @@ -73,13 +74,13 @@ function genStoryExport(ast, counter) {

let parameters = getAttr(ast.openingElement, 'parameters');
parameters = parameters && parameters.expression;
const source = `\`${storyCode.replace(/`/g, '\\`')}\``;
const source = jsStringEscape(storyCode);
if (parameters) {
const { code: params } = generate(parameters, {});
// FIXME: hack in the story's source as a parameter
statements.push(`${storyKey}.story.parameters = { mdxSource: ${source}, ...${params} };`);
statements.push(`${storyKey}.story.parameters = { mdxSource: '${source}', ...${params} };`);
} else {
statements.push(`${storyKey}.story.parameters = { mdxSource: ${source} };`);
statements.push(`${storyKey}.story.parameters = { mdxSource: '${source}' };`);
}

let decorators = getAttr(ast.openingElement, 'decorators');
Expand Down
1 change: 1 addition & 0 deletions addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@storybook/theming": "5.2.0-beta.3",
"core-js": "^3.0.1",
"global": "^4.3.2",
"js-string-escape": "^1.0.1",
"lodash": "^4.17.11",
"prop-types": "^15.7.2"
},
Expand Down
41 changes: 41 additions & 0 deletions examples/official-storybook/stories/demo/button.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState } from 'react';
import { action } from '@storybook/addon-actions';
import { Button } from '@storybook/react/demo';
import { Meta, Story, Preview } from '@storybook/addon-docs/blocks';

<Meta
title="Other|Demo/ButtonMdx"
parameters={{
component: Button,
}}
/>

# Simple Button

## Hello

<Story name="with text">
<Button onClick={action('clicked')}>Hello Button</Button>
</Story>

## Emoji

<Story name="with some emoji">
<Button onClick={action('clicked')}>
<span role="img" aria-label="so cool">
😀 😎 👍 💯
</span>
</Button>
</Story>

## Counter w/ Code

<Preview>
<Story name="with coumter">
{React.createElement(() => {
const [counter, setCounter] = useState(0);
const label = `Testing: ${counter}`;
return <Button onClick={() => setCounter(counter + 1)}>{label}</Button>;
})}
</Story>
</Preview>
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17332,6 +17332,11 @@ js-levenshtein@^1.1.3:
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
integrity sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==

js-string-escape@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/js-string-escape/-/js-string-escape-1.0.1.tgz#e2625badbc0d67c7533e9edc1068c587ae4137ef"
integrity sha1-4mJbrbwNZ8dTPp7cEGjFh65BN+8=

js-stringify@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/js-stringify/-/js-stringify-1.0.2.tgz#1736fddfd9724f28a3682adc6230ae7e4e9679db"
Expand Down