Skip to content

Commit

Permalink
docs: Fix Story names and extra imports (#966)
Browse files Browse the repository at this point in the history
Before a story named 'NamedKeys' would show up to Storybook as 'NamedKeys'. This change will present the story as 'Named Keys' like it should.
  • Loading branch information
NicholasBoll committed Feb 10, 2021
1 parent 8dcc951 commit 159da10
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .storybook/mdx-code-block-rewrite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const {storyNameFromExport} = require('@storybook/csf');

// This loader replaces example code blocks with Storybook specific tags
// before:
Expand All @@ -7,6 +8,7 @@ const path = require('path');
// <Canvas><Story name="MyComponent" story={MyComponent} parameters={{storySource: { source: MyComponent.__RAW__ }}}
// __RAW__ comes from the `whole-source-loader
module.exports = function rewriteExampleCodeBlock(source) {
const hasSpecialBlocks = /<(Meta|ExampleCodeBlock|PropsTable)/.test(source);
const hasMeta = /import {.*Meta[,\s}]/.test(source);
const hasCanvas = /import {.*Canvas[,\s}]/.test(source);
const hasStory = /import {.*Story[,\s}]/.test(source);
Expand All @@ -17,10 +19,14 @@ module.exports = function rewriteExampleCodeBlock(source) {
if (!hasStory) imports.push('Story');
if (!hasArgsTable) imports.push('ArgsTable');
return (
(imports.length ? `import {${imports.join(',')}} from '@storybook/addon-docs/blocks';\n` : '') +
(imports.length && hasSpecialBlocks
? `import {${imports.join(',')}} from '@storybook/addon-docs/blocks';\n\n`
: '') +
source
.replace(/\<ExampleCodeBlock code={([A-Za-z0-9]+)} \/\>/g, function replacer(match, p1, p2) {
return `<Canvas><Story name="${p1}" parameters={{storySource: {source: ${p1}.__RAW__}}}><${p1} /></Story></Canvas>`;
return `<Canvas><Story name="${storyNameFromExport(
p1
)}" parameters={{storySource: {source: ${p1}.__RAW__}}}><${p1} /></Story></Canvas>`;
})
.replace(/\<PropsTable of=/g, '<ArgsTable of=')
);
Expand Down

0 comments on commit 159da10

Please sign in to comment.