-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
Is it possible to automatically add README's based on the module path? #83
Comments
To implement this we should patch webpack config.
I would be happy for any help. |
Work to do! Okay, I will keep this in mind. Thanks for the feedback. Not sure when / if I will get time to work on this, but I'll see what I can do. |
Yes, it's possible: I have done it like this: config.js import React from 'react'
import path from 'path'
import { ThemeDecorator } from './decorators/index'
import {
configure,
storiesOf,
getStorybook,
addDecorator,
} from '@storybook/react'
import { withKnobs } from '@storybook/addon-knobs/react'
import { withReadme } from 'storybook-readme'
import { withOptions } from '@storybook/addon-options'
const getPackageName = filePath =>
path
.dirname(filePath)
.split(path.sep)
.reverse()[1]
const req = require.context(
'../../../components',
true,
/^((?!node_modules).)*\.example\.(js|tsx)$/
)
const readMeReq = require.context(
'../../../components',
true,
/^((?!node_modules).)*\README\.md$/
)
addDecorator(
withOptions({
name: 'Components',
})
)
configure(() => {
req.keys().forEach(pathToExample => {
const { name, Example, options = {} } = req(pathToExample)
const packageName = getPackageName(pathToExample)
const readmePath = readMeReq.keys().find(rm => rm.includes(packageName))
const readme = readMeReq(readmePath)
storiesOf(packageName, module)
.addDecorator(withKnobs)
.addDecorator(withReadme(readme))
.addDecorator(withOptions(options))
.addDecorator(ThemeDecorator)
.add(name, () => <Example />, { options })
})
}, module)
export { getStorybook } HelloWorld.example.js: import React from 'react'
import HelloWorld from 'path/to/hello-world'
export const name = 'Default'
export const Example = () => (
<HelloWorld />
) |
We're planning on making this a feature of addon-notes in the future too, so "it just works". Great setup you discovered there @kristof0425 ! |
@tuchk4 - Will this implementation mentioned above from @kristof0425 still work with v5? I am only getting it to work for one of my components?
|
@jephjohnson yes, it should work readme: {
content: `
<!-- STORY -->
<!-- PROPS -->
`,
sidebar: readme,
},
}) |
@tuchk4 - Ah, stil no dice. |
Here is working example:
Button.example.js import React from 'react';
import Button from './';
export const name = 'Default';
export const Example = () => <Button label="Hi" />;
// copy proptypes so <!-- PROPS --> will work
Example.propTypes = Button.propTypes; |
The decorator was in there. I’m thinking it’s related to my es lint folder. Still having the same issue of it not iterating through all the readmes |
Thinking that I should to add @kristof0425's solution to README and close this issue |
That would be awesome! 🎉 @tuchk4 Also thanks for the v5 support! |
@kristof0425 Need you advice :) I would like to add to docs how to automatically add README based on the story path. When tests faced the problem:
In your example this function is used to get all stories and README files by specific pattern. Found this issue storybookjs/storybook#4479 Do we need to install additional babel plugins to use your solution? |
Currently, this is the setup we use for Storybook:
Webpack configconst themeOverride = require('../webpack.config.theme.override')
process.env.NODE_ENV = 'development'
module.exports = {
resolve: {
extensions: ['.mjs', '.ts', '.tsx', '.js'],
},
module: {
rules: [
{
test: /\.(js|ts|tsx)$/,
use: ['babel-loader'],
},
{
test: /\.svg$/,
loader: 'file-loader',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.less$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'less-loader',
options: {
modifyVars: themeOverride,
javascriptEnabled: true,
},
},
],
},
],
},
devtool: 'source-map',
} Storybook configimport React from 'react'
import path from 'path'
import { ThemeDecorator } from './decorators/index'
import {
configure,
storiesOf,
getStorybook,
addParameters,
} from '@storybook/react'
import { withKnobs } from '@storybook/addon-knobs/react'
import { addReadme } from 'storybook-readme'
const getPackageName = filePath =>
path
.dirname(filePath)
.split(path.sep)
.reverse()[1]
const getPathToExample = path => {
let hasReachedExamples = false
return path
.split('/')
.filter((folder, i) => {
// first two folders are common
if (folder === 'examples') hasReachedExamples = true
if (i < 2 || hasReachedExamples || folder.includes('.tsx')) return false
else return true
})
.join('/')
.replace(/.example.(js|tsx)/, '')
}
const req = require.context(
'../../../components',
true,
/^((?!node_modules).)*\.example\.(js|tsx)$/
)
const readMeReq = require.context(
'../../../components',
true,
/^((?!node_modules).)*\README\.md$/
)
addParameters({
name: 'MK-Components',
})
configure(() => {
req.keys().forEach(pathToExample => {
const { name, Example, options = {} } = req(pathToExample)
const packageName = getPackageName(pathToExample)
const readmePath = readMeReq.keys().find(rm => rm.includes(packageName))
const readme = readMeReq(readmePath)
const path = getPathToExample(pathToExample)
storiesOf(path, module)
.addDecorator(
withKnobs({
escapeHTML: false,
})
)
.addDecorator(addReadme)
.addParameters({
readme: {
sidebar: readme,
},
})
.addParameters(options)
.addDecorator(ThemeDecorator)
.add(name, () => <Example />, { options })
})
}, module)
export { getStorybook } Babel config (I'm not sure whether it's relevant to this problem) module.exports = {
presets: [
'@babel/preset-env',
'@babel/preset-react',
'@babel/preset-flow',
'@babel/typescript',
],
plugins: [
'@babel/plugin-transform-async-to-generator',
[
'import',
{
libraryName: 'antd',
style: true,
},
],
'react-hot-loader/babel',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-json-strings',
[
'@babel/plugin-proposal-decorators',
{
legacy: true,
},
],
'@babel/plugin-proposal-function-sent',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-throw-expressions',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-logical-assignment-operators',
'@babel/plugin-proposal-optional-chaining',
[
'@babel/plugin-proposal-pipeline-operator',
{
proposal: 'minimal',
},
],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-do-expressions',
'@babel/plugin-proposal-function-bind',
],
env: {
test: {
plugins: [
'emotion',
[
'@babel/plugin-transform-modules-commonjs',
{
loose: true,
},
],
],
},
development: {
plugins: [
['emotion', { sourceMap: true, autoLabel: true }],
'@babel/plugin-transform-modules-commonjs',
'babel-plugin-dynamic-import-node',
],
},
production: {
plugins: [
['emotion', { hoist: true }],
'@babel/plugin-transform-modules-commonjs',
],
},
},
} @tuchk4 If you could share with me how you test the package, I can dig into the problem myself as well. 🙂 |
@kristof0425 @tuchk4 - Hey guys, sorry for the delay...Any new feedback on this? Still haven't got it resolved on my end. Here is the pakage.json.
|
Hey @tuchk4 This would become a much easier process once storybook config has changed to monoconfig: |
@ndelangen nice new feature! Seems it will be possible to implement awesome new features much easier :) |
@tuchk4 I put together a repo that demonstrates the error @jephjohnson has been describing. https://github.com/fgaleano/readme Just The main problem is that prop's You can see Storybook's configuration in Let us know if you can reproduce the error as I'm describing it. Thank you! |
@kristof0425 @tuchk4 - Any thoughts on the repo above? Thanks, Jeph |
@jephjohnson sorry for the delay, had hard working days. |
Hi @tuchk4 - Just touching base on this. Anything new? Thanks, J |
Is there any way to have the addon automatically add the local
README.md
? Given a consistent file structure, I was hoping to be able to just always load the README for a given story, without having to add the decorator to every single story.For example, I use:
for every component.
The text was updated successfully, but these errors were encountered: