Skip to content

Commit

Permalink
Add support for autotitle
Browse files Browse the repository at this point in the history
  • Loading branch information
j3rem1e committed Sep 1, 2023
1 parent c48e985 commit 4d942cf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 22 deletions.
9 changes: 6 additions & 3 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import type { StorybookConfig } from '@storybook/svelte-vite';

const config: StorybookConfig = {
framework: '@storybook/svelte-vite',
stories: [
'../stories/**/*.stories.svelte',
],
stories: [{
directory: '../stories',
files:'**/*.stories.svelte',
titlePrefix:'Demo'
}],
addons: [
'../dist/preset/index.js',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
],

};
export default config;
12 changes: 0 additions & 12 deletions src/parser/extract-stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "MyStory",
"source": "<div>a story</div>",
"storyId": "mystory--my-story",
"template": false,
},
},
Expand Down Expand Up @@ -56,7 +55,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "MyStory",
"source": "<div>a story</div>",
"storyId": "myid--my-id",
"template": false,
},
},
Expand Down Expand Up @@ -86,7 +84,6 @@ describe('extractSource', () => {
"hasArgs": true,
"name": "MyStory",
"source": "<div>a story</div>",
"storyId": "mystory--my-story",
"template": false,
},
},
Expand Down Expand Up @@ -116,7 +113,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "MyTemplate",
"source": "<div>a template</div>",
"storyId": "mytemplate--my-template",
"template": true,
},
},
Expand Down Expand Up @@ -146,7 +142,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "default",
"source": "<div>a template</div>",
"storyId": "default--default",
"template": true,
},
},
Expand Down Expand Up @@ -179,14 +174,12 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "story1--story-1",
"template": false,
},
"Story2": {
"hasArgs": false,
"name": "Story2",
"source": "<div>story 2</div>",
"storyId": "story2--story-2",
"template": false,
},
},
Expand Down Expand Up @@ -222,7 +215,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
Expand Down Expand Up @@ -261,7 +253,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
Expand Down Expand Up @@ -301,7 +292,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
Expand Down Expand Up @@ -340,7 +330,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Story1",
"source": "<div>story 1</div>",
"storyId": "test--story-1",
"template": false,
},
},
Expand Down Expand Up @@ -398,7 +387,6 @@ describe('extractSource', () => {
"hasArgs": false,
"name": "Button",
"source": "<div>a story</div>",
"storyId": "button77471352--button-77471352",
"template": false,
},
},
Expand Down
4 changes: 0 additions & 4 deletions src/parser/extract-stories.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as svelte from 'svelte/compiler';
import type { Node } from 'estree';

import { storyNameFromExport, toId } from '@storybook/csf';

import dedent from 'dedent';
import { extractId } from './extract-id.js';

interface StoryDef {
storyId: string;
name: string;
template: boolean;
source: string;
Expand Down Expand Up @@ -228,7 +225,6 @@ export function extractStories(component: string): StoriesDef {
source = dedent`${component.substr(start, end - start)}`;
}
stories[isTemplate ? `tpl:${id}` : id] = {
storyId: toId(meta.id || meta.title || id, storyNameFromExport(id)),
name,
template: isTemplate,
source,
Expand Down
6 changes: 4 additions & 2 deletions src/preset/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as svelte from 'svelte/compiler';
import { extractStories } from '../parser/extract-stories.js';
import fs from 'fs-extra';
import { loadSvelteConfig } from '../config-loader.js';
import { storyNameFromExport, toId } from '@storybook/csf';

export async function svelteIndexer(fileName, { makeTitle }) {
let code = (await fs.readFile(fileName, 'utf-8')).toString();
Expand All @@ -14,12 +15,13 @@ export async function svelteIndexer(fileName, { makeTitle }) {

const defs = extractStories(code);

const meta = { ...defs.meta, title: makeTitle(defs.meta.title) }
return {
meta: { title: makeTitle(defs.meta.title), tags: defs.meta.tags },
meta,
stories: Object.entries(defs.stories)
.filter(([id, story]) => !story.template)
.map(([id, story]) => ({
id: story.storyId,
id: toId(meta.id || meta.title || id, storyNameFromExport(id)),
name: story.name,
})),
};
Expand Down
2 changes: 1 addition & 1 deletion stories/button.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
</script>

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

<Template let:args>
<Button {...args} on:click={handleClick}>
Expand Down

0 comments on commit 4d942cf

Please sign in to comment.