Last active
November 28, 2023 12:46
-
-
Save mpalpha/8f0414e0013ba26bd12cca3d78614456 to your computer and use it in GitHub Desktop.
Storybook render stories source code to prettier formatted html. supports js, jsx, ts, tsx, and mdx formats. tested with "@storybook/html": "^6.2.5"
This file contains hidden or 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
// .storybook/main.js | |
module.exports = { | |
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'], | |
addons: [ | |
'@storybook/addon-links', | |
'@storybook/addon-essentials' | |
], | |
}; |
This file contains hidden or 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
// .storybook/preview.js | |
import renderToHTML from './render.html' | |
export const parameters = { | |
actions: { argTypesRegex: '^on[A-Z].*' }, | |
docs: { | |
transformSource: (src, storyContext) => renderToHTML(storyContext.storyFn), | |
}, | |
} |
This file contains hidden or 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
// .storybook/render.html.js | |
/** | |
* Render stories to html formatted source code inside previews | |
* for the @storybook/addon-docs package which is included in | |
* the @storybook/addon-essentials package | |
*/ | |
import { renderToStaticMarkup } from 'react-dom/server'; | |
import { AllHtmlEntities } from 'html-entities'; | |
import prettier from 'prettier'; | |
import HTMLParser from 'prettier/parser-html'; | |
const entities = new AllHtmlEntities(); | |
export default (story) => | |
prettier.format(entities.decode(renderToStaticMarkup(story())), { | |
parser: 'html', | |
plugins: [HTMLParser], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone else needs this for Storybook 7, these changes work for me:
preview.js
render.html.js