Skip to content

Instantly share code, notes, and snippets.

@gnarf
Last active February 18, 2019 10:39
Show Gist options
  • Save gnarf/ea9bd0f76e36516b6f1f38e4aba542a3 to your computer and use it in GitHub Desktop.
Save gnarf/ea9bd0f76e36516b6f1f38e4aba542a3 to your computer and use it in GitHub Desktop.
An example of how to use storybook-addon-info with real source files and readme support
const storyFiles = require.context('../src', true, /__stories__\/.+\.js$/)
const sources = require.context('!!raw-loader!../src', true,
/__stories__\/.+\.js$/)
const readme = require.context('!!raw-loader!../src', true, /README\.md$/)
const READMES = readme.keys().reduce((memo, key) => {
const [,baseName] = key.match(/^\.\/([^\/]+)\//)
memo[baseName] = readme(key)
return memo
}, {})
const generateStories = storyFiles.keys()
generateStories.forEach(jsSource => {
const source = sources(jsSource)
const story = storyFiles(jsSource).default
// take the folder name
const [,baseName,storyName] = jsSource.match(/^\.\/([^\/]+)\/__stories__\/(.+)\.js$/)
const readmemd = READMES[baseName] ? `## README
${READMES[baseName]}` : ''
const md = `${readmemd}
## Story Source:
\`\`\` javascript
${source}
\`\`\`
`
const group = storiesOf(baseName, module)
group.addWithInfo(storyName, md, story, {inline: true, source: false, propTables: false})
})
import React from 'react'
import Button from '../Button'
import { action } from '@kadira/storybook'
export default () => <Button onClick={action('clicked')}>Button</Button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment