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

Supports for tags in <Meta/> #134

Merged
merged 3 commits into from
Sep 16, 2023
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
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ It supports:
## Example

```svelte
<script>
import { Meta, Story, Template } from '@storybook/addon-svelte-csf';

<script content="module">
import Button from './Button.svelte';

export const meta = {
title: "Button",
component: Button
}
</script>

<script>
import { Story, Template } from '@storybook/addon-svelte-csf';

let count = 0;
function handleClick() {
count += 1;
}
</script>

<Meta title="Button" component={Button}/>

<Template let:args>
<Button {...args} on:click={handleClick}>
You clicked: {count}
Expand Down
19 changes: 18 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ interface TemplateProps extends BaseAnnotations<any, DecoratorReturnType> {
id?: string;
}

interface MetaProps extends BaseMeta<any>, BaseAnnotations<any, DecoratorReturnType> { }
interface MetaProps extends BaseMeta<any>, BaseAnnotations<any, DecoratorReturnType> {
/**
* Enable the tag 'autodocs'.
*
* @see [Automatic documentation](https://storybook.js.org/docs/svelte/writing-docs/autodocs)
*/
autodocs?: boolean;
/**
* List of tags to add to the stories.
*
* It should be a static array of strings.
*
* @example tags={['autodocs']}
*/
tags?: string[];
}

interface Slots {
default: {
Expand All @@ -55,6 +70,8 @@ interface Slots {
}
/**
* Meta.
*
* @deprecated Use `export const meta`. See https://github.com/storybookjs/addon-svelte-csf for an example
*/
export class Meta extends SvelteComponent<MetaProps> { }
/**
Expand Down
4 changes: 2 additions & 2 deletions src/parser/collect-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const createFragment = document.createDocumentFragment
? () => document.createDocumentFragment()
: () => document.createElement('div');

export default (StoriesComponent, { stories = {}, allocatedIds = [] }) => {
export default (StoriesComponent, { stories = {}, allocatedIds = [] }, exportedMeta = undefined) => {
const repositories = {
meta: null as Meta | null,
stories: [] as Story[],
Expand All @@ -55,7 +55,7 @@ export default (StoriesComponent, { stories = {}, allocatedIds = [] }) => {
logger.error(`Error extracting stories ${e.toString()}`, e);
}

const { meta } = repositories;
const meta = exportedMeta || repositories.meta;
if (!meta) {
logger.error('Missing <Meta/> tag');
return {};
Expand Down
144 changes: 144 additions & 0 deletions src/parser/extract-stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,150 @@ describe('extractSource', () => {
}
`);
});
test('Add tags autodocs', () => {
expect(
extractStories(`
<script>
import { Story, Meta } from '@storybook/addon-svelte-csf';
</script>

<Meta title='test' autodocs/>

<Story name="Story1">
<div>story 1</div>
</Story>
`)
).toMatchInlineSnapshot(`
{
"allocatedIds": [
"default",
"Story",
"Meta",
],
"meta": {
"id": undefined,
"tags": [
"autodocs",
],
"title": "test",
},
"stories": {
"Story1": {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
}
`);
});
test('Add tags', () => {
expect(
extractStories(`
<script>
import { Story, Meta } from '@storybook/addon-svelte-csf';
</script>

<Meta title='test' tags={['a','b']}/>

<Story name="Story1">
<div>story 1</div>
</Story>
`)
).toMatchInlineSnapshot(`
{
"allocatedIds": [
"default",
"Story",
"Meta",
],
"meta": {
"id": undefined,
"tags": [
"a",
"b",
],
"title": "test",
},
"stories": {
"Story1": {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
}
`);
});
test('Add Only one tag', () => {
expect(
extractStories(`
<script>
import { Story, Meta } from '@storybook/addon-svelte-csf';
</script>

<Meta title='test' tags='a'/>

<Story name="Story1">
<div>story 1</div>
</Story>
`)
).toMatchInlineSnapshot(`
{
"allocatedIds": [
"default",
"Story",
"Meta",
],
"meta": {
"id": undefined,
"tags": [
"a",
],
"title": "test",
},
"stories": {
"Story1": {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
}
`);
});
test('Meta as exported module object', () => {
expect(
extractStories(`
<script context='module'>
export const meta = {
title: 'MyStory',
tags: ['a']
};
</script>
`)
).toMatchInlineSnapshot(`
{
"allocatedIds": [
"default",
],
"meta": {
"id": undefined,
"tags": [
"a",
],
"title": "MyStory",
},
"stories": {},
}
`);
});
test('Duplicate Id', () => {
expect(
extractStories(`
Expand Down
Loading