The Example Generator automatically generates framework-specific example code from Storybook stories. It uses an interface-based architecture that makes it easy to add support for new frameworks.
example-generator/
├── src/
│ ├── index.ts # Main entry point
│ ├── interfaces.ts # Core interfaces
│ ├── types.ts # Type definitions
│ ├── parser.ts # Story parsing logic
│ ├── generators/ # Framework-specific generators
│ │ ├── html-generator.ts
│ │ ├── react-generator.ts
│ │ ├── angular-generator.ts
│ │ └── vue-generator.ts
│ ├── formatters/ # Code formatters
│ │ ├── html-formatter.ts
│ │ ├── react-formatter.ts
│ │ ├── angular-formatter.ts
│ │ └── vue-formatter.ts
│ └── utils/ # Shared utilities
│ ├── string-utils.ts
│ └── file-updater.ts
└── package.json
Each framework has a generator that implements the IExampleGenerator interface:
- HTMLExampleGenerator - Generates vanilla HTML/Web Component examples
- ReactExampleGenerator - Generates React/TSX examples
- AngularExampleGenerator - Generates Angular examples
- VueExampleGenerator - Generates Vue examples
Formatters handle converting component data to framework-specific code:
- Convert props to correct syntax (e.g.,
checked→[checked]in Angular) - Generate event handlers
- Format imports and component usage
Updates example app files using marked regions:
<!-- <AUTO-GENERATED-COMPONENTS> -->
<!-- Generated code appears here -->
<!-- </AUTO-GENERATED-COMPONENTS> -->User code outside these markers is preserved.
- Extract: Parse Storybook stories to extract component information
- Supports both CSF2 (function exports) and CSF3 (object with render function)
- Example CSF3:
export const Default: StoryObj = { render: (args) => html...}
- Format: Use formatters to convert data to framework-specific code
- Update: Inject generated code into example apps using marked regions
- Create a Formatter in
src/formatters/ - Create a Generator in
src/generators/ - Register the generator in
src/index.ts
Example:
// formatters/svelte-formatter.ts
export class SvelteCodeFormatter implements ICodeFormatter {
formatComponent(component: ComponentInfo): string {
// Convert to Svelte syntax
}
}
// generators/svelte-generator.ts
export class SvelteExampleGenerator implements IExampleGenerator {
generate(components: ComponentInfo[], config: GeneratorConfig): GenerationResult {
// Generate Svelte examples
}
}
// index.ts
const generators = [
new HTMLExampleGenerator(),
new ReactExampleGenerator(),
new SvelteExampleGenerator() // Add new generator
];cd example-generator
pnpm run start # Generate examples onceOutput updates all example apps in examples/ directory.
For usage instructions, see README.md