-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook: Add badges based on
tags
(#61111)
* Consolidate tags in stories * DimensionControl: Standardize story meta format for easier parsing * Storybook: Add badges based on `tags` Co-authored-by: mirka <[email protected]> Co-authored-by: tyxla <[email protected]>
- Loading branch information
1 parent
e357a62
commit 48422a4
Showing
11 changed files
with
99 additions
and
15 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
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
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
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,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const babel = require( '@babel/core' ); | ||
|
||
/** | ||
* Reads `tags` from the story metadata and copies them to `badges`. | ||
* | ||
* @see https://github.com/geometricpanda/storybook-addon-badges | ||
* @see '../badges.js' - Badges config | ||
*/ | ||
function copyTagsToBadgePlugin() { | ||
return { | ||
visitor: { | ||
ExportDefaultDeclaration( visitorPath ) { | ||
const properties = | ||
// When default export is anonymous, the declaration is an object expression | ||
visitorPath.node?.declaration.properties ?? | ||
// When default export is named, the declaration is an identifier, usually the previous node | ||
visitorPath.getPrevSibling().node.declarations[ 0 ].init | ||
.properties; | ||
|
||
alterParameters( properties ); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function alterParameters( properties ) { | ||
const STATUS_PREFIX = 'status-'; | ||
|
||
const tags = | ||
properties | ||
.find( ( op ) => op.key.name === 'tags' ) | ||
?.value.elements.map( ( tag ) => tag.value ) ?? []; | ||
const statusTags = tags.filter( ( tag ) => | ||
tag.startsWith( STATUS_PREFIX ) | ||
); | ||
|
||
const badges = babel.types.objectProperty( | ||
babel.types.identifier( 'badges' ), | ||
babel.types.arrayExpression( | ||
statusTags.map( ( tag ) => | ||
babel.types.stringLiteral( | ||
tag.substring( STATUS_PREFIX.length ) | ||
) | ||
) | ||
) | ||
); | ||
|
||
let parameters = properties.find( ( op ) => op.key.name === 'parameters' ); | ||
|
||
if ( ! parameters ) { | ||
parameters = babel.types.objectProperty( | ||
babel.types.identifier( 'parameters' ), | ||
babel.types.objectExpression( [] ) | ||
); | ||
properties.push( parameters ); | ||
} | ||
|
||
parameters.value.properties = [ badges, ...parameters.value.properties ]; | ||
} | ||
|
||
module.exports = function ( source ) { | ||
const output = babel.transform( source, { | ||
plugins: [ copyTagsToBadgePlugin ], | ||
filename: this.resourcePath, | ||
sourceType: 'module', | ||
} ); | ||
return output.code; | ||
}; |