diff --git a/addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap b/addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap index 0986c6fd528..a8c09401768 100644 --- a/addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap +++ b/addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap @@ -95,7 +95,7 @@ MDXContent.isMDXComponent = true; export const one = () => ; one.story = {}; -one.story.parameters = { mdxSource: \`\` }; +one.story.parameters = { mdxSource: '' }; one.story.decorators = [storyFn =>
{storyFn()}
]; const componentMeta = { @@ -166,12 +166,12 @@ MDXContent.isMDXComponent = true; export const one = () => ; one.story = {}; -one.story.parameters = { mdxSource: \`\` }; +one.story.parameters = { mdxSource: '' }; export const helloStory = () => ; helloStory.story = {}; helloStory.story.name = 'hello story'; -helloStory.story.parameters = { mdxSource: \`\` }; +helloStory.story.parameters = { mdxSource: '' }; const componentMeta = { title: 'Button', includeStories: ['one', 'helloStory'] }; @@ -240,15 +240,8 @@ export const toStorybook = () => ({ toStorybook.story = {}; toStorybook.story.name = 'to storybook'; toStorybook.story.parameters = { - mdxSource: \`{ - template: \\\\\`\\\\\`, - props: { - showApp: linkTo('Button') - }, - moduleMetadata: { - declarations: [Welcome] - } -}\`, + mdxSource: + '{\\\\n template: \`\`,\\\\n props: {\\\\n showApp: linkTo(\\\\'Button\\\\')\\\\n },\\\\n moduleMetadata: {\\\\n declarations: [Welcome]\\\\n }\\\\n}', }; const componentMeta = { title: 'MDX|Welcome', includeStories: ['toStorybook'] }; @@ -315,13 +308,13 @@ MDXContent.isMDXComponent = true; export const componentNotes = () => ; componentNotes.story = {}; componentNotes.story.name = 'component notes'; -componentNotes.story.parameters = { mdxSource: \`\` }; +componentNotes.story.parameters = { mdxSource: '' }; export const storyNotes = () => ; storyNotes.story = {}; storyNotes.story.name = 'story notes'; storyNotes.story.parameters = { - mdxSource: \`\`, + mdxSource: '', ...{ notes: 'story notes', }, @@ -398,11 +391,11 @@ MDXContent.isMDXComponent = true; export const helloButton = () => ; helloButton.story = {}; helloButton.story.name = 'hello button'; -helloButton.story.parameters = { mdxSource: \`\` }; +helloButton.story.parameters = { mdxSource: '' }; export const two = () => ; two.story = {}; -two.story.parameters = { mdxSource: \`\` }; +two.story.parameters = { mdxSource: '' }; const componentMeta = { title: 'Button', @@ -462,12 +455,12 @@ MDXContent.isMDXComponent = true; export const one = () => ; one.story = {}; -one.story.parameters = { mdxSource: \`\` }; +one.story.parameters = { mdxSource: '' }; export const helloStory = () => ; helloStory.story = {}; helloStory.story.name = 'hello story'; -helloStory.story.parameters = { mdxSource: \`\` }; +helloStory.story.parameters = { mdxSource: '' }; const componentMeta = { title: 'Button', includeStories: ['one', 'helloStory'] }; @@ -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'] }; diff --git a/addons/docs/mdx-compiler-plugin.js b/addons/docs/mdx-compiler-plugin.js index f8f22c52059..8d8130da809 100644 --- a/addons/docs/mdx-compiler-plugin.js +++ b/addons/docs/mdx-compiler-plugin.js @@ -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 @@ -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'); diff --git a/addons/docs/package.json b/addons/docs/package.json index 9b1656eb6c7..615afb84776 100644 --- a/addons/docs/package.json +++ b/addons/docs/package.json @@ -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" }, diff --git a/examples/official-storybook/stories/demo/button.stories.mdx b/examples/official-storybook/stories/demo/button.stories.mdx new file mode 100644 index 00000000000..fbdbfebf81b --- /dev/null +++ b/examples/official-storybook/stories/demo/button.stories.mdx @@ -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'; + + + +# Simple Button + +## Hello + + + + + +## Emoji + + + + + +## Counter w/ Code + + + + {React.createElement(() => { + const [counter, setCounter] = useState(0); + const label = `Testing: ${counter}`; + return ; + })} + + diff --git a/yarn.lock b/yarn.lock index 3abe643676f..1b5b38b8ee2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"