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

Csf-tools: Fix local vars handling in MDX-generated CSF #20255

Merged
merged 2 commits into from
Dec 14, 2022
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
30 changes: 30 additions & 0 deletions code/lib/csf-tools/src/CsfFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,36 @@ describe('CsfFile', () => {
`);
});

it('docs-only story with local vars', () => {
expect(
parse(
dedent`
export const TestControl = () => _jsx("p", {
children: "Hello"
});
export default { title: 'foo/bar', tags: ['mdx'], includeStories: ["__page"] };
export const __page = () => {};
__page.parameters = { docsOnly: true };
`,
true
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
tags:
- mdx
includeStories:
- __page
stories:
- id: foo-bar--page
name: Page
parameters:
__isArgsStory: false
__id: foo-bar--page
docsOnly: true
`);
});

it('title variable', () => {
expect(
parse(
Expand Down
6 changes: 5 additions & 1 deletion code/lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ export class CsfFile {
if (isExportStory(key, self._meta)) {
const id = toId(self._meta.id || self._meta.title, storyNameFromExport(key));
const parameters: Record<string, any> = { ...story.parameters, __id: id };
if (entries.length === 1 && key === '__page') {
const { includeStories } = self._meta || {};
if (
key === '__page' &&
(entries.length === 1 || (Array.isArray(includeStories) && includeStories.length === 1))
) {
parameters.docsOnly = true;
}
acc[key] = { ...story, id, parameters };
Expand Down