-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook: Add webpack loader for easier story descriptions (#39165)
* Storybook: Add webpack loader for story descriptions * Convert story descriptions for FontSizePicker
- Loading branch information
Showing
3 changed files
with
120 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Allows a story description to be written as a doc comment above the exported story. | ||
* | ||
* Based on https://github.com/izhan/storybook-description-loader | ||
* | ||
* @example | ||
* ```jsx | ||
* // This comment will become the description for the story in the generated docs. | ||
* export const MyStory = Template.bind({}); | ||
* ``` | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const babel = require( '@babel/core' ); | ||
|
||
function createDescriptionNode( name, description ) { | ||
return babel.types.expressionStatement( | ||
babel.types.assignmentExpression( | ||
'=', | ||
babel.types.memberExpression( | ||
babel.types.identifier( name ), | ||
babel.types.identifier( 'story' ) | ||
), | ||
babel.types.objectExpression( [ | ||
babel.types.objectProperty( | ||
babel.types.identifier( 'parameters' ), | ||
babel.types.objectExpression( [ | ||
babel.types.objectProperty( | ||
babel.types.identifier( 'docs' ), | ||
babel.types.objectExpression( [ | ||
babel.types.objectProperty( | ||
babel.types.identifier( | ||
'storyDescription' | ||
), | ||
babel.types.stringLiteral( description ) | ||
), | ||
] ) | ||
), | ||
] ) | ||
), | ||
] ) | ||
) | ||
); | ||
} | ||
|
||
function annotateDescriptionPlugin() { | ||
return { | ||
visitor: { | ||
ExportNamedDeclaration( path ) { | ||
if ( path.node.leadingComments ) { | ||
const commentValues = path.node.leadingComments.map( | ||
( node ) => { | ||
if ( node.type === 'CommentLine' ) { | ||
return node.value.trimLeft(); | ||
} | ||
// else, node.type === 'CommentBlock' | ||
return node.value | ||
.split( '\n' ) | ||
.map( ( line ) => { | ||
// stripping out the whitespace and * from comment blocks | ||
return line.replace( | ||
/^(\s+)?(\*+)?(\s+)?/, | ||
'' | ||
); | ||
} ) | ||
.join( '\n' ) | ||
.trim(); | ||
} | ||
); | ||
const description = commentValues.join( '\n' ); | ||
const declaration = path.node.declaration.declarations[ 0 ]; | ||
|
||
path.insertAfter( | ||
createDescriptionNode( | ||
declaration.id.name, | ||
description | ||
) | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = function ( source ) { | ||
const output = babel.transform( source, { | ||
plugins: [ annotateDescriptionPlugin ], | ||
filename: __filename, | ||
sourceType: 'module', | ||
} ); | ||
return output.code; | ||
}; |