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

Remove uses of AddContext #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
75 changes: 4 additions & 71 deletions src/sb-mdx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ describe('docs-mdx-compiler-plugin', () => {
},
includeStories: ['componentNotes'],
};

const mdxStoryNameToKey = { 'component notes': 'componentNotes' };
`);
});

Expand All @@ -98,8 +96,6 @@ describe('docs-mdx-compiler-plugin', () => {
component: Button,
includeStories: ['componentNotes'],
};

const mdxStoryNameToKey = { 'component notes': 'componentNotes' };
`);
});

Expand Down Expand Up @@ -132,8 +128,6 @@ describe('docs-mdx-compiler-plugin', () => {
_Foo_.storyName = 'renamed';

const componentMeta = { title: 'MDX/CSF imports', includeStories: ['_Basic_', '_Other_', '_Foo_'] };

const mdxStoryNameToKey = { _Basic_: '_Basic_', _Other_: '_Other_', renamed: '_Foo_' };
`);
});

Expand Down Expand Up @@ -175,8 +169,6 @@ describe('docs-mdx-compiler-plugin', () => {
],
includeStories: ['one'],
};

const mdxStoryNameToKey = { one: 'one' };
`);
});

Expand All @@ -199,8 +191,6 @@ describe('docs-mdx-compiler-plugin', () => {
__page.parameters = { docsOnly: true };

const componentMeta = { title: 'docs-only', includeStories: ['__page'] };

const mdxStoryNameToKey = {};
`);
});

Expand Down Expand Up @@ -237,8 +227,6 @@ describe('docs-mdx-compiler-plugin', () => {
],
includeStories: ['one'],
};

const mdxStoryNameToKey = { one: 'one' };
`);
});

Expand All @@ -257,8 +245,6 @@ describe('docs-mdx-compiler-plugin', () => {
__page.parameters = { docsOnly: true };

const componentMeta = { title: "Addons/Docs/what's in a title?", includeStories: ['__page'] };

const mdxStoryNameToKey = {};
`);
});

Expand Down Expand Up @@ -292,8 +278,6 @@ describe('docs-mdx-compiler-plugin', () => {
helloStory.parameters = { storySource: { source: '<Button>Hello button</Button>' } };

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

const mdxStoryNameToKey = { one: 'one', 'hello story': 'helloStory' };
`);
});

Expand Down Expand Up @@ -335,8 +319,6 @@ describe('docs-mdx-compiler-plugin', () => {
component: Button,
includeStories: ['componentNotes', 'storyNotes'],
};

const mdxStoryNameToKey = { 'component notes': 'componentNotes', 'story notes': 'storyNotes' };
`);
});

Expand Down Expand Up @@ -386,8 +368,6 @@ describe('docs-mdx-compiler-plugin', () => {
component: Button,
includeStories: ['helloButton', 'two'],
};

const mdxStoryNameToKey = { 'hello button': 'helloButton', two: 'two' };
`);
});

Expand Down Expand Up @@ -429,8 +409,6 @@ describe('docs-mdx-compiler-plugin', () => {
componentNotes.parameters = { storySource: { source: 'args => <Button>Component notes</Button>' } };

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

const mdxStoryNameToKey = { 'component notes': 'componentNotes' };
`);
});

Expand All @@ -443,11 +421,7 @@ describe('docs-mdx-compiler-plugin', () => {

<Story id="." />
`)
).toMatchInlineSnapshot(`
const componentMeta = { includeStories: [] };

const mdxStoryNameToKey = {};
`);
).toMatchInlineSnapshot(`const componentMeta = { includeStories: [] };`);
});

it('story-def-text-only.mdx', () => {
Expand All @@ -467,8 +441,6 @@ describe('docs-mdx-compiler-plugin', () => {
text.parameters = { storySource: { source: "'Plain text'" } };

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

const mdxStoryNameToKey = { text: 'text' };
`);
});

Expand Down Expand Up @@ -519,13 +491,6 @@ describe('docs-mdx-compiler-plugin', () => {
title: 'Button',
includeStories: ['one', 'helloStory', 'wPunctuation', '_1FineDay'],
};

const mdxStoryNameToKey = {
one: 'one',
'hello story': 'helloStory',
'w/punctuation': 'wPunctuation',
'1 fine day': '_1FineDay',
};
`);
});

Expand All @@ -550,8 +515,6 @@ describe('docs-mdx-compiler-plugin', () => {
basic.parameters = { storySource: { source: 'basicFn' } };

const componentMeta = { title: 'story-function-var', includeStories: ['basic'] };

const mdxStoryNameToKey = { basic: 'basic' };
`);
});

Expand Down Expand Up @@ -583,8 +546,6 @@ describe('docs-mdx-compiler-plugin', () => {
};

const componentMeta = { includeStories: ['functionStory'] };

const mdxStoryNameToKey = { function: 'functionStory' };
`);
});

Expand Down Expand Up @@ -615,8 +576,6 @@ describe('docs-mdx-compiler-plugin', () => {
};

const componentMeta = { title: 'Multiple', includeStories: ['multipleChildren'] };

const mdxStoryNameToKey = { 'multiple children': 'multipleChildren' };
`);
});

Expand Down Expand Up @@ -662,8 +621,6 @@ describe('docs-mdx-compiler-plugin', () => {
};

const componentMeta = { title: 'MDX|Welcome', includeStories: ['toStorybook'] };

const mdxStoryNameToKey = { 'to storybook': 'toStorybook' };
`);
});

Expand All @@ -676,11 +633,7 @@ describe('docs-mdx-compiler-plugin', () => {

<Story id="welcome--welcome" />
`)
).toMatchInlineSnapshot(`
const componentMeta = { includeStories: [] };

const mdxStoryNameToKey = {};
`);
).toMatchInlineSnapshot(`const componentMeta = { includeStories: [] };`);
});

it('title-template-string.mdx', () => {
Expand All @@ -702,8 +655,6 @@ describe('docs-mdx-compiler-plugin', () => {
__page.parameters = { docsOnly: true };

const componentMeta = { title: \`\${titleFunction('template')}\`, includeStories: ['__page'] };

const mdxStoryNameToKey = {};
`);
});

Expand All @@ -718,11 +669,7 @@ describe('docs-mdx-compiler-plugin', () => {

<Button>Hello button</Button>
`)
).toMatchInlineSnapshot(`
const componentMeta = { includeStories: [] };

const mdxStoryNameToKey = {};
`);
).toMatchInlineSnapshot(`const componentMeta = { includeStories: [] };`);
});

it('errors on missing story props', async () => {
Expand Down Expand Up @@ -754,11 +701,7 @@ describe('docs-mdx-compiler-plugin', () => {

Spme **markdown** here!
`)
).toMatchInlineSnapshot(`
const componentMeta = { includeStories: [] };

const mdxStoryNameToKey = {};
`);
).toMatchInlineSnapshot(`const componentMeta = { includeStories: [] };`);
});

it('auto-title.mdx', () => {
Expand All @@ -779,8 +722,6 @@ describe('docs-mdx-compiler-plugin', () => {
basic.parameters = { storySource: { source: '<Button>Basic</Button>' } };

const componentMeta = { component: Button, includeStories: ['basic'] };

const mdxStoryNameToKey = { Basic: 'basic' };
`);
});

Expand All @@ -800,8 +741,6 @@ describe('docs-mdx-compiler-plugin', () => {
basic.parameters = { storySource: { source: '{}' } };

const componentMeta = { title: 'Button', component: Button, includeStories: ['basic'] };

const mdxStoryNameToKey = { Basic: 'basic' };
`);
});

Expand All @@ -826,8 +765,6 @@ describe('docs-mdx-compiler-plugin', () => {
render: (args) => <Button {...args} />,
includeStories: ['basic'],
};

const mdxStoryNameToKey = { Basic: 'basic' };
`);
});

Expand All @@ -848,8 +785,6 @@ describe('docs-mdx-compiler-plugin', () => {
basic.render = (args) => <Button {...args} />;

const componentMeta = { title: 'Button', component: Button, includeStories: ['basic'] };

const mdxStoryNameToKey = { Basic: 'basic' };
`);
});

Expand All @@ -870,8 +805,6 @@ describe('docs-mdx-compiler-plugin', () => {
basic.play = () => console.log('play');

const componentMeta = { title: 'Button', component: Button, includeStories: ['basic'] };

const mdxStoryNameToKey = { Basic: 'basic' };
`);
});
});
Expand Down
7 changes: 2 additions & 5 deletions src/sb-mdx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,11 @@ function getExports(node: Element, context: Context, options: CompilerOptions) {
return null;
}

// insert `mdxStoryNameToKey` and `mdxComponentMeta` into the context so that we
// can reconstruct the Story ID dynamically from the `name` at render time
export const wrapperJs = `
componentMeta.parameters = componentMeta.parameters || {};
componentMeta.parameters.docs = {
...(componentMeta.parameters.docs || {}),
page: () => <AddContext mdxStoryNameToKey={mdxStoryNameToKey} mdxComponentAnnotations={componentMeta}><MDXContent /></AddContext>,
page: () => <MDXContent />,
};
`.trim();

Expand Down Expand Up @@ -505,11 +503,10 @@ function extractExports(root: Element, options: CompilerOptions) {

const defaultJsx = toJSX(root, {}, { ...options, skipExport: true });
const fullJsx = [
'import { assertIsFn, AddContext } from "@storybook/addon-docs";',
'import { assertIsFn } from "@storybook/addon-docs";',
defaultJsx,
...storyExports,
`const componentMeta = ${stringifyMeta(metaExport)};`,
`const mdxStoryNameToKey = ${JSON.stringify(context.storyNameToKey)};`,
wrapperJs,
'export default componentMeta;',
].join('\n\n');
Expand Down